Skip to content

TST: Avoid bare pytest.raises in test_series.py #32879

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
4 changes: 2 additions & 2 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ def _get_axis_number(cls, axis):
return cls._AXIS_NUMBERS[axis]
except KeyError:
pass
raise ValueError(f"No axis named {axis} for object type {cls}")
raise ValueError(f"No axis named {axis} for object type {cls.__name__}")

@classmethod
def _get_axis_name(cls, axis):
Expand All @@ -367,7 +367,7 @@ def _get_axis_name(cls, axis):
return cls._AXIS_NAMES[axis]
except KeyError:
pass
raise ValueError(f"No axis named {axis} for object type {cls}")
raise ValueError(f"No axis named {axis} for object type {cls.__name__}")

def _get_axis(self, axis):
name = self._get_axis_name(axis)
Expand Down
28 changes: 12 additions & 16 deletions pandas/tests/dtypes/test_dtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -414,25 +414,22 @@ def test_construction_from_string(self, dtype):
assert is_dtype_equal(dtype, result)
result = PeriodDtype.construct_from_string("period[D]")
assert is_dtype_equal(dtype, result)
msg = "Cannot construct a 'PeriodDtype' from "
with pytest.raises(TypeError, match=msg):
PeriodDtype.construct_from_string("foo")
with pytest.raises(TypeError, match=msg):
PeriodDtype.construct_from_string("period[foo]")
with pytest.raises(TypeError, match=msg):
PeriodDtype.construct_from_string("foo[D]")

with pytest.raises(TypeError, match=msg):
PeriodDtype.construct_from_string("datetime64[ns]")
with pytest.raises(TypeError, match=msg):
PeriodDtype.construct_from_string("datetime64[ns, US/Eastern]")

with pytest.raises(TypeError, match="list"):
PeriodDtype.construct_from_string([1, 2, 3])

@pytest.mark.parametrize(
"string",
[
"foo",
"period[foo]",
"foo[D]",
"datetime64[ns]",
"datetime64[ns, US/Eastern]",
],
)
def test_construct_dtype_from_string_invalid_raises(self, string):
msg = f"Cannot construct a 'PeriodDtype' from '{string}'"
with pytest.raises(TypeError, match=re.escape(msg)):
PeriodDtype.construct_from_string(string)

def test_is_dtype(self, dtype):
assert PeriodDtype.is_dtype(dtype)
assert PeriodDtype.is_dtype("period[D]")
Expand Down Expand Up @@ -479,7 +476,6 @@ def test_basic(self, dtype):

def test_empty(self):
dt = PeriodDtype()
# https://github.com/pandas-dev/pandas/issues/27388
msg = "object has no attribute 'freqstr'"
with pytest.raises(AttributeError, match=msg):
str(dt)
Expand Down
7 changes: 2 additions & 5 deletions pandas/tests/frame/methods/test_quantile.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,13 +100,10 @@ def test_quantile_axis_parameter(self):
result = df.quantile(0.5, axis="columns")
tm.assert_series_equal(result, expected)

msg = "No axis named -1 for object type <class 'pandas.core.frame.DataFrame'>"
msg = "No axis named -1 for object type DataFrame"
with pytest.raises(ValueError, match=msg):
df.quantile(0.1, axis=-1)
msg = (
"No axis named column for object type "
"<class 'pandas.core.frame.DataFrame'>"
)
msg = "No axis named column for object type DataFrame"
with pytest.raises(ValueError, match=msg):
df.quantile(0.1, axis="column")

Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/frame/methods/test_sort_values.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def test_sort_values(self):
sorted_df = frame.sort_values(by=["B", "A"], ascending=[True, False])
tm.assert_frame_equal(sorted_df, expected)

msg = "No axis named 2 for object type <class 'pandas.core.frame.DataFrame'>"
msg = "No axis named 2 for object type DataFrame"
with pytest.raises(ValueError, match=msg):
frame.sort_values(by=["A", "B"], axis=2, inplace=True)

Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/frame/methods/test_to_period.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,6 @@ def test_frame_to_period(self):
pts = df.to_period("M", axis=1)
tm.assert_index_equal(pts.columns, exp.columns.asfreq("M"))

msg = "No axis named 2 for object type <class 'pandas.core.frame.DataFrame'>"
msg = "No axis named 2 for object type DataFrame"
with pytest.raises(ValueError, match=msg):
df.to_period(axis=2)
4 changes: 2 additions & 2 deletions pandas/tests/frame/test_analytics.py
Original file line number Diff line number Diff line change
Expand Up @@ -919,7 +919,7 @@ def test_idxmin(self, float_frame, int_frame):
expected = df.apply(Series.idxmin, axis=axis, skipna=skipna)
tm.assert_series_equal(result, expected)

msg = "No axis named 2 for object type <class 'pandas.core.frame.DataFrame'>"
msg = "No axis named 2 for object type DataFrame"
with pytest.raises(ValueError, match=msg):
frame.idxmin(axis=2)

Expand All @@ -934,7 +934,7 @@ def test_idxmax(self, float_frame, int_frame):
expected = df.apply(Series.idxmax, axis=axis, skipna=skipna)
tm.assert_series_equal(result, expected)

msg = "No axis named 2 for object type <class 'pandas.core.frame.DataFrame'>"
msg = "No axis named 2 for object type DataFrame"
with pytest.raises(ValueError, match=msg):
frame.idxmax(axis=2)

Expand Down
5 changes: 1 addition & 4 deletions pandas/tests/frame/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -371,10 +371,7 @@ def test_swapaxes(self):
tm.assert_frame_equal(df.T, df.swapaxes(0, 1))
tm.assert_frame_equal(df.T, df.swapaxes(1, 0))
tm.assert_frame_equal(df, df.swapaxes(0, 0))
msg = (
"No axis named 2 for object type "
r"<class 'pandas.core(.sparse)?.frame.(Sparse)?DataFrame'>"
)
msg = "No axis named 2 for object type DataFrame"
with pytest.raises(ValueError, match=msg):
df.swapaxes(2, 5)

Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/frame/test_missing.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ def test_dropna(self):
tm.assert_frame_equal(dropped, expected)

# bad input
msg = "No axis named 3 for object type <class 'pandas.core.frame.DataFrame'>"
msg = "No axis named 3 for object type DataFrame"
with pytest.raises(ValueError, match=msg):
df.dropna(axis=3)

Expand Down
4 changes: 2 additions & 2 deletions pandas/tests/generic/test_generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -692,10 +692,10 @@ def test_squeeze(self):
tm.assert_series_equal(df.squeeze(axis=1), df.iloc[:, 0])
tm.assert_series_equal(df.squeeze(axis="columns"), df.iloc[:, 0])
assert df.squeeze() == df.iloc[0, 0]
msg = "No axis named 2 for object type <class 'pandas.core.frame.DataFrame'>"
msg = "No axis named 2 for object type DataFrame"
with pytest.raises(ValueError, match=msg):
df.squeeze(axis=2)
msg = "No axis named x for object type <class 'pandas.core.frame.DataFrame'>"
msg = "No axis named x for object type DataFrame"
with pytest.raises(ValueError, match=msg):
df.squeeze(axis="x")

Expand Down
3 changes: 2 additions & 1 deletion pandas/tests/generic/test_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ def test_set_axis_name_mi(self, func):

def test_set_axis_name_raises(self):
s = pd.Series([1])
with pytest.raises(ValueError):
msg = "No axis named 1 for object type Series"
with pytest.raises(ValueError, match=msg):
s._set_axis_name(name="a", axis=1)

def test_get_numeric_data_preserve_dtype(self):
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/series/methods/test_between_time.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,6 @@ def test_between_time_axis(self):

assert len(ts.between_time(stime, etime)) == expected_length
assert len(ts.between_time(stime, etime, axis=0)) == expected_length
msg = "No axis named 1 for object type <class 'pandas.core.series.Series'>"
msg = "No axis named 1 for object type Series"
with pytest.raises(ValueError, match=msg):
ts.between_time(stime, etime, axis=1)
4 changes: 1 addition & 3 deletions pandas/tests/series/methods/test_rank.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,9 +202,7 @@ def test_rank_categorical(self):
def test_rank_signature(self):
s = Series([0, 1])
s.rank(method="average")
msg = (
"No axis named average for object type <class 'pandas.core.series.Series'>"
)
msg = "No axis named average for object type Series"
with pytest.raises(ValueError, match=msg):
s.rank("average")

Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/series/methods/test_sort_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def test_sort_index(self, datetime_series):
sorted_series = random_order.sort_index(axis=0)
tm.assert_series_equal(sorted_series, datetime_series)

msg = "No axis named 1 for object type <class 'pandas.core.series.Series'>"
msg = "No axis named 1 for object type Series"
with pytest.raises(ValueError, match=msg):
random_order.sort_values(axis=1)

Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/series/test_missing.py
Original file line number Diff line number Diff line change
Expand Up @@ -823,7 +823,7 @@ def test_dropna_empty(self):
assert len(s) == 0

# invalid axis
msg = "No axis named 1 for object type <class 'pandas.core.series.Series'>"
msg = "No axis named 1 for object type Series"
with pytest.raises(ValueError, match=msg):
s.dropna(axis=1)

Expand Down