pandas.core.resample.Resampler.interpolate#

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

根据不同的方法插值目标时间戳之间的值。

原始索引首先被重新索引到目标时间戳(参见 core.resample.Resampler.asfreq() ),然后通过 DataFrame.interpolate()NaN 值进行插值。

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:
DataFrame 或 Series

在指定频率下的插值。

参见

core.resample.Resampler.asfreq

返回新频率下的值,本质上是重新索引。

DataFrame.interpolate

使用插值方法填充NaN值。

Notes

对于高频或非等距的时间序列,时间戳的重新索引后插值可能会导致信息丢失,如下例所示。

Examples

>>> start = "2023-03-01T07:00:00"
>>> timesteps = pd.date_range(start, periods=5, freq="s")
>>> series = pd.Series(data=[1, -1, 2, 1, 3], index=timesteps)
>>> series
2023-03-01 07:00:00    1
2023-03-01 07:00:01   -1
2023-03-01 07:00:02    2
2023-03-01 07:00:03    1
2023-03-01 07:00:04    3
Freq: s, dtype: int64

通过提供 2 秒的周期时间将 DataFrame 上采样到 0.5Hz。

>>> series.resample("2s").interpolate("linear")
2023-03-01 07:00:00    1
2023-03-01 07:00:02    2
2023-03-01 07:00:04    3
Freq: 2s, dtype: int64

通过提供 500 毫秒的周期时间将 DataFrame 下采样到 2Hz。

>>> series.resample("500ms").interpolate("linear")
2023-03-01 07:00:00.000    1.0
2023-03-01 07:00:00.500    0.0
2023-03-01 07:00:01.000   -1.0
2023-03-01 07:00:01.500    0.5
2023-03-01 07:00:02.000    2.0
2023-03-01 07:00:02.500    1.5
2023-03-01 07:00:03.000    1.0
2023-03-01 07:00:03.500    2.0
2023-03-01 07:00:04.000    3.0
Freq: 500ms, dtype: float64

在插值之前使用 asfreq() 进行内部重新索引会导致基于重新索引的时间戳(锚点)的插值时间序列。由于并非原始序列中的所有数据点都成为锚点,这可能导致误导性的插值结果,如下例所示:

>>> series.resample("400ms").interpolate("linear")
2023-03-01 07:00:00.000    1.0
2023-03-01 07:00:00.400    1.2
2023-03-01 07:00:00.800    1.4
2023-03-01 07:00:01.200    1.6
2023-03-01 07:00:01.600    1.8
2023-03-01 07:00:02.000    2.0
2023-03-01 07:00:02.400    2.2
2023-03-01 07:00:02.800    2.4
2023-03-01 07:00:03.200    2.6
2023-03-01 07:00:03.600    2.8
2023-03-01 07:00:04.000    3.0
Freq: 400ms, dtype: float64

请注意,该序列在两个锚点 07:00:0007:00:02 之间错误地增加。