diff --git a/doc/source/whatsnew/v0.25.0.rst b/doc/source/whatsnew/v0.25.0.rst index 5dd6ce168a0de..ea08a0a6fe07b 100644 --- a/doc/source/whatsnew/v0.25.0.rst +++ b/doc/source/whatsnew/v0.25.0.rst @@ -196,7 +196,7 @@ Missing ^^^^^^^ - Fixed misleading exception message in :meth:`Series.missing` if argument ``order`` is required, but omitted (:issue:`10633`, :issue:`24014`). -- +- Fixed class type displayed in exception message in :meth:`DataFrame.dropna` if invalid ``axis`` parameter passed (:issue:`25555`) - MultiIndex diff --git a/pandas/core/generic.py b/pandas/core/generic.py index 7915d98662c9e..d7f71df99cdb6 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -358,7 +358,7 @@ def _get_axis_number(cls, axis): except KeyError: pass raise ValueError('No axis named {0} for object type {1}' - .format(axis, type(cls))) + .format(axis, cls)) @classmethod def _get_axis_name(cls, axis): @@ -372,7 +372,7 @@ def _get_axis_name(cls, axis): except KeyError: pass raise ValueError('No axis named {0} for object type {1}' - .format(axis, type(cls))) + .format(axis, cls)) def _get_axis(self, axis): name = self._get_axis_name(axis) diff --git a/pandas/tests/frame/test_analytics.py b/pandas/tests/frame/test_analytics.py index 3363a45149fff..2969e8be2db03 100644 --- a/pandas/tests/frame/test_analytics.py +++ b/pandas/tests/frame/test_analytics.py @@ -1385,7 +1385,8 @@ def test_idxmin(self, float_frame, int_frame): skipna=skipna) tm.assert_series_equal(result, expected) - msg = "No axis named 2 for object type " + msg = ("No axis named 2 for object type" + " ") with pytest.raises(ValueError, match=msg): frame.idxmin(axis=2) @@ -1402,7 +1403,8 @@ def test_idxmax(self, float_frame, int_frame): skipna=skipna) tm.assert_series_equal(result, expected) - msg = "No axis named 2 for object type " + msg = ("No axis named 2 for object type" + " ") with pytest.raises(ValueError, match=msg): frame.idxmax(axis=2) diff --git a/pandas/tests/frame/test_api.py b/pandas/tests/frame/test_api.py index 118341276d799..badfa0ca8fd15 100644 --- a/pandas/tests/frame/test_api.py +++ b/pandas/tests/frame/test_api.py @@ -366,7 +366,8 @@ def test_swapaxes(self): self._assert_frame_equal(df.T, df.swapaxes(0, 1)) self._assert_frame_equal(df.T, df.swapaxes(1, 0)) self._assert_frame_equal(df, df.swapaxes(0, 0)) - msg = "No axis named 2 for object type " + msg = ("No axis named 2 for object type" + r" ") with pytest.raises(ValueError, match=msg): df.swapaxes(2, 5) diff --git a/pandas/tests/frame/test_axis_select_reindex.py b/pandas/tests/frame/test_axis_select_reindex.py index fb00776b33cbb..cf8c55f00b061 100644 --- a/pandas/tests/frame/test_axis_select_reindex.py +++ b/pandas/tests/frame/test_axis_select_reindex.py @@ -1067,7 +1067,8 @@ def test_reindex_axis(self): reindexed2 = self.intframe.reindex(index=rows) assert_frame_equal(reindexed1, reindexed2) - msg = "No axis named 2 for object type " + msg = ("No axis named 2 for object type" + " ") with pytest.raises(ValueError, match=msg): self.intframe.reindex_axis(rows, axis=2) diff --git a/pandas/tests/frame/test_missing.py b/pandas/tests/frame/test_missing.py index 2f3b0a9f76de9..189531c7b4459 100644 --- a/pandas/tests/frame/test_missing.py +++ b/pandas/tests/frame/test_missing.py @@ -140,7 +140,8 @@ def test_dropna(self): assert_frame_equal(dropped, expected) # bad input - msg = "No axis named 3 for object type " + msg = ("No axis named 3 for object type" + " ") with pytest.raises(ValueError, match=msg): df.dropna(axis=3) diff --git a/pandas/tests/frame/test_quantile.py b/pandas/tests/frame/test_quantile.py index 19b6636978643..facbfdd0c032b 100644 --- a/pandas/tests/frame/test_quantile.py +++ b/pandas/tests/frame/test_quantile.py @@ -95,10 +95,12 @@ def test_quantile_axis_parameter(self): result = df.quantile(.5, axis="columns") assert_series_equal(result, expected) - msg = "No axis named -1 for object type " + msg = ("No axis named -1 for object type" + " ") with pytest.raises(ValueError, match=msg): df.quantile(0.1, axis=-1) - msg = "No axis named column for object type " + msg = ("No axis named column for object type" + " ") with pytest.raises(ValueError, match=msg): df.quantile(0.1, axis="column") diff --git a/pandas/tests/frame/test_sorting.py b/pandas/tests/frame/test_sorting.py index 8b29394bcab84..baf50982d8ab0 100644 --- a/pandas/tests/frame/test_sorting.py +++ b/pandas/tests/frame/test_sorting.py @@ -55,7 +55,8 @@ def test_sort_values(self): sorted_df = frame.sort_values(by=['B', 'A'], ascending=[True, False]) assert_frame_equal(sorted_df, expected) - msg = "No axis named 2 for object type " + msg = ("No axis named 2 for object type" + " ") with pytest.raises(ValueError, match=msg): frame.sort_values(by=['A', 'B'], axis=2, inplace=True) diff --git a/pandas/tests/frame/test_timeseries.py b/pandas/tests/frame/test_timeseries.py index 716a9e30e4cc3..9965be9091451 100644 --- a/pandas/tests/frame/test_timeseries.py +++ b/pandas/tests/frame/test_timeseries.py @@ -862,7 +862,8 @@ 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 " + msg = ("No axis named 2 for object type" + " ") with pytest.raises(ValueError, match=msg): df.to_period(axis=2) diff --git a/pandas/tests/series/test_analytics.py b/pandas/tests/series/test_analytics.py index 1f265d574da15..d7d9c526503cb 100644 --- a/pandas/tests/series/test_analytics.py +++ b/pandas/tests/series/test_analytics.py @@ -771,6 +771,7 @@ def test_isin_empty(self, empty): result = s.isin(empty) tm.assert_series_equal(expected, result) + @pytest.mark.skipif(PY2, reason="pytest.raises match regex fails") def test_ptp(self): # GH21614 N = 1000 @@ -796,7 +797,8 @@ def test_ptp(self): with tm.assert_produces_warning(FutureWarning, check_stacklevel=False): tm.assert_series_equal(s.ptp(level=0, skipna=False), expected) - msg = r"No axis named 1 for object type <(class|type) 'type'>" + msg = ("No axis named 1 for object type" + " ") with pytest.raises(ValueError, match=msg): with tm.assert_produces_warning(FutureWarning, check_stacklevel=False): diff --git a/pandas/tests/series/test_missing.py b/pandas/tests/series/test_missing.py index f07dd1dfb5fda..ef9e575e60385 100644 --- a/pandas/tests/series/test_missing.py +++ b/pandas/tests/series/test_missing.py @@ -10,7 +10,7 @@ import pytz from pandas._libs.tslib import iNaT -from pandas.compat import range +from pandas.compat import PY2, range from pandas.errors import PerformanceWarning import pandas.util._test_decorators as td @@ -654,6 +654,7 @@ def test_timedelta64_nan(self): # expected = (datetime_series >= -0.5) & (datetime_series <= 0.5) # assert_series_equal(selector, expected) + @pytest.mark.skipif(PY2, reason="pytest.raises match regex fails") def test_dropna_empty(self): s = Series([]) assert len(s.dropna()) == 0 @@ -661,7 +662,8 @@ def test_dropna_empty(self): assert len(s) == 0 # invalid axis - msg = r"No axis named 1 for object type <(class|type) 'type'>" + msg = ("No axis named 1 for object type" + " ") with pytest.raises(ValueError, match=msg): s.dropna(axis=1) diff --git a/pandas/tests/series/test_rank.py b/pandas/tests/series/test_rank.py index dfcda889269ee..373083c077e28 100644 --- a/pandas/tests/series/test_rank.py +++ b/pandas/tests/series/test_rank.py @@ -9,7 +9,7 @@ from pandas._libs.algos import Infinity, NegInfinity from pandas._libs.tslib import iNaT import pandas.compat as compat -from pandas.compat import product +from pandas.compat import PY2, product import pandas.util._test_decorators as td from pandas import NaT, Series, Timestamp, date_range @@ -203,10 +203,12 @@ def test_rank_categorical(self): assert_series_equal(na_ser.rank(na_option='bottom', pct=True), exp_bot) assert_series_equal(na_ser.rank(na_option='keep', pct=True), exp_keep) + @pytest.mark.skipif(PY2, reason="pytest.raises match regex fails") def test_rank_signature(self): s = Series([0, 1]) s.rank(method='average') - msg = r"No axis named average for object type <(class|type) 'type'>" + msg = ("No axis named average for object type" + " ") with pytest.raises(ValueError, match=msg): s.rank('average') diff --git a/pandas/tests/series/test_sorting.py b/pandas/tests/series/test_sorting.py index 216f84c8f077a..162fa4ac9ab52 100644 --- a/pandas/tests/series/test_sorting.py +++ b/pandas/tests/series/test_sorting.py @@ -5,6 +5,8 @@ import numpy as np import pytest +from pandas.compat import PY2 + from pandas import Categorical, DataFrame, IntervalIndex, MultiIndex, Series import pandas.util.testing as tm from pandas.util.testing import assert_almost_equal, assert_series_equal @@ -88,6 +90,7 @@ def test_sort_values(self): with pytest.raises(ValueError, match=msg): s.sort_values(inplace=True) + @pytest.mark.skipif(PY2, reason="pytest.raises match regex fails") def test_sort_index(self): rindex = list(self.ts.index) random.shuffle(rindex) @@ -109,7 +112,8 @@ def test_sort_index(self): sorted_series = random_order.sort_index(axis=0) assert_series_equal(sorted_series, self.ts) - msg = r"No axis named 1 for object type <(class|type) 'type'>" + msg = ("No axis named 1 for object type" + " ") with pytest.raises(ValueError, match=msg): random_order.sort_values(axis=1) diff --git a/pandas/tests/series/test_timeseries.py b/pandas/tests/series/test_timeseries.py index d082b023e1f27..b6896685dd474 100644 --- a/pandas/tests/series/test_timeseries.py +++ b/pandas/tests/series/test_timeseries.py @@ -8,7 +8,7 @@ from pandas._libs.tslib import iNaT from pandas._libs.tslibs.np_datetime import OutOfBoundsDatetime -from pandas.compat import StringIO, lrange, product +from pandas.compat import PY2, StringIO, lrange, product from pandas.errors import NullFrequencyError import pandas.util._test_decorators as td @@ -867,6 +867,7 @@ def test_between_time_formats(self): for time_string in strings: assert len(ts.between_time(*time_string)) == expected_length + @pytest.mark.skipif(PY2, reason="pytest.raises match regex fails") def test_between_time_axis(self): # issue 8839 rng = date_range('1/1/2000', periods=100, freq='10min') @@ -876,7 +877,8 @@ 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 = r"No axis named 1 for object type <(class|type) 'type'>" + msg = ("No axis named 1 for object type" + " ") with pytest.raises(ValueError, match=msg): ts.between_time(stime, etime, axis=1)