Index.union
,
reindex
interpolate
MCVE公司
创建玩具数据。每隔一天三排。
tidx = pd.date_range('2018-01-01', periods=3, freq='2D')
df = pd.DataFrame(dict(A=[1, 3, 5]), tidx)
df
A
2018-01-01 1
2018-01-03 3
2018-01-05 5
这两天的新指数
other_tidx = pd.date_range(tidx.min(), tidx.max()).difference(tidx)
创建一个新索引,它是旧索引和新索引的联合
union_idx = other_tidx.union(df.index)
当我们
我们得到了这个
df.reindex(union_idx)
A
2018-01-01 1.0
2018-01-02 NaN
2018-01-03 3.0
2018-01-04 NaN
2018-01-05 5.0
我们看到了预期的差距。现在我们可以用
插入
method='index'
以确保我们根据索引中的间隙大小进行插值。
df.reindex(union_idx).interpolate('index')
A
2018-01-01 1.0
2018-01-02 2.0
2018-01-03 3.0
2018-01-04 4.0
2018-01-05 5.0
现在这些空白被填补了。
df.reindex(union_idx).interpolate('index').reindex(other_tidx)
A
2018-01-02 2.0
2018-01-04 4.0