将Pandas tseries对象转换为DataFrame

时间:2016-09-05 16:13:22

标签: python pandas dataframe time-series resampling

我希望将以下<'pandas.tseries.resample.DatetimeIndexResampler'>类型对象转换为pandas DataFrame对象(<'pandas.core.frame.DataFrame'>)。但是我无法在pandas文档中找到相关功能以允许我这样做。

数据采用以下形式:

                  M30
Date                 
2016-02-29  -61.187699
2016-03-31  -60.869565
2016-04-30  -61.717922
2016-05-31  -61.823966
2016-06-30  -62.142100
...

任何人都可以提供替代解决方案吗?

1 个答案:

答案 0 :(得分:6)

您需要一些汇总功能,例如summean

使用您的数据示例:

print (df)
                  M30
Date                 
2016-02-29 -61.187699
2016-03-31 -60.869565
2016-04-30 -61.717922
2016-05-31 -61.823966
2016-06-30 -62.142100

#resample by 2 months
r = df.resample('2M')
print (r)
DatetimeIndexResampler [freq=<2 * MonthEnds>, 
                        axis=0, 
                        closed=right, 
                        label=right, 
                        convention=start, 
                        base=0]
#aggregate sum
print (r.sum())
                   M30
Date                  
2016-02-29  -61.187699
2016-04-30 -122.587487
2016-06-30 -123.966066

#aggregate mean
print (r.mean())
                  M30
Date                 
2016-02-29 -61.187699
2016-04-30 -61.293743
2016-06-30 -61.983033

#aggregate first
print (r.first())
                  M30
Date                 
2016-02-29 -61.187699
2016-04-30 -60.869565
2016-06-30 -61.823966