将Pandas时间序列对象转换为数据框(DataFrame)

4
我希望将以下类型的<'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
...

有没有其他解决方案可以提供?

它们的功能基本上是相同的。数据框是数据列和索引,而系列对象基本上是单个数据列和索引。你需要数据框来做什么? - James
1个回答

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接