用数字索引重采样 pandas 系列 [英] resampling pandas series with numeric index

查看:72
本文介绍了用数字索引重采样 pandas 系列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

suppose I have a pandas.Series with index with numeric value type e.g.

pd.Series( [10,20], [1.1, 2.3] )

我们如何以0.1间隔对上述系列进行重新采样?看起来.resample函数只能在日期时间间隔上工作?

How do we resample above series with 0.1 interval? look like the .resample func only work on datetime interval?

推荐答案

这就是插值的名称.您可以考虑将重采样作为插值的一种特殊情况.

That goes by the name of interpolation. You can think for resampling as a special case of interpolation.

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

我们需要new_idx是原始索引和我们要插值的值的并集,以便不删除原始索引.

We need new_idx to be a union of the original index and the values we want to interpolate, so that the original index isn't dropped.

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

这篇关于用数字索引重采样 pandas 系列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆