用数字索引重采样熊猫系列

Joh*_*ohn 6 python pandas

假设我有一个带有数值类型索引的 pandas.Series 例如

pd.Series( [10,20], [1.1, 2.3] )
Run Code Online (Sandbox Code Playgroud)

我们如何以 0.1 的间隔重新采样以上系列?看起来像 .resample func 只适用于日期时间间隔?

Tom*_*ger 5

这就是插值的名称。您可以将重采样视为插值的一种特殊情况。

In [24]: new_idx = s.index + pd.Index(np.arange(1.1, 2.3, .01))

In [25]: s.reindex(new_idx).interpolate().head()
Out[25]: 
1.10    10.000000
1.11    10.083333
1.12    10.166667
1.13    10.250000
1.14    10.333333
dtype: float64

In [26]: s.reindex(new_idx).interpolate().tail()
Out[26]: 
2.26    19.666667
2.27    19.750000
2.28    19.833333
2.29    19.916667
2.30    20.000000
dtype: float64
Run Code Online (Sandbox Code Playgroud)

我们需要new_idx是原始索引和我们想要插入的值的联合,这样原始索引不会被删除。

看看插值方法:http : //pandas.pydata.org/pandas-docs/stable/generated/pandas.Series.interpolate.html