Filling¶
Fill forward¶
[2]:
import pandas as pd
import tstoolbox.tstoolbox as ts
[3]:
dr = pd.date_range(periods=16, start="2010-01-01", freq="H")
df = pd.DataFrame([1.1, 2.3, 2.1] + 10 * [None] + [5.4, 6.5, 7.2], dr)
[4]:
df
[4]:
0 | |
---|---|
2010-01-01 00:00:00 | 1.1 |
2010-01-01 01:00:00 | 2.3 |
2010-01-01 02:00:00 | 2.1 |
2010-01-01 03:00:00 | NaN |
2010-01-01 04:00:00 | NaN |
2010-01-01 05:00:00 | NaN |
2010-01-01 06:00:00 | NaN |
2010-01-01 07:00:00 | NaN |
2010-01-01 08:00:00 | NaN |
2010-01-01 09:00:00 | NaN |
2010-01-01 10:00:00 | NaN |
2010-01-01 11:00:00 | NaN |
2010-01-01 12:00:00 | NaN |
2010-01-01 13:00:00 | 5.4 |
2010-01-01 14:00:00 | 6.5 |
2010-01-01 15:00:00 | 7.2 |
[5]:
ts.plot(input_ts=df, ofilename="base.png", style="r-*")
[6]:
ndf = ts.fill(input_ts=df)
ndf
[6]:
0_fill | |
---|---|
Datetime | |
2010-01-01 00:00:00 | 1.1 |
2010-01-01 01:00:00 | 2.3 |
2010-01-01 02:00:00 | 2.1 |
2010-01-01 03:00:00 | 2.1 |
2010-01-01 04:00:00 | 2.1 |
2010-01-01 05:00:00 | 2.1 |
2010-01-01 06:00:00 | 2.1 |
2010-01-01 07:00:00 | 2.1 |
2010-01-01 08:00:00 | 2.1 |
2010-01-01 09:00:00 | 2.1 |
2010-01-01 10:00:00 | 2.1 |
2010-01-01 11:00:00 | 2.1 |
2010-01-01 12:00:00 | 2.1 |
2010-01-01 13:00:00 | 5.4 |
2010-01-01 14:00:00 | 6.5 |
2010-01-01 15:00:00 | 7.2 |
[ ]:
ts.plot(input_ts=ndf, ofilename="ffill.png", style="r-*")
[4]:
ndf = ts.fill(input_ts=df, method="bfill")
[ ]:
ts.plot(input_ts=ndf, ofilename="bfill.png", style="r-*")
[ ]: