@@ -418,23 +418,63 @@ def pad(self, limit=None):
418
418
419
419
def nearest (self , limit = None ):
420
420
"""
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.
422
431
423
432
Parameters
424
433
----------
425
- limit : integer , optional
426
- limit of how many values to fill
434
+ limit : int , optional
435
+ Limit of how many values to fill.
427
436
428
437
.. versionadded:: 0.21.0
429
438
430
439
Returns
431
440
-------
432
- an upsampled Series
441
+ Series or DataFrame
442
+ An upsampled Series or DataFrame with ``NaN`` values filled with
443
+ their nearest value.
433
444
434
445
See Also
435
446
--------
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
438
478
"""
439
479
return self ._upsample ('nearest' , limit = limit )
440
480
0 commit comments