pythonpandaspandas-resampleffill

Also ffill last value when resampling in pandas


I want to resample a dataframe and ffill values, see code below. However, the last value isn't forward filled.

df=pd.DataFrame(index=pd.date_range(start='1/1/2022',periods=5,freq='h'),data=range(0,5))
print(df.resample('15T').ffill())

results in:

                         0
2022-01-01 00:00:00  0
2022-01-01 00:15:00  0
2022-01-01 00:30:00  0
2022-01-01 00:45:00  0
2022-01-01 01:00:00  1
2022-01-01 01:15:00  1
2022-01-01 01:30:00  1
2022-01-01 01:45:00  1
2022-01-01 02:00:00  2
2022-01-01 02:15:00  2
2022-01-01 02:30:00  2
2022-01-01 02:45:00  2
2022-01-01 03:00:00  3
2022-01-01 03:15:00  3
2022-01-01 03:30:00  3
2022-01-01 03:45:00  3
2022-01-01 04:00:00  4

I would like the last entry to also occur 3 more times. Currently I handle this by adding an extra entry manually, resample and then drop the last value, but that seems cumbersome. I hope there is a more elegant way.


Solution

  • As @mozway mentioned, this is just the way resampling works in pandas. Alternatively, you can manually do the upsampling with a join.

    df = pd.DataFrame(
        index=pd.date_range(start='1/1/2022',periods=5,freq='1h'),
        data=range(0,5)
    )
    pd.DataFrame(
        index=pd.date_range(start='1/1/2022',periods=5*4,freq='15min')
    ).join(df).ffill()