在 pandas 中取样数据框 [英] Upsampling Dataframe in Pandas

查看:75
本文介绍了在 pandas 中取样数据框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

鉴于按月索引的数据框,我希望按日索引(上采样)。以前按月索引的值现在应该除以该月份的天数。

Given a dataframe indexed by month, I'd like to reindex by day (upsample). Values that were previously indexed by month should now be divided by the number of days in the month.

推荐答案

尝试:

tidx_m = pd.date_range('2011-01-31', periods=2, freq='M')
tidx_d = pd.date_range('2011-01-01', '2011-02-28', freq='D')

d = pd.Series(100, tidx_m)
d.reindex(tidx_d, fill_value=0).groupby(pd.TimeGrouper('M')).transform('mean')

收益率

2011-01-01    3.225806
2011-01-02    3.225806
2011-01-03    3.225806
2011-01-04    3.225806
2011-01-05    3.225806
2011-01-06    3.225806
2011-01-07    3.225806
2011-01-08    3.225806
2011-01-09    3.225806
2011-01-10    3.225806
2011-01-11    3.225806
2011-01-12    3.225806
2011-01-13    3.225806
2011-01-14    3.225806
2011-01-15    3.225806
2011-01-16    3.225806
2011-01-17    3.225806
2011-01-18    3.225806
2011-01-19    3.225806
2011-01-20    3.225806
2011-01-21    3.225806
2011-01-22    3.225806
2011-01-23    3.225806
2011-01-24    3.225806
2011-01-25    3.225806
2011-01-26    3.225806
2011-01-27    3.225806
2011-01-28    3.225806
2011-01-29    3.225806
2011-01-30    3.225806
2011-01-31    3.225806
2011-02-01    3.571429
2011-02-02    3.571429
2011-02-03    3.571429
2011-02-04    3.571429
2011-02-05    3.571429
2011-02-06    3.571429
2011-02-07    3.571429
2011-02-08    3.571429
2011-02-09    3.571429
2011-02-10    3.571429
2011-02-11    3.571429
2011-02-12    3.571429
2011-02-13    3.571429
2011-02-14    3.571429
2011-02-15    3.571429
2011-02-16    3.571429
2011-02-17    3.571429
2011-02-18    3.571429
2011-02-19    3.571429
2011-02-20    3.571429
2011-02-21    3.571429
2011-02-22    3.571429
2011-02-23    3.571429
2011-02-24    3.571429
2011-02-25    3.571429
2011-02-26    3.571429
2011-02-27    3.571429
2011-02-28    3.571429
Freq: D, dtype: float64

这篇关于在 pandas 中取样数据框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆