【发布时间】:2019-01-29 18:57:48
【问题描述】:
我想通过前向填充结果将一个系列从每周频率上采样到每天频率。
如果我原始系列的最后一次观察是NaN,我本来希望这个值被之前的有效值替换,但它仍然是NaN。
设置
import numpy as np
import pandas as pd
all_dates = pd.date_range(start='2018-01-01', freq='W-WED', periods=4)
ts = pd.Series([1, 2, 3], index=all_dates[:3])
ts[all_dates[3]] = np.nan
ts
Out[16]:
2018-01-03 1.0
2018-01-10 2.0
2018-01-17 3.0
2018-01-24 NaN
Freq: W-WED, dtype: float64
结果
ts.resample('B').ffill()
ts.resample('B').ffill()
Out[17]:
2018-01-03 1.0
2018-01-04 1.0
2018-01-05 1.0
2018-01-08 1.0
2018-01-09 1.0
2018-01-10 2.0
2018-01-11 2.0
2018-01-12 2.0
2018-01-15 2.0
2018-01-16 2.0
2018-01-17 3.0
2018-01-18 3.0
2018-01-19 3.0
2018-01-22 3.0
2018-01-23 3.0
2018-01-24 NaN
Freq: B, dtype: float64
虽然我预计最后一个值也是 3。
有人对这种行为有解释吗?
【问题讨论】:
-
你不会在这里替换
nulls。为此使用df['B'].fillna(ffill) -
你的建议给出了完全相同的结果:pandas.pydata.org/pandas-docs/stable/reference/api/…
-
@FLab
2018-01-24是NaN所以试试ts.ffill().resample('B').ffill()
标签: python pandas missing-data resampling