http://stackoverflow.com/questions/21878599/pandas-drops-timestamp-columns-in-resample
init_time=pd.date_range(start='2010-01-01 00:00', end='2010-01-05 00:00', freq='12H')
valid_time=pd.date_range(start='2010-01-01 00:00', end='2010-01-05 00:00', freq='12H')
data = np.random.random(len(valid_time))
frame = pd.DataFrame(index=valid_time, data=data)
frame['init_time'] = init_time
Works, but a little hacky
In [48]: numeric = frame.resample('D',how='mean')
In [49]: datelike = frame.loc[:,frame.dtypes.isin([np.dtype('datetime64[ns]')])]
In [50]: datelike = datelike.resample('D',
how=lambda x: Timestamp(x.view('i8').mean()))
In [51]: concat([numeric,datelike],axis=1)
Out[51]:
0 init_time
2010-01-01 0.798880 2010-01-01 06:00:00
2010-01-02 0.859781 2010-01-02 06:00:00
2010-01-03 0.515503 2010-01-03 06:00:00
2010-01-04 0.505557 2010-01-04 06:00:00
2010-01-05 0.979835 2010-01-05 00:00:00
[5 rows x 2 columns]
Comment From: jbrockmendel
frame.resample("D").mean()
now retains the dt64 column. i think this is closable?
Comment From: MarcoGorelli
yeah this seems to work now
In [24]: frame
Out[24]:
0 init_time
2010-01-01 00:00:00 0.110461 2010-01-01 00:00:00
2010-01-01 12:00:00 0.063558 2010-01-01 12:00:00
2010-01-02 00:00:00 0.016708 2010-01-02 00:00:00
2010-01-02 12:00:00 0.685998 2010-01-02 12:00:00
2010-01-03 00:00:00 0.607047 2010-01-03 00:00:00
2010-01-03 12:00:00 0.090777 2010-01-03 12:00:00
2010-01-04 00:00:00 0.871941 2010-01-04 00:00:00
2010-01-04 12:00:00 0.928479 2010-01-04 12:00:00
2010-01-05 00:00:00 0.272012 2010-01-05 00:00:00
In [25]: frame.resample('D').mean()
Out[25]:
0 init_time
2010-01-01 0.087010 2010-01-01 06:00:00
2010-01-02 0.351353 2010-01-02 06:00:00
2010-01-03 0.348912 2010-01-03 06:00:00
2010-01-04 0.900210 2010-01-04 06:00:00
2010-01-05 0.272012 2010-01-05 00:00:00
closing then