- API reference
- Resampling
- pandas.core....
pandas.core.resample.Resampler.nearest#
- finalResampler.nearest(limit=None)[source]#
Resample by using the nearest value.
When resampling data, missing values may appear (e.g., when theresampling frequency is higher than the original frequency).Thenearest method will replace
NaNvalues that appeared inthe resampled data with the value from the nearest member of thesequence, based on the index value.Missing values that existed in the original data will not be modified.Iflimit is given, fill only this many values in each direction foreach of the original values.- Parameters:
- limitint, optional
Limit of how many values to fill.
- Returns:
- Series or DataFrame
An upsampled Series or DataFrame with
NaNvalues filled withtheir nearest value.
See also
backfillBackward fill the new missing values in the resampled data.
padForward fill
NaNvalues.
Examples
>>>s=pd.Series([1,2],...index=pd.date_range('20180101',...periods=2,...freq='1h'))>>>s2018-01-01 00:00:00 12018-01-01 01:00:00 2Freq: h, dtype: int64
>>>s.resample('15min').nearest()2018-01-01 00:00:00 12018-01-01 00:15:00 12018-01-01 00:30:00 22018-01-01 00:45:00 22018-01-01 01:00:00 2Freq: 15min, dtype: int64
Limit the number of upsampled values imputed by the nearest:
>>>s.resample('15min').nearest(limit=1)2018-01-01 00:00:00 1.02018-01-01 00:15:00 1.02018-01-01 00:30:00 NaN2018-01-01 00:45:00 2.02018-01-01 01:00:00 2.0Freq: 15min, dtype: float64