Skip to content
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

BUG: Correct numeric_only default for resample var and std #47749

Merged
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
16 changes: 14 additions & 2 deletions pandas/core/resample.py
Original file line number Diff line number Diff line change
Expand Up @@ -937,7 +937,13 @@ def asfreq(self, fill_value=None):
"""
return self._upsample("asfreq", fill_value=fill_value)

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

Expand All @@ -958,7 +964,13 @@ def std(self, ddof=1, numeric_only: bool = False, *args, **kwargs):
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, *args, **kwargs):
def var(
self,
ddof=1,
numeric_only: bool | lib.NoDefault = lib.no_default,
*args,
**kwargs,
):
"""
Compute variance of groups, excluding missing values.

Expand Down
8 changes: 6 additions & 2 deletions pandas/tests/resample/test_resample_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -859,6 +859,10 @@ def test_frame_downsample_method(method, numeric_only, expected_data):
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")
if numeric_only is lib.no_default:
kwargs = {}
else:
kwargs = {"numeric_only": numeric_only}

func = getattr(resampled, method)
if numeric_only is lib.no_default and method not in (
Expand All @@ -882,9 +886,9 @@ def test_frame_downsample_method(method, numeric_only, expected_data):
if isinstance(expected_data, str):
klass = TypeError if method == "var" else ValueError
with pytest.raises(klass, match=expected_data):
_ = func(numeric_only=numeric_only)
_ = func(**kwargs)
else:
result = func(numeric_only=numeric_only)
result = func(**kwargs)
expected = DataFrame(expected_data, index=expected_index)
tm.assert_frame_equal(result, expected)

Expand Down