8000 DEPR: numeric_only default in resampler ops by rhshadrach · Pull Request #47177 · pandas-dev/pandas · GitHub
[go: up one dir, main page]

Skip to content

DEPR: numeric_only default in resampler ops #47177

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 3 commits into from
Jun 5, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Revert removal of args/kwargs
  • Loading branch information
rhshadrach committed Jun 4, 2022
commit 73bac86da7b17c83446abe8021ac22a9ea9b5081
18 changes: 18 additions & 0 deletions pandas/compat/numpy/function.py
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,24 @@ def validate_groupby_func(name, args, kwargs, allowed=None) -> None:
)


RESAMPLER_NUMPY_OPS = ("min", "max", "sum", "prod", "mean", "std", "var")


def validate_resampler_func(method: str, args, kwargs) -> None:
"""
'args' and 'kwargs' should be empty because all of their necessary
parameters are explicitly listed in the function signature
"""
if len(args) + len(kwargs) > 0:
if method in RESAMPLER_NUMPY_OPS:
raise UnsupportedFunctionCall(
"numpy operations are not valid with resample. "
f"Use .resample(...).{method}() instead"
)
else:
raise TypeError("too many arguments passed in")


def validate_minmax_axis(axis: int | None, ndim: int = 1) -> None:
"""
Ensure that the axis argument passed to min, max, argmin, or argmax is zero
Expand Down
19 changes: 16 additions & 3 deletions pandas/core/resample.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
TimestampConvertibleTypes,
npt,
)
from pandas.compat.numpy import function as nv
from pandas.errors import (
AbstractMethodError,
DataError,
Expand Down Expand Up @@ -936,7 +937,7 @@ def asfreq(self, fill_value=None):
"""
return self._upsample("asfreq", fill_value=fill_value)

def std(self, ddof=1, numeric_only: bool = False):
def std(self, ddof=1, numeric_only: bool = False, *args, **kwargs):
"""
Compute standard deviation of groups, excluding missing values.

Expand All @@ -954,9 +955,10 @@ def std(self, ddof=1, numeric_only: bool = False):
DataFrame or Series
Standard deviation of values within each group.
"""
nv.validate_resampler_func("std", args, kwargs)
return self._downsample("std", ddof=ddof, numeric_only=numeric_only)

def var(self, ddof=1, numeric_only: bool = False):
def var(self, ddof=1, numeric_only: bool = False, *args, **kwargs):
"""
Compute variance of groups, excluding missing values.

Expand All @@ -975,6 +977,7 @@ def var(self, ddof=1, numeric_only: bool = False):
DataFrame or Series
Variance of values within each group.
"""
nv.validate_resampler_func("var", args, kwargs)
return self._downsample("var", ddof=ddof, numeric_only=numeric_only)

@doc(GroupBy.size)
Expand Down Expand Up @@ -1063,7 +1066,10 @@ def f(
self,
numeric_only: bool | lib.NoDefault = lib.no_default,
min_count: int = 0,
*args,
**kwargs,
):
nv.validate_resampler_func(name, args, kwargs)
if numeric_only is lib.no_default and name != "sum":
# For DataFrameGroupBy, set it to be False for methods other than `sum`.
numeric_only = False
Expand All @@ -1075,8 +1081,9 @@ def f(
elif args == ("numeric_only",):
# error: All conditional function variants must have identical signatures
def f( # type: ignore[misc]
self, numeric_only: bool | lib.NoDefault = lib.no_default
self, numeric_only: bool | lib.NoDefault = lib.no_default, *args, **kwargs
):
nv.validate_resampler_func(name, args, kwargs)
return self._downsample(name, numeric_only=numeric_only)

elif args == ("ddof", "numeric_only"):
Expand All @@ -1085,14 +1092,20 @@ def f( # type: ignore[misc]
self,
ddof: int = 1,
numeric_only: bool | lib.NoDefault = lib.no_default,
*args,
**kwargs,
):
nv.validate_resampler_func(name, args, kwargs)
return self._downsample(name, ddof=ddof, numeric_only=numeric_only)

else:
# error: All conditional function variants must have identical signatures
def f( # type: ignore[misc]
self,
*args,
**kwargs,
):
nv.validate_resampler_func(name, args, kwargs)
return self._downsample(name)

f.__doc__ = getattr(docs_class, name).__doc__
Expand Down
15 changes: 15 additions & 0 deletions pandas/tests/resample/test_datetime_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

from pandas._libs import lib
from pandas._typing import DatetimeNaTType
from pandas.errors import UnsupportedFunctionCall

import pandas as pd
from pandas import (
Expand Down Expand Up @@ -225,6 +226,20 @@ def _ohlc(group):
tm.assert_frame_equal(result, expected)


@pytest.mark.parametrize("func", ["min", "max", "sum", "prod", "mean", "var", "std"])
def test_numpy_compat(func):
# see gh-12811
s = Series([1, 2, 3, 4, 5], index=date_range("20130101", periods=5, freq="s"))
r = s.resample("2s")

msg = "numpy operations are not valid with resample"

with pytest.raises(UnsupportedFunctionCall, match=msg):
getattr(r, func)(func, 1, 2, 3)
with pytest.raises(UnsupportedFunctionCall, match=msg):
getattr(r, func)(axis=1)


def test_resample_how_callables():
# GH#7929
data = np.arange(5, dtype=np.int64)
Expand Down
0