grouping by weekly break down of datetimes 64 python

221 Views Asked by At

I have a pandas data frame with a column that represents dates as:

Name: ts_placed, Length: 13631, dtype: datetime64[ns]

It looks like this:

0    2014-10-18 16:53:00
1    2014-10-27 11:57:00
2    2014-10-27 11:57:00
3    2014-10-08 16:35:00
4    2014-10-24 16:36:00
5    2014-11-06 15:34:00
6    2014-11-11 10:30:00
....

I know how to group it in general using the function:

grouped = data.groupby('ts_placed')

What I want to do is to use the same function but to group the rows by week.

2

There are 2 best solutions below

5
On BEST ANSWER

Pass

pd.DatetimeIndex(df.date).week

as the argument to groupby. This is the ordinal week in the year; see DatetimeIndex for other definitions of week.

0
On

you can also use Timergrouper

df.set_index(your_date).groupby(pd.TimeGrouper('W')).size()