Skip to content
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
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 numeric_only"
with pytest.raises(NotImplementedError, match=msg):
s.any(bool_only=True)
with pytest.raises(NotImplementedError):
msg = "Series.all does not implement numeric_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
15 changes: 12 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 @@ -132,13 +133,21 @@ def _check_stat_op(
exp = alternate(s)
assert res == exp

# Series.median(Series('abc'))
# check on string data
msg = (
"could not convert string to float|"
r"complex\(\) arg is a malformed string|"
"can't multiply sequence by non-int of type 'str'|"
"Could not convert abc to numeric"
)
if name not in ["sum", "min", "max"]:
with pytest.raises(TypeError):
with pytest.raises(TypeError, match=msg):
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