'Pandas get_group method on DatetimeIndexResamplerGroupby

Question: Does the get_group method work on a DataFrame with a DatetimeIndexResamplerGroupby index? If so, what is the appropriate syntax?

Sample data:

data = [[2, 4, 1, datetime.datetime(2017, 1, 1)],
        [2, 4, 2, datetime.datetime(2017, 1, 5)], 
        [3, 4, 1, datetime.datetime(2017, 1, 7)]]
df1 = pd.DataFrame(data, columns=list('abc') + ['dates'])

gb3 = df1.set_index('dates').groupby('a').resample('D')
DatetimeIndexResamplerGroupby [freq=<Day>, axis=0, closed=left, label=left, convention=e, base=0]

gb3.sum()

                  a   b   c
a   dates           
2   2017-01-01  2.0 4.0 1.0
    2017-01-02  NaN NaN NaN
    2017-01-03  NaN NaN NaN
    2017-01-04  NaN NaN NaN
    2017-01-05  2.0 4.0 2.0
3   2017-01-07  3.0 4.0 1.0

The get_group method is working for me on a pandas.core.groupby.DataFrameGroupBy object. I've tried various approaches, the typical error is TypeError: Cannot convert input [(0, 1)] of type <class 'tuple'> to Timestamp



Solution 1:[1]

The below should be what you're looking for (if I understand the question correctly):

import pandas as pd
import datetime
?

data = [[2, 4, 1, datetime.datetime(2017, 1, 1)],
        [2, 4, 2, datetime.datetime(2017, 1, 5)], 
        [3, 4, 1, datetime.datetime(2017, 1, 7)]]
df1 = pd.DataFrame(data, columns=list('abc') + ['dates'])
gb3 = df1.groupby(['a',pd.Grouper('dates')])
gb3.get_group((2, '2017-01-01'))
?
Out[14]:
a   b   c   dates
0   2   4   1   2017-01-01

I believe resample/pd.Grouper can be used interchangeably in this case (someone correct me if I'm wrong). Let me know if this works for you.

Solution 2:[2]

Yes it does, the following code returns the monthly values sum of the year 2015

df.resample('MS').sum().resample('Y').get_group('2015-12-31')

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Andrew L
Solution 2 user17672697