重新采样不适用于第一个数据
我尝试将年度数据转换为每月,从2001年到2020年, 但是,2001年的数据未转换为月度。
原始数据集看起来如下:
0 2001-12-31 16.5648 4.9887 11.1706 ... 6.4568 7.9525 7.7998 11.2796
1 2002-12-12-31 15.3589 14.1851
14.1851 11.4080 ... 54 ... 8.6358 9.0530 9.4951 11.8169
3 2004-12-31 13.4166 15.6389 12.9674 ... 9.9278 10.7078 11.1832 13.0509
4 20055-12-31
有人
df4['Date']=pd.to_datetime(df4['Date'])
df4 = df4.set_index('Date').resample('M').bfill().reset_index()
输出:
2001-12-31 16.5648 4.9887 11.1706 ... 7.9525 7.7998 11.2796
2002-01-31 15.3589 14.1851 11.4080 ... 7.6465 8.1067 11.6526
2002-02-28 15.3589 14.1851 11.4080 ... 7.6465 8.1067 11.6526
2002-03-31 15.3589 14.1851 11.4080 ... 7.6465 8.1067 11.6526
2002-04-30 15.3589 14.1851 11.4080 ... 7.6465 8.1067 11.6526
可以告诉我我错过了什么吗?谢谢
I tried to convert yearly data to monthly, starting from 2001 to 2020,
however 2001 data was not converted to monthly.
The original dataset looks like:
0 2001-12-31 16.5648 4.9887 11.1706 ... 6.4568 7.9525 7.7998 11.2796
1 2002-12-31 15.3589 14.1851 11.4080 ... 7.7080 7.6465 8.1067 11.6526
2 2003-12-31 10.0559 15.1041 11.5154 ... 8.6358 9.0530 9.4951 11.8169
3 2004-12-31 13.4166 15.6389 12.9674 ... 9.9278 10.7078 11.1832 13.0509
4 2005-12-31 13.3857 16.7083 8.9411 ... 11.0563 12.7320 11.9903 13.0580
What I tried:
df4['Date']=pd.to_datetime(df4['Date'])
df4 = df4.set_index('Date').resample('M').bfill().reset_index()
The output:
2001-12-31 16.5648 4.9887 11.1706 ... 7.9525 7.7998 11.2796
2002-01-31 15.3589 14.1851 11.4080 ... 7.6465 8.1067 11.6526
2002-02-28 15.3589 14.1851 11.4080 ... 7.6465 8.1067 11.6526
2002-03-31 15.3589 14.1851 11.4080 ... 7.6465 8.1067 11.6526
2002-04-30 15.3589 14.1851 11.4080 ... 7.6465 8.1067 11.6526
Can anybody tell me what I missed? Thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您正在做的是将数据从每年重新采样为每月,然后调用
bfill
用常数值向后填充数据间隙。如果您想插入间隙,您应该调用
resample('M').interpolate()
。What you're doing is resampling the data from yearly to monthly and then calling
bfill
to backwards fill the data gaps with constant value.If you would like to interpolate the gaps, you should call
resample('M').interpolate()
instead.