pandas.api.typing.Resampler.interpolate — pandas 3.0.0rc0+33.g1fd184de2a documentation (original) (raw)

final Resampler.interpolate(method='linear', *, axis=0, limit=None, limit_direction='forward', limit_area=None, **kwargs)[source]#

Interpolate values between target timestamps according to different methods.

The original index is first reindexed to target timestamps (see core.resample.Resampler.asfreq()), then the interpolation of NaN values via DataFrame.interpolate()happens.

Parameters:

methodstr, default ‘linear’

Interpolation technique to use. One of:

axis{{0 or ‘index’, 1 or ‘columns’, None}}, default None

Axis to interpolate along. For Series this parameter is unused and defaults to 0.

limitint, optional

Maximum number of consecutive NaNs to fill. Must be greater than 0.

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

Consecutive NaNs will be filled in this direction.

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

If limit is specified, consecutive NaNs will be filled with this restriction.

**kwargsoptional

Keyword arguments to pass on to the interpolating function.

Returns:

DataFrame or Series

Interpolated values at the specified freq.

See also

core.resample.Resampler.asfreq

Return the values at the new freq, essentially a reindex.

DataFrame.interpolate

Fill NaN values using an interpolation method.

DataFrame.bfill

Backward fill NaN values in the resampled data.

DataFrame.ffill

Forward fill NaN values.

Notes

For high-frequent or non-equidistant time-series with timestamps the reindexing followed by interpolation may lead to information loss as shown in the last example.

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

Downsample the dataframe to 0.5Hz by providing the period time of 2s.

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

Upsample the dataframe to 2Hz by providing the period time of 500ms.

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

Internal reindexing with asfreq() prior to interpolation leads to an interpolated timeseries on the basis of the reindexed timestamps (anchors). It is assured that all available datapoints from original series become anchors, so it also works for resampling-cases that lead to non-aligned timestamps, as in the following example:

series.resample("400ms").interpolate("linear") 2023-03-01 07:00:00.000 1.000000 2023-03-01 07:00:00.400 0.333333 2023-03-01 07:00:00.800 -0.333333 2023-03-01 07:00:01.200 0.000000 2023-03-01 07:00:01.600 1.000000 2023-03-01 07:00:02.000 2.000000 2023-03-01 07:00:02.400 1.666667 2023-03-01 07:00:02.800 1.333333 2023-03-01 07:00:03.200 1.666667 2023-03-01 07:00:03.600 2.333333 2023-03-01 07:00:04.000 3.000000 Freq: 400ms, dtype: float64

Note that the series correctly decreases between two anchors07:00:00 and 07:00:02.