问题: get_group方法是否适用于具有DatetimeIndexResamplerGroupby索引的DataFrame?如果是这样,那么适当的语法是什么?
示例数据:
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
get_group方法适用于pandas.core.groupby.DataFrameGroupBy object
。
我尝试了各种方法,典型的错误是TypeError: Cannot convert input [(0, 1)] of type <class 'tuple'> to Timestamp
答案 0 :(得分:0)
以下内容应该是您正在寻找的内容(如果我理解正确的话):
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
我相信resample / pd.Grouper在这种情况下可以互换使用(如果我错了,有人会纠正我)。如果这对您有用,请告诉我。