pandas.Series.interpolate#

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

使用插值法填充NaN值。

请注意,只有 method='linear' 支持具有多索引的DataFrame/Series。

参数
method字符串,默认‘线性’

要使用的插补技术。以下选项之一:

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

  • “Time”:处理每日和更高分辨率的数据,以插入给定间隔长度的数据。

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

  • ‘Pad’:使用现有值填充NAN。

  • “最接近”、“零”、“线性”、“二次”、“三次”、“样条线”、“重心”、“多项式”:传递到 scipy.interpolate.interp1d 。这些方法使用的是指数的数值。“多项式”和“样条线”都要求您还指定 order (Int),例如 df.interpolate(method='polynomial', order=5)

  • ‘krogh’,‘Pieewise_Poliplomy’,‘Spline’,‘pChip’,‘Akima’,‘CucuicSpline’:类似名称的SciPy插值法的包装器。看见 Notes

  • ‘From_Deducts’:指 scipy.interpolate.BPoly.from_derivatives 它取代了Scipy 0.18中的分段多项式插值法。

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

要沿其插补的轴。

limit整型,可选

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

inplace布尔值,默认为False

如有可能,请就地更新数据。

limit_direction{{‘向前’,‘向后’,‘两者都’}},可选

将在此方向填充连续的NAN。

如果指定了限制:
  • 如果“方法”为“Pad”或“ffill”,则“Limit_Direction”必须为“Forward”。“

  • 如果“方法”为“Backill”或“bill”,则“Limit_Direction”必须为“Backback”。“”

如果未指定‘Limit’:
  • 如果‘方法’是‘BACKFILL’或‘bFILL’,则默认为‘BACKFILL’

  • 否则缺省值为‘Forward’

在 1.1.0 版更改: 在以下情况下引发ValueError limit_direction 为‘Forward’或‘Both’,方法为‘BackFill’或‘bill’。在以下情况下引发ValueError limit_direction 为‘Backup’或‘Both’,方法为‘Pad’或‘ffill’。

limit_area{{None, 'inside', 'outside'}}, default None

如果指定了限制,则将使用此限制填充连续的NAN。

  • None :无填充限制。

  • ‘Inside’:仅填充由有效值包围的NAN(内插)。

  • ‘OUTHER’:仅填充有效值之外的NAN(外推)。

downcast可选的‘INFER’或NONE,缺省为NONE

如果可能,向下转换dtype。

`` Kwargs``**可选

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

退货
系列、DataFrame或无

返回与调用方相同的对象类型,部分或全部内插 NaN 值或无,如果 inplace=True

参见

fillna

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

scipy.interpolate.Akima1DInterpolator

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

scipy.interpolate.BPoly.from_derivatives

Bernstein基中的分段多项式。

scipy.interpolate.interp1d

对一维函数进行插值。

scipy.interpolate.KroghInterpolator

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

scipy.interpolate.PchipInterpolator

PCHIP一维单调三次插值。

scipy.interpolate.CubicSpline

三次样条数据插值器。

注意事项

‘Krogh’、‘Pecewise_Poliplomy’、‘Spline’、‘pChip’和‘Akima’方法是名称相似的各自的SciPy实现的包装器。这些值使用索引的实际数值。有关其行为的更多信息,请参见 SciPy documentation

示例

填写 NaN 在一个 Series 通过线性插值法。

>>> 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

填写 NaN 在一个序列中通过填充,但最多连续填充两个 NaN 一次一次。

>>> s = pd.Series([np.nan, "single_one", np.nan,
...                "fill_two_more", np.nan, np.nan, np.nan,
...                4.71, np.nan])
>>> s
0              NaN
1       single_one
2              NaN
3    fill_two_more
4              NaN
5              NaN
6              NaN
7             4.71
8              NaN
dtype: object
>>> s.interpolate(method='pad', limit=2)
0              NaN
1       single_one
2       single_one
3    fill_two_more
4    fill_two_more
5    fill_two_more
6              NaN
7             4.71
8             4.71
dtype: object

填写 NaN 在级数中通过多项式插值或样条线:“多项式”和“样条线”方法都要求您还指定一个 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