Skip to content

added nearest to resample + test #17498

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 17, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2025,6 +2025,7 @@ Upsampling
Resampler.backfill
Resampler.bfill
Resampler.pad
Resampler.nearest
Resampler.fillna
Resampler.asfreq
Resampler.interpolate
Expand Down
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.21.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ New features
and :class:`~pandas.ExcelWriter` to work properly with the file system path protocol (:issue:`13823`)
- Added ``skipna`` parameter to :func:`~pandas.api.types.infer_dtype` to
support type inference in the presence of missing values (:issue:`17059`).
- :class:`~pandas.Resampler.nearest` is added to support nearest-neighbor upsampling (:issue:`17496`).

.. _whatsnew_0210.enhancements.infer_objects:

Expand Down
30 changes: 30 additions & 0 deletions pandas/core/resample.py
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,10 @@ def pad(self, limit=None):
limit : integer, optional
limit of how many values to fill

Returns
-------
an upsampled Series

See Also
--------
Series.fillna
Expand All @@ -463,6 +467,28 @@ def pad(self, limit=None):
return self._upsample('pad', limit=limit)
ffill = pad

def nearest(self, limit=None):
"""
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

versionadded tag

Fill values with nearest neighbor starting from center

Parameters
----------
limit : integer, optional
limit of how many values to fill

.. versionadded:: 0.21.0

Returns
-------
an upsampled Series

See Also
--------
Series.fillna
DataFrame.fillna
"""
return self._upsample('nearest', limit=limit)

def backfill(self, limit=None):
"""
Backward fill the values
Expand All @@ -472,6 +498,10 @@ def backfill(self, limit=None):
limit : integer, optional
limit of how many values to fill

Returns
-------
an upsampled Series

See Also
--------
Series.fillna
Expand Down
28 changes: 27 additions & 1 deletion pandas/tests/test_resample.py
Original file line number Diff line number Diff line change
Expand Up @@ -1329,6 +1329,14 @@ def test_upsample_with_limit(self):
expected = ts.reindex(result.index, method='ffill', limit=2)
assert_series_equal(result, expected)

def test_nearest_upsample_with_limit(self):
rng = date_range('1/1/2000', periods=3, freq='5t')
ts = Series(np.random.randn(len(rng)), rng)

result = ts.resample('t').nearest(limit=2)
expected = ts.reindex(result.index, method='nearest', limit=2)
assert_series_equal(result, expected)

def test_resample_ohlc(self):
s = self.series

Expand Down Expand Up @@ -2934,6 +2942,24 @@ def test_getitem_multiple(self):
result = r['buyer'].count()
assert_series_equal(result, expected)

def test_nearest(self):

# GH 17496
# Resample nearest
index = pd.date_range('1/1/2000', periods=3, freq='T')
result = pd.Series(range(3), index=index).resample('20s').nearest()

expected = pd.Series(
np.array([0, 0, 1, 1, 1, 2, 2]),
index=pd.DatetimeIndex(
['2000-01-01 00:00:00', '2000-01-01 00:00:20',
'2000-01-01 00:00:40', '2000-01-01 00:01:00',
'2000-01-01 00:01:20', '2000-01-01 00:01:40',
'2000-01-01 00:02:00'],
dtype='datetime64[ns]',
freq='20S'))
assert_series_equal(result, expected)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you add a test with limit as well


def test_methods(self):
g = self.frame.groupby('A')
r = g.resample('2s')
Expand All @@ -2960,7 +2986,7 @@ def test_methods(self):
expected = g.B.apply(lambda x: getattr(x.resample('2s'), f)())
assert_series_equal(result, expected)

for f in ['backfill', 'ffill', 'asfreq']:
for f in ['nearest', 'backfill', 'ffill', 'asfreq']:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's add a test for this specifically to make sure its doing the right thing. IOW construct a frame with an expected result (rather than the auto generated one here which might or might now actually do nearest resampling, as it doesn't have nans).

result = getattr(r, f)()
expected = g.apply(lambda x: getattr(x.resample('2s'), f)())
assert_frame_equal(result, expected)
Expand Down