1

Let's take the following DataFrame:

df = pd.DataFrame({
'Buyer': 'Carl Mark Joe John Joe Mark Carl'.split(),
'Quantity': [1,3,5,8,9,3,3],
'Date' : [
    DT.datetime(2013,9,1,13,0),
    DT.datetime(2013,9,1,13,5),
    DT.datetime(2013,10,1,20,0),
    DT.datetime(2013,10,3,10,0),
    DT.datetime(2013,12,2,12,0),                                      
    DT.datetime(2013,12,2,14,0),
    DT.datetime(2013,12,2,14,0)
    ]})
df.set_index('Date', inplace=True)

With that I would like to use a weekly grouping using:

df.groupby(TimeGrouper('1W')).apply(mytest)

is there any possibility to get the corresponding week anyhow in the mytest function. I am asking because I need to query in the mytest function another DataFrame which has the same week index as the one which is derived through the applied weekly grouping.

Any help is appreciated

EdChum
  • 376,765
  • 198
  • 813
  • 562
Andy
  • 9,483
  • 12
  • 38
  • 39
  • 1
    What are you actually trying to do with "mytest", it looks like what you're looking for is actually a df.groupby and then an agg which can view the index, or a df.resample and then an apply which can also reference the index – Nicholas Aldershof May 28 '15 at 16:31
  • I created two other datasets using groupby(TimeGrouper('1W')) which are very similar to the timeseries in the particular DataFrame and I would like to access their corresponding content in mytest – Andy May 28 '15 at 16:47
  • Does `pd.TimeGrouper('1W')._get_binner_for_grouping(df)` give you the information you are looking for? If so, you could assign it to a column of `df`. Unfortunately, this requires accessing a private method... – unutbu May 28 '15 at 16:53
  • @unutbu I seem to remember that the individual groups have a `.name` attribute. seems like the applied function could access that. – Paul H May 28 '15 at 17:33
  • @PaulH: That looks like a viable solution. – unutbu May 28 '15 at 17:49

1 Answers1

1

I'm not sure what your TimeGrouper('1W') is, so I assumed you want to group by week (the principle is the same in any case).

First, it's very easy to create a week groupby criterion:

df.Date = pd.to_datetime(df.Date)
week = df.Date.apply(lambda d: d.week)

and groupby according to it:

df.groupby(week).apply(mytest)

However, within mytest, it's exactly the same:

def mytest(f):
    print f.Date.apply(lambda d: d.week).values[0]
    return 0

As you see, you have access to the week.

Ami Tavory
  • 74,578
  • 11
  • 141
  • 185
  • Hi thanks for your help, but d.week just gives me an integer, I would like to get somehow '2013-09-01' for the first two and '2013-10-06' for the third. Just do df.groupby(TimeGrouper('1W')).first() to reconstruct – Andy May 28 '15 at 16:44
  • It's the same idea, but with a different key (the week not in number form, but in full-date form). See [this question](http://stackoverflow.com/questions/19502506/convert-numpy-datetime64-to-string-object-in-python) for that. – Ami Tavory May 28 '15 at 17:07