pandas.Series.interpolate#

Series.interpolate(method='linear', *, axis=0, limit=None, inplace=False, limit_direction=None, limit_area=None, downcast=_NoDefault.no_default, **kwargs)[源代码]#

使用插值方法填充NaN值。

请注意,对于具有 MultiIndex 的 DataFrame/Series,只支持 method='linear'

Parameters:
<strong>method</strong>str, default ‘linear’

插值技术。可选值:

  • ‘linear’:忽略索引,将值视为等间距。这是 MultiIndexes 上唯一支持的方法。

  • ‘time’:适用于每日及更高分辨率的数据,用于在给定间隔长度内进行插值。

  • ‘index’, ‘values’:使用索引的实际数值。

  • ‘pad’:使用现有值填充 NaN。

  • ‘nearest’, ‘zero’, ‘slinear’, ‘quadratic’, ‘cubic’, ‘barycentric’, ‘polynomial’:传递给 scipy.interpolate.interp1d,而 ‘spline’ 传递给 scipy.interpolate.UnivariateSpline。这些方法使用索引的数值。 ‘polynomial’ 和 ‘spline’ 方法都需要指定一个 order`(int),例如 ``df.interpolate(method=’polynomial’, order=5)`。请注意,Pandas 中的 slinear 方法指的是 Scipy 的一阶 spline,而不是 Pandas 的一阶 spline

  • ‘krogh’, ‘piecewise_polynomial’, ‘spline’, ‘pchip’, ‘akima’, ‘cubicspline’:围绕同名的 SciPy 插值方法进行的包装。参见“注释”。

  • ‘from_derivatives’:指 scipy.interpolate.BPoly.from_derivatives

axis{{0 或 ‘index’,1 或 ‘columns’,None}},默认 None

沿哪个轴进行插值。对于 Series,此参数未使用,默认为 0。

<strong>limit</strong>int, optional

要填充的连续 NaN 的最大数量。必须大于 0。

inplacebool,默认 False

如果可能,就地更新数据。

limit_direction{{‘forward’, ‘backward’, ‘both’}}, Optional

沿此方向填充连续 NaN。

如果指定了 limit:
  • 如果 ‘method’ 是 ‘pad’ 或 ‘ffill’,则 ‘limit_direction’ 必须是 ‘forward’。

  • 如果 ‘method’ 是 ‘backfill’ 或 ‘bfill’,则 ‘limit_direction’ 必须是 ‘backwards’。

如果未指定 ‘limit’:
  • 如果 ‘method’ 是 ‘backfill’ 或 ‘bfill’,则默认为 ‘backward’。

  • 否则默认为 ‘forward’。

如果 limit_direction 为 ‘forward’ 或 ‘both’ 且

method 为 ‘backfill’ 或 ‘bfill’,则引发 ValueError

如果 limit_direction 为 ‘backward’ 或 ‘both’ 且

method 为 ‘pad’ 或 ‘ffill’,则引发 ValueError

limit_area{{None, ‘inside’, ‘outside’}}, default None

如果指定了 limit,连续的 NaNs 将根据此限制进行填充。

  • None:无填充限制。

  • ‘inside’:仅填充被有效值包围的 NaN(插值)。

  • ‘outside’:仅填充有效值之外的 NaN(外插)。

downcastoptional, ‘infer’ or None, defaults to None

如果可能,向下转换数据类型。

自 2.1.0 版本弃用.

``**kwargs``optional

要传递给插值函数的关键字参数。

Returns:
Series 或 DataFrame 或 None

返回与调用者相同的对象类型,在部分或全部 NaN 值处进行插值,如果 inplace=True 则返回 None。

参见

fillna

使用不同方法填充缺失值。

scipy.interpolate.Akima1DInterpolator

分段三次多项式(Akima 插值器)。

scipy.interpolate.BPoly.from_derivatives

伯恩斯坦基中的分段多项式。

scipy.interpolate.interp1d

插值一维函数。

scipy.interpolate.KroghInterpolator

插值多项式(Krogh 插值器)。

scipy.interpolate.PchipInterpolator

PCHIP 一维单调三次插值。

scipy.interpolate.CubicSpline

三次样条数据插值器。

Notes

‘krogh’、’piecewise_polynomial’、’spline’、’pchip’ 和 ‘akima’ 方法是对相应 SciPy 实现的同名方法的包装。这些方法使用索引的实际数值。有关其行为的更多信息,请参阅 SciPy documentation

Examples

通过线性插值填充 Series 中的 NaN

>>> s = pd.Series([0, 1, np.nan, 3])
>>> s
0    0.0
1    1.0
2    NaN
3    3.0
dtype: float64
>>> s.interpolate()
0    0.0
1    1.0
2    2.0
3    3.0
dtype: float64

通过多项式插值或样条填充 Series 中的 NaN:’polynomial’ 和 ‘spline’ 方法都需要指定一个 ``order``(int)。

>>> s = pd.Series([0, 2, np.nan, 8])
>>> s.interpolate(method='polynomial', order=2)
0    0.000000
1    2.000000
2    4.666667
3    8.000000
dtype: float64

通过线性插值沿每列向下填充 DataFrame。

注意最后一列“a”的条目是如何以不同的方式插值的,因为它后面没有条目可用于插值。注意第一列“b”的条目仍然是“NaN”,因为它前面没有条目可用于插值。

>>> df = pd.DataFrame([(0.0, np.nan, -1.0, 1.0),
...                    (np.nan, 2.0, np.nan, np.nan),
...                    (2.0, 3.0, np.nan, 9.0),
...                    (np.nan, 4.0, -4.0, 16.0)],
...                   columns=list('abcd'))
>>> df
     a    b    c     d
0  0.0  NaN -1.0   1.0
1  NaN  2.0  NaN   NaN
2  2.0  3.0  NaN   9.0
3  NaN  4.0 -4.0  16.0
>>> df.interpolate(method='linear', limit_direction='forward', axis=0)
     a    b    c     d
0  0.0  NaN -1.0   1.0
1  1.0  2.0 -2.0   5.0
2  2.0  3.0 -3.0   9.0
3  2.0  4.0 -4.0  16.0

使用多项式插值。

>>> df['d'].interpolate(method='polynomial', order=2)
0     1.0
1     4.0
2     9.0
3    16.0
Name: d, dtype: float64