Skip to content

TST: GH30999 Add match=msg to all pytest.raises in tests/reductions and add an error message to nanmedian #38720

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
25 changes: 16 additions & 9 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -10351,15 +10351,22 @@ def _logical_func(
name, func, axis=0, bool_only=bool_only, skipna=skipna, **kwargs
)
return res._logical_func(name, func, skipna=skipna, **kwargs)

return self._reduce(
func,
name=name,
axis=axis,
skipna=skipna,
numeric_only=bool_only,
filter_type="bool",
)
try:
return self._reduce(
func,
name=name,
axis=axis,
skipna=skipna,
numeric_only=bool_only,
filter_type="bool",
)
except NotImplementedError as exc:
err_msg = str(exc)
if err_msg.endswith("does not implement numeric_only."):
raise NotImplementedError(
err_msg.replace("numeric_only", "bool_only")
) from exc
raise

def any(self, axis=0, bool_only=None, skipna=True, level=None, **kwargs):
return self._logical_func(
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/nanops.py
Original file line number Diff line number Diff line change
Expand Up @@ -685,7 +685,7 @@ def get_median(x):
values = values.astype("f8")
except ValueError as err:
# e.g. "could not convert string to float: 'a'"
raise TypeError from err
raise TypeError(str(err)) from err
if mask is not None:
values[mask] = np.nan

Expand Down
40 changes: 29 additions & 11 deletions pandas/tests/reductions/test_reductions.py
Original file line number Diff line number Diff line change
Expand Up @@ -526,9 +526,17 @@ def test_numpy_minmax_period(self):
def test_min_max_categorical(self):

ci = pd.CategoricalIndex(list("aabbca"), categories=list("cab"), ordered=False)
with pytest.raises(TypeError):
msg = (
r"Categorical is not ordered for operation min\n"
r"you can use .as_ordered\(\) to change the Categorical to an ordered one\n"
)
with pytest.raises(TypeError, match=msg):
ci.min()
with pytest.raises(TypeError):
msg = (
r"Categorical is not ordered for operation max\n"
r"you can use .as_ordered\(\) to change the Categorical to an ordered one\n"
)
with pytest.raises(TypeError, match=msg):
ci.max()

ci = pd.CategoricalIndex(list("aabbca"), categories=list("cab"), ordered=True)
Expand Down Expand Up @@ -880,16 +888,18 @@ def test_all_any_params(self):
tm.assert_series_equal(s.all(level=0), Series([False, True, False]))
tm.assert_series_equal(s.any(level=0), Series([False, True, True]))

# bool_only is not implemented with level option.
with pytest.raises(NotImplementedError):
msg = "Option bool_only is not implemented with option level"
with pytest.raises(NotImplementedError, match=msg):
s.any(bool_only=True, level=0)
with pytest.raises(NotImplementedError):
with pytest.raises(NotImplementedError, match=msg):
s.all(bool_only=True, level=0)

# bool_only is not implemented alone.
with pytest.raises(NotImplementedError):
msg = "Series.any does not implement bool_only"
with pytest.raises(NotImplementedError, match=msg):
s.any(bool_only=True)
with pytest.raises(NotImplementedError):
msg = "Series.all does not implement bool_only."
with pytest.raises(NotImplementedError, match=msg):
s.all(bool_only=True)

def test_all_any_boolean(self):
Expand Down Expand Up @@ -980,13 +990,21 @@ def test_assert_idxminmax_raises(self, test_input, error_type):
"""
Cases where ``Series.argmax`` and related should raise an exception
"""
with pytest.raises(error_type):
msg = (
"reduction operation 'argmin' not allowed for this dtype|"
"attempt to get argmin of an empty sequence"
)
with pytest.raises(error_type, match=msg):
test_input.idxmin()
with pytest.raises(error_type):
with pytest.raises(error_type, match=msg):
test_input.idxmin(skipna=False)
with pytest.raises(error_type):
msg = (
"reduction operation 'argmax' not allowed for this dtype|"
"attempt to get argmax of an empty sequence"
)
with pytest.raises(error_type, match=msg):
test_input.idxmax()
with pytest.raises(error_type):
with pytest.raises(error_type, match=msg):
test_input.idxmax(skipna=False)

def test_idxminmax_with_inf(self):
Expand Down
8 changes: 5 additions & 3 deletions pandas/tests/reductions/test_stat_reductions.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,8 @@ def _check_stat_op(
# mean, idxmax, idxmin, min, and max are valid for dates
if name not in ["max", "min", "mean", "median", "std"]:
ds = Series(pd.date_range("1/1/2001", periods=10))
with pytest.raises(TypeError):
msg = f"'DatetimeArray' does not implement reduction '{name}'"
with pytest.raises(TypeError, match=msg):
f(ds)

# skipna or no
Expand Down Expand Up @@ -134,11 +135,12 @@ def _check_stat_op(

# check on string data
if name not in ["sum", "min", "max"]:
with pytest.raises(TypeError):
with pytest.raises(TypeError, match=None):
f(Series(list("abc")))

# Invalid axis.
with pytest.raises(ValueError):
msg = "No axis named 1 for object type Series"
with pytest.raises(ValueError, match=msg):
f(string_series_, axis=1)

# Unimplemented numeric_only parameter.
Expand Down