Skip to content

ENH: add .resample(..).interpolate() #12925 #12974

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

Closed
wants to merge 1 commit into from
Closed
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
4 changes: 4 additions & 0 deletions doc/source/whatsnew/v0.18.1.txt
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,12 @@ Other Enhancements
idx = pd.Index(['a|b', 'a|c', 'b|c'])
idx.str.get_dummies('|')


- ``pd.crosstab()`` has gained a ``normalize`` argument for normalizing frequency tables (:issue:`12569`). Examples in the updated docs :ref:`here <reshaping.crosstabulations>`.

- ``.resample(..).interpolate()`` is now supported (:issue:`12925`)



.. _whatsnew_0181.sparse:

Expand Down
12 changes: 9 additions & 3 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -3445,9 +3445,7 @@ def replace(self, to_replace=None, value=None, inplace=False, limit=None,
else:
return self._constructor(new_data).__finalize__(self)

def interpolate(self, method='linear', axis=0, limit=None, inplace=False,
limit_direction='forward', downcast=None, **kwargs):
"""
_shared_docs['interpolate'] = """
Interpolate values according to different methods.

Please note that only ``method='linear'`` is supported for
Expand Down Expand Up @@ -3521,6 +3519,14 @@ def interpolate(self, method='linear', axis=0, limit=None, inplace=False,
dtype: float64

"""

@Appender(_shared_docs['interpolate'] % _shared_doc_kwargs)
def interpolate(self, method='linear', axis=0, limit=None, inplace=False,
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 versionadded tag (for 0.18.1)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Where exactly? In the Resampler class documentation or in the interpolate method?

Copy link
Contributor

Choose a reason for hiding this comment

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

right here

limit_direction='forward', downcast=None, **kwargs):
"""
.. versionadded:: 0.18.1
``.resample(..).interpolate()`` is now supported (:issue:`12925`)
Copy link
Member

Choose a reason for hiding this comment

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

Putting this here in the beginning of the docstring does not really work well I think. Because the rest (the shared_docs['interpolate']) are appended, the explanation starts with 'versionadded', while it should rather be "Interpolate values according to different methods" (although, as long as it is not listed in an API overview, it is not really that big of a problem. But, it should actually be added here:http://pandas-docs.github.io/pandas-docs-travis/api.html#upsampling)

"""
if self.ndim > 2:
raise NotImplementedError("Interpolate has not been implemented "
"on Panel and Panel 4D objects.")
Expand Down
13 changes: 13 additions & 0 deletions pandas/tseries/resample.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@
import pandas.lib as lib
import pandas.tslib as tslib

from pandas.util.decorators import Appender
from pandas.core.generic import _shared_docs
_shared_docs_kwargs = dict()


class Resampler(_GroupBy):

Expand Down Expand Up @@ -448,6 +452,15 @@ def fillna(self, method, limit=None):
"""
return self._upsample(method, limit=limit)

@Appender(_shared_docs['interpolate'] % _shared_docs_kwargs)
def interpolate(self, method='linear', axis=0, limit=None, inplace=False,
limit_direction='forward', downcast=None, **kwargs):
result = self._upsample(None)
return result.interpolate(method=method, axis=axis, limit=limit,
inplace=inplace,
limit_direction=limit_direction,
downcast=downcast, **kwargs)

def asfreq(self):
"""
return the values at the new freq,
Expand Down
7 changes: 7 additions & 0 deletions pandas/tseries/tests/test_resample.py
Original file line number Diff line number Diff line change
Expand Up @@ -620,6 +620,13 @@ def test_asfreq_upsample(self):
expected = frame.reindex(new_index)
assert_frame_equal(result, expected)

def test_resample_interpolate(self):
# # 12925
df = self.create_series().to_frame('value')
assert_frame_equal(
df.resample('1T').asfreq().interpolate(),
df.resample('1T').interpolate())


class TestDatetimeIndex(Base, tm.TestCase):
_multiprocess_can_split_ = True
Expand Down