0

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.

Ami Tavory
  • 74,578
  • 11
  • 141
  • 185
Blue Moon
  • 4,421
  • 20
  • 52
  • 91

2 Answers2

0

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.

Ami Tavory
  • 74,578
  • 11
  • 141
  • 185
  • it works. just one more thing. the output is do you know how can I see it? sorry for the nooby question but I can't find it anyway. – Blue Moon Jun 26 '15 at 13:17
  • Oh, you need to refer to the [`groupby` docs](http://pandas.pydata.org/pandas-docs/stable/groupby.html). Groupby is just a step - you need to do something with the result, e.g., `df.groupby(...).mean()`. I really suggest you read the docs. – Ami Tavory Jun 26 '15 at 13:33
  • once I have grouped them how do I save them as separated datasets? – Blue Moon Jun 26 '15 at 13:59
  • You mean to separate files? That's a legitimate question, but I really think it's a separate one. It's not that easy to converse via these comments. – Ami Tavory Jun 26 '15 at 14:04
  • I'll ask one more question then. Thank you – Blue Moon Jun 26 '15 at 14:09
0

you can also use Timergrouper

df.set_index(your_date).groupby(pd.TimeGrouper('W')).size()
steboc
  • 1,161
  • 1
  • 7
  • 17