Pandas resample business days and ffill not filling

120

Question: 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, i.e. nothing was filled. python error The first few dates of april are still NaN.

I'm trying to work out the exact logic that resample is doing here, even if I python error set the limit as very large (60 say) it still doesn't fill these values.

EDIT: When you remove the lines completely from the test data python error which are NaN, ffill then works fine when it adds the dates itself.

Total Answers: 1

5

Answers 1: of Pandas resample business days and ffill not filling

Using fillna or ffill before resampling should do the trick. Here is some code with the mean() aggregation method :

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