Skip to content

ENH: Add numeric_only to resampler methods #46792

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 7 commits into from
Apr 23, 2022
Merged
Show file tree
Hide file tree
Changes from 6 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/whatsnew/v1.5.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ Other enhancements
- :meth:`pd.concat` now raises when ``levels`` contains duplicate values (:issue:`46653`)
- Added ``numeric_only`` argument to :meth:`DataFrame.corr`, :meth:`DataFrame.corrwith`, and :meth:`DataFrame.cov` (:issue:`46560`)
- A :class:`errors.PerformanceWarning` is now thrown when using ``string[pyarrow]`` dtype with methods that don't dispatch to ``pyarrow.compute`` methods (:issue:`42613`)
- Added ``numeric_only`` argument to :meth:`Resampler.sum`, :meth:`Resampler.prod`, :meth:`Resampler.min`, :meth:`Resampler.max`, :meth:`Resampler.first`, and :meth:`Resampler.last` (:issue:`46442`)

.. ---------------------------------------------------------------------------
.. _whatsnew_150.notable_bug_fixes:
Expand Down
17 changes: 15 additions & 2 deletions pandas/core/resample.py
Original file line number Diff line number Diff line change
Expand Up @@ -1027,9 +1027,22 @@ def quantile(self, q=0.5, **kwargs):
# downsample methods
for method in ["sum", "prod", "min", "max", "first", "last"]:

def f(self, _method=method, min_count=0, *args, **kwargs):
def f(
self,
_method: str = method,
numeric_only: bool | lib.NoDefault = lib.no_default,
min_count: int = 0,
*args,
**kwargs,
):
if numeric_only is lib.no_default:
if (self.obj.ndim == 1) or (_method != "sum"):
# For SeriesGroupBy, set the default to be False.
# For DataFrameGroupBy, set it to be False for methods other than `sum`.
numeric_only = False

nv.validate_resampler_func(_method, args, kwargs)
return self._downsample(_method, min_count=min_count)
return self._downsample(_method, numeric_only=numeric_only, min_count=min_count)

f.__doc__ = getattr(GroupBy, method).__doc__
setattr(Resampler, method, f)
Expand Down
55 changes: 55 additions & 0 deletions pandas/tests/resample/test_resample_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -771,3 +771,58 @@ def test_end_and_end_day_origin(
)

tm.assert_series_equal(res, expected)


@pytest.mark.parametrize("numeric_only", [True, False])
def test_downsample_method(numeric_only):
# test if `numeric_only` behave as expected for Resampler downsample methods.

index = date_range("2018-01-01", periods=2, freq="D")
expected_index = date_range("2018-12-31", periods=1, freq="Y")
df = DataFrame({"cat": ["cat_1", "cat_2"], "num": [5, 20]}, index=index)
resampled = df.resample("Y")

# test Resampler.sum
result = resampled.sum(numeric_only=numeric_only)
if numeric_only:
expected = DataFrame({"num": [25]}, index=expected_index)
else:
expected = DataFrame({"cat": ["cat_1cat_2"], "num": [25]}, index=expected_index)
tm.assert_frame_equal(result, expected)

# test Resampler.prod
result = resampled.prod(numeric_only=numeric_only)
expected = DataFrame({"num": [100]}, index=expected_index)
tm.assert_frame_equal(result, expected)

# test Resampler.min
result = resampled.min(numeric_only=numeric_only)
if numeric_only:
expected = DataFrame({"num": [5]}, index=expected_index)
else:
expected = DataFrame({"cat": ["cat_1"], "num": [5]}, index=expected_index)
tm.assert_frame_equal(result, expected)

# test Resampler.max
result = resampled.max(numeric_only=numeric_only)
if numeric_only:
expected = DataFrame({"num": [20]}, index=expected_index)
else:
expected = DataFrame({"cat": ["cat_2"], "num": [20]}, index=expected_index)
tm.assert_frame_equal(result, expected)

# test Resampler.first
result = resampled.first(numeric_only=numeric_only)
if numeric_only:
expected = DataFrame({"num": [5]}, index=expected_index)
else:
expected = DataFrame({"cat": ["cat_1"], "num": [5]}, index=expected_index)
tm.assert_frame_equal(result, expected)

# test Resampler.last
result = resampled.last(numeric_only=numeric_only)
if numeric_only:
expected = DataFrame({"num": [20]}, index=expected_index)
else:
expected = DataFrame({"cat": ["cat_2"], "num": [20]}, index=expected_index)
tm.assert_frame_equal(result, expected)