简体   繁体   English

Pandas 重新采样工作日并填写未填写

[英]Pandas resample business days and ffill not filling

I have the following test data frame:我有以下测试数据框:


                 data
date                  
2021-03-01  3968513.99
2021-03-02  5909640.34
2021-03-03  6452578.11
2021-03-04  7260439.94
2021-03-05  6659379.74
2021-03-08  6693275.88
2021-03-09  6861187.90
2021-03-10  6716384.27
2021-03-11  6772700.54
2021-03-12  6461420.20
2021-03-15  6005397.14
2021-03-16  4695128.31
2021-03-17  4271115.19
2021-03-18  3927571.69
2021-03-19  3329363.28
2021-03-22  3395927.62
2021-03-23  3163365.76
2021-03-24  2876007.38
2021-03-25  2812801.36
2021-03-26  2854624.55
2021-03-29  2893050.72
2021-03-30  2094476.41
2021-03-31  1923014.42
2021-04-01         NaN
2021-04-02         NaN
2021-04-05         NaN
2021-04-06  1428389.82
2021-04-07  1381988.35
2021-04-08  1256461.70
2021-04-09  1324881.19
2021-04-12  1374734.95
2021-04-13  1261978.35

When I run the following however resample and ffill:当我运行以下但是重新采样和填充时:

test_df.resample("B").ffill()

I get exactly the same as above, ie nothing was filled.我得到与上面完全相同的结果,即没有填充任何内容。 The first few dates of april are still NaN. 4 月的前几个日期仍然是 NaN。

I'm trying to work out the exact logic that resample is doing here, even if I set the limit as very large (60 say) it still doesn't fill these values.我正在尝试计算 resample 在这里执行的确切逻辑,即使我将限制设置为非常大(例如 60),它仍然无法填充这些值。

EDIT: When you remove the lines completely from the test data which are NaN, ffill then works fine when it adds the dates itself.编辑:当您从测试数据中完全删除 NaN 行时, ffill 在添加日期本身时可以正常工作。

Using fillna or ffill before resampling should do the trick.在重新采样之前使用fillnaffill应该可以解决问题。 Here is some code with the mean() aggregation method:下面是一些带有 mean() 聚合方法的代码:

df.fillna(method='ffill').resample('B').mean()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM