Skip to content

Commit 07cc86a

Browse files
thicorfonPingviinituutti
authored andcommitted
DOC: update pandas.core.resample.Resampler.nearest docstring (pandas-dev#20381)
1 parent 92d22b9 commit 07cc86a

File tree

1 file changed

+46
-6
lines changed

1 file changed

+46
-6
lines changed

pandas/core/resample.py

+46-6
Original file line numberDiff line numberDiff line change
@@ -418,23 +418,63 @@ def pad(self, limit=None):
418418

419419
def nearest(self, limit=None):
420420
"""
421-
Fill values with nearest neighbor starting from center
421+
Resample by using the nearest value.
422+
423+
When resampling data, missing values may appear (e.g., when the
424+
resampling frequency is higher than the original frequency).
425+
The `nearest` method will replace ``NaN`` values that appeared in
426+
the resampled data with the value from the nearest member of the
427+
sequence, based on the index value.
428+
Missing values that existed in the original data will not be modified.
429+
If `limit` is given, fill only this many values in each direction for
430+
each of the original values.
422431
423432
Parameters
424433
----------
425-
limit : integer, optional
426-
limit of how many values to fill
434+
limit : int, optional
435+
Limit of how many values to fill.
427436
428437
.. versionadded:: 0.21.0
429438
430439
Returns
431440
-------
432-
an upsampled Series
441+
Series or DataFrame
442+
An upsampled Series or DataFrame with ``NaN`` values filled with
443+
their nearest value.
433444
434445
See Also
435446
--------
436-
Series.fillna
437-
DataFrame.fillna
447+
backfill : Backward fill the new missing values in the resampled data.
448+
pad : Forward fill ``NaN`` values.
449+
450+
Examples
451+
--------
452+
>>> s = pd.Series([1, 2],
453+
... index=pd.date_range('20180101',
454+
... periods=2,
455+
... freq='1h'))
456+
>>> s
457+
2018-01-01 00:00:00 1
458+
2018-01-01 01:00:00 2
459+
Freq: H, dtype: int64
460+
461+
>>> s.resample('15min').nearest()
462+
2018-01-01 00:00:00 1
463+
2018-01-01 00:15:00 1
464+
2018-01-01 00:30:00 2
465+
2018-01-01 00:45:00 2
466+
2018-01-01 01:00:00 2
467+
Freq: 15T, dtype: int64
468+
469+
Limit the number of upsampled values imputed by the nearest:
470+
471+
>>> s.resample('15min').nearest(limit=1)
472+
2018-01-01 00:00:00 1.0
473+
2018-01-01 00:15:00 1.0
474+
2018-01-01 00:30:00 NaN
475+
2018-01-01 00:45:00 2.0
476+
2018-01-01 01:00:00 2.0
477+
Freq: 15T, dtype: float64
438478
"""
439479
return self._upsample('nearest', limit=limit)
440480

0 commit comments

Comments
 (0)