Skip to content

Commit f6e6933

Browse files
committed
added support for nearest upsample + comments on returns for upsampled functions
1 parent f11bbf2 commit f6e6933

File tree

3 files changed

+46
-1
lines changed

3 files changed

+46
-1
lines changed

doc/source/whatsnew/v0.21.0.txt

+2
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ New features
2828
and :class:`~pandas.ExcelWriter` to work properly with the file system path protocol (:issue:`13823`)
2929
- Added ``skipna`` parameter to :func:`~pandas.api.types.infer_dtype` to
3030
support type inference in the presence of missing values (:issue:`17059`).
31+
- Added ``nearest`` method to :class:`~pandas.Resampler` to support
32+
nearest-neighbor upsampling (:issue:`17496`).
3133

3234
.. _whatsnew_0210.enhancements.infer_objects:
3335

pandas/core/resample.py

+28
Original file line numberDiff line numberDiff line change
@@ -455,6 +455,10 @@ def pad(self, limit=None):
455455
limit : integer, optional
456456
limit of how many values to fill
457457
458+
Returns
459+
-------
460+
an upsampled Series
461+
458462
See Also
459463
--------
460464
Series.fillna
@@ -463,6 +467,26 @@ def pad(self, limit=None):
463467
return self._upsample('pad', limit=limit)
464468
ffill = pad
465469

470+
def nearest(self, limit=None):
471+
"""
472+
Fill values with nearest neighbor starting from center
473+
474+
Parameters
475+
----------
476+
limit : integer, optional
477+
limit of how many values to fill
478+
479+
Returns
480+
-------
481+
an upsampled Series
482+
483+
See Also
484+
--------
485+
Series.fillna
486+
DataFrame.fillna
487+
"""
488+
return self._upsample('nearest', limit=limit)
489+
466490
def backfill(self, limit=None):
467491
"""
468492
Backward fill the values
@@ -472,6 +496,10 @@ def backfill(self, limit=None):
472496
limit : integer, optional
473497
limit of how many values to fill
474498
499+
Returns
500+
-------
501+
an upsampled Series
502+
475503
See Also
476504
--------
477505
Series.fillna

pandas/tests/test_resample.py

+16-1
Original file line numberDiff line numberDiff line change
@@ -2934,6 +2934,21 @@ def test_getitem_multiple(self):
29342934
result = r['buyer'].count()
29352935
assert_series_equal(result, expected)
29362936

2937+
def test_nearest(self):
2938+
index = pd.date_range('1/1/2000', periods=3, freq='T')
2939+
result = pd.Series(range(3), index=index).resample('20s').nearest()
2940+
2941+
expected = pd.Series(
2942+
np.array([0, 0, 1, 1, 1, 2, 2]),
2943+
index=pd.DatetimeIndex(
2944+
['2000-01-01 00:00:00', '2000-01-01 00:00:20',
2945+
'2000-01-01 00:00:40', '2000-01-01 00:01:00',
2946+
'2000-01-01 00:01:20', '2000-01-01 00:01:40',
2947+
'2000-01-01 00:02:00'],
2948+
dtype='datetime64[ns]',
2949+
freq='20S'))
2950+
assert_series_equal(result, expected)
2951+
29372952
def test_methods(self):
29382953
g = self.frame.groupby('A')
29392954
r = g.resample('2s')
@@ -2960,7 +2975,7 @@ def test_methods(self):
29602975
expected = g.B.apply(lambda x: getattr(x.resample('2s'), f)())
29612976
assert_series_equal(result, expected)
29622977

2963-
for f in ['backfill', 'ffill', 'asfreq']:
2978+
for f in ['nearest', 'backfill', 'ffill', 'asfreq']:
29642979
result = getattr(r, f)()
29652980
expected = g.apply(lambda x: getattr(x.resample('2s'), f)())
29662981
assert_frame_equal(result, expected)

0 commit comments

Comments
 (0)