我想按时间戳(日期)分组并按时间戳访问每个组,这看起来无法正常工作。看起来组键奇怪地用不同的格式索引。
df= pd.DataFrame({'DATE' : ['10-Oct-2013', '10-Oct-2013', '10-Oct-2013', '11-Oct-2013', '11-Oct-2013', '11-Oct-2013'],'VAL' : [1,2,3,4,5,6]})
>>> df
DATE VAL
0 10-Oct-2013 1
1 10-Oct-2013 2
2 10-Oct-2013 3
3 11-Oct-2013 4
4 11-Oct-2013 5
5 11-Oct-2013 6
dfg=df.groupby(df['DATE'].apply(lambda x: pd.to_datetime(x)))
>>> dfg.groups.keys()
[numpy.datetime64('NaT'), numpy.datetime64('2013-10-10T17:00:00.000000000-0700'), numpy.datetime64('2013-10-09T17:00:00.000000000-0700')]
for d in dfg.groups.keys():
try:
print d,dfg.get_group(d).describe()
except:
print 'err'
>>
NaT err
2013-10-10T17:00:00.000000000-0700 err
2013-10-09T17:00:00.000000000-0700 err
rng = pd.to_datetime(pd.date_range('10/10/2013', periods=3, freq='D'))
for d in rng:
try:
print d,dfg.get_group(d).describe()
except:
print 'err'
2013-10-10 00:00:00 err
2013-10-11 00:00:00 err
2013-10-12 00:00:00 err