1

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.

Python404
  • 13
  • 3
  • 1
    Unfortunately, the bounds are unchanged, so you must modify the end value before resampling. – mozway Sep 15 '22 at 12:12

1 Answers1

0

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()
kjanker
  • 71
  • 3
  • Thankyou (also @mozway), I'll stick with modifying the end value then. Good to know there is no build in option. – Python404 Sep 19 '22 at 11:21