Skip to content

Commit 169b26d

Browse files
moinkluckyvs1
authored andcommitted
TST: GH30999 Add match=msg to all pytest.raises in tests/reductions and add an error message to nanmedian (pandas-dev#38720)
1 parent 356ba63 commit 169b26d

File tree

3 files changed

+37
-15
lines changed

3 files changed

+37
-15
lines changed

pandas/core/nanops.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -685,7 +685,7 @@ def get_median(x):
685685
values = values.astype("f8")
686686
except ValueError as err:
687687
# e.g. "could not convert string to float: 'a'"
688-
raise TypeError from err
688+
raise TypeError(str(err)) from err
689689
if mask is not None:
690690
values[mask] = np.nan
691691

pandas/tests/reductions/test_reductions.py

+31-11
Original file line numberDiff line numberDiff line change
@@ -527,9 +527,17 @@ def test_numpy_minmax_period(self):
527527
def test_min_max_categorical(self):
528528

529529
ci = pd.CategoricalIndex(list("aabbca"), categories=list("cab"), ordered=False)
530-
with pytest.raises(TypeError):
530+
msg = (
531+
r"Categorical is not ordered for operation min\n"
532+
r"you can use .as_ordered\(\) to change the Categorical to an ordered one\n"
533+
)
534+
with pytest.raises(TypeError, match=msg):
531535
ci.min()
532-
with pytest.raises(TypeError):
536+
msg = (
537+
r"Categorical is not ordered for operation max\n"
538+
r"you can use .as_ordered\(\) to change the Categorical to an ordered one\n"
539+
)
540+
with pytest.raises(TypeError, match=msg):
533541
ci.max()
534542

535543
ci = pd.CategoricalIndex(list("aabbca"), categories=list("cab"), ordered=True)
@@ -881,16 +889,20 @@ def test_all_any_params(self):
881889
tm.assert_series_equal(s.all(level=0), Series([False, True, False]))
882890
tm.assert_series_equal(s.any(level=0), Series([False, True, True]))
883891

884-
# bool_only is not implemented with level option.
885-
with pytest.raises(NotImplementedError):
892+
msg = "Option bool_only is not implemented with option level"
893+
with pytest.raises(NotImplementedError, match=msg):
886894
s.any(bool_only=True, level=0)
887-
with pytest.raises(NotImplementedError):
895+
with pytest.raises(NotImplementedError, match=msg):
888896
s.all(bool_only=True, level=0)
889897

890898
# bool_only is not implemented alone.
891-
with pytest.raises(NotImplementedError):
899+
# TODO GH38810 change this error message to:
900+
# "Series.any does not implement bool_only"
901+
msg = "Series.any does not implement numeric_only"
902+
with pytest.raises(NotImplementedError, match=msg):
892903
s.any(bool_only=True)
893-
with pytest.raises(NotImplementedError):
904+
msg = "Series.all does not implement numeric_only."
905+
with pytest.raises(NotImplementedError, match=msg):
894906
s.all(bool_only=True)
895907

896908
def test_all_any_boolean(self):
@@ -1023,13 +1035,21 @@ def test_assert_idxminmax_raises(self, test_input, error_type):
10231035
"""
10241036
Cases where ``Series.argmax`` and related should raise an exception
10251037
"""
1026-
with pytest.raises(error_type):
1038+
msg = (
1039+
"reduction operation 'argmin' not allowed for this dtype|"
1040+
"attempt to get argmin of an empty sequence"
1041+
)
1042+
with pytest.raises(error_type, match=msg):
10271043
test_input.idxmin()
1028-
with pytest.raises(error_type):
1044+
with pytest.raises(error_type, match=msg):
10291045
test_input.idxmin(skipna=False)
1030-
with pytest.raises(error_type):
1046+
msg = (
1047+
"reduction operation 'argmax' not allowed for this dtype|"
1048+
"attempt to get argmax of an empty sequence"
1049+
)
1050+
with pytest.raises(error_type, match=msg):
10311051
test_input.idxmax()
1032-
with pytest.raises(error_type):
1052+
with pytest.raises(error_type, match=msg):
10331053
test_input.idxmax(skipna=False)
10341054

10351055
def test_idxminmax_with_inf(self):

pandas/tests/reductions/test_stat_reductions.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,8 @@ def _check_stat_op(
9898
# mean, idxmax, idxmin, min, and max are valid for dates
9999
if name not in ["max", "min", "mean", "median", "std"]:
100100
ds = Series(pd.date_range("1/1/2001", periods=10))
101-
with pytest.raises(TypeError):
101+
msg = f"'DatetimeArray' does not implement reduction '{name}'"
102+
with pytest.raises(TypeError, match=msg):
102103
f(ds)
103104

104105
# skipna or no
@@ -134,11 +135,12 @@ def _check_stat_op(
134135

135136
# check on string data
136137
if name not in ["sum", "min", "max"]:
137-
with pytest.raises(TypeError):
138+
with pytest.raises(TypeError, match=None):
138139
f(Series(list("abc")))
139140

140141
# Invalid axis.
141-
with pytest.raises(ValueError):
142+
msg = "No axis named 1 for object type Series"
143+
with pytest.raises(ValueError, match=msg):
142144
f(string_series_, axis=1)
143145

144146
# Unimplemented numeric_only parameter.

0 commit comments

Comments
 (0)