重新采样不适用于第一个数据

发布于 2025-01-20 03:31:26 字数 908 浏览 3 评论 0原文

我尝试将年度数据转换为每月,从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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冷默言语 2025-01-27 03:31:26

您正在做的是将数据从每年重新采样为每月,然后调用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文