Skip to content

ENH: add quantile method to resample #22304

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 2 commits into from
Aug 22, 2018
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 @@ -2352,6 +2352,7 @@ Computations / Descriptive Stats
Resampler.std
Resampler.sum
Resampler.var
Resampler.quantile

Style
-----
Expand Down
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.24.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ Other Enhancements
- :class:`Series` and :class:`DataFrame` now support :class:`Iterable` in constructor (:issue:`2193`)
- :class:`DatetimeIndex` gained :attr:`DatetimeIndex.timetz` attribute. Returns local time with timezone information. (:issue:`21358`)
- :class:`Resampler` now is iterable like :class:`GroupBy` (:issue:`15314`).
- :ref:`Series.resample` and :ref:`DataFrame.resample` have gained the :meth:`Resampler.quantile` (:issue:`15023`).

.. _whatsnew_0240.api_breaking:

Expand Down
7 changes: 6 additions & 1 deletion pandas/core/groupby/groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -1168,7 +1168,12 @@ def var(self, ddof=1, *args, **kwargs):
"""
nv.validate_groupby_func('var', args, kwargs)
if ddof == 1:
return self._cython_agg_general('var', **kwargs)
try:
return self._cython_agg_general('var', **kwargs)
except Exception:
f = lambda x: x.var(ddof=ddof, **kwargs)
with _group_selection_context(self):
return self._python_agg_general(f)
else:
f = lambda x: x.var(ddof=ddof, **kwargs)
with _group_selection_context(self):
Expand Down
21 changes: 20 additions & 1 deletion pandas/core/resample.py
Original file line number Diff line number Diff line change
Expand Up @@ -766,6 +766,24 @@ def size(self):
result = pd.Series([], index=result.index, dtype='int64')
return result

def quantile(self, q=0.5, **kwargs):
"""
Return value at the given quantile.

Copy link
Contributor

Choose a reason for hiding this comment

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

add a versionadded tag

.. versionadded:: 0.24.0

Parameters
----------
q : float or array-like, default 0.5 (50% quantile)

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 some See Also and point to the Series / groupby versions

See Also
--------
Series.quantile
DataFrame.quantile
DataFrameGroupBy.quantile
"""
return self._downsample('quantile', q=q, **kwargs)


# downsample methods
for method in ['sum', 'prod']:
Expand Down Expand Up @@ -1060,7 +1078,8 @@ def _downsample(self, how, **kwargs):

if is_subperiod(ax.freq, self.freq):
# Downsampling
return self._groupby_and_aggregate(how, grouper=self.grouper)
return self._groupby_and_aggregate(how, grouper=self.grouper,
**kwargs)
elif is_superperiod(ax.freq, self.freq):
if how == 'ohlc':
# GH #13083
Expand Down
11 changes: 10 additions & 1 deletion pandas/tests/test_resample.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@

# The various methods we support
downsample_methods = ['min', 'max', 'first', 'last', 'sum', 'mean', 'sem',
'median', 'prod', 'var', 'ohlc']
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 the test from the OP which compares 2 methods of getting the same result

'median', 'prod', 'var', 'ohlc', 'quantile']
upsample_methods = ['count', 'size']
series_methods = ['nunique']
resample_methods = downsample_methods + upsample_methods + series_methods
Expand Down Expand Up @@ -782,6 +782,15 @@ def test_resampler_is_iterable(self):
assert rk == gk
assert_series_equal(rv, gv)

def test_resample_quantile(self):
# GH 15023
s = self.create_series()
q = 0.75
freq = 'H'
result = s.resample(freq).quantile(q)
expected = s.resample(freq).agg(lambda x: x.quantile(q))
tm.assert_series_equal(result, expected)


class TestDatetimeIndex(Base):
_index_factory = lambda x: date_range
Expand Down