From bf659993d465c43265f4011ee1ee672ad3b68f30 Mon Sep 17 00:00:00 2001 From: Brock Date: Tue, 3 Nov 2020 16:29:27 -0800 Subject: [PATCH] parametrize set_axis tests --- pandas/tests/frame/test_alter_axes.py | 16 ------ pandas/tests/generic/methods/test_set_axis.py | 22 ++++++++ pandas/tests/series/methods/test_set_name.py | 21 +++++++ pandas/tests/series/test_alter_axes.py | 55 ------------------- 4 files changed, 43 insertions(+), 71 deletions(-) create mode 100644 pandas/tests/series/methods/test_set_name.py delete mode 100644 pandas/tests/series/test_alter_axes.py diff --git a/pandas/tests/frame/test_alter_axes.py b/pandas/tests/frame/test_alter_axes.py index 3cd35e900ee06..4bd1d5fa56468 100644 --- a/pandas/tests/frame/test_alter_axes.py +++ b/pandas/tests/frame/test_alter_axes.py @@ -1,7 +1,6 @@ from datetime import datetime import numpy as np -import pytest from pandas.core.dtypes.common import ( is_categorical_dtype, @@ -24,15 +23,6 @@ class TestDataFrameAlterAxes: - def test_set_index_directly(self, float_string_frame): - df = float_string_frame - idx = Index(np.arange(len(df))[::-1]) - - df.index = idx - tm.assert_index_equal(df.index, idx) - with pytest.raises(ValueError, match="Length mismatch"): - df.index = idx[::2] - def test_convert_dti_to_series(self): # don't cast a DatetimeIndex WITH a tz, leave as object # GH 6032 @@ -101,12 +91,6 @@ def test_convert_dti_to_series(self): df.pop("ts") tm.assert_frame_equal(df, expected) - def test_set_columns(self, float_string_frame): - cols = Index(np.arange(len(float_string_frame.columns))) - float_string_frame.columns = cols - with pytest.raises(ValueError, match="Length mismatch"): - float_string_frame.columns = cols[::2] - def test_dti_set_index_reindex(self): # GH 6631 df = DataFrame(np.random.random(6)) diff --git a/pandas/tests/generic/methods/test_set_axis.py b/pandas/tests/generic/methods/test_set_axis.py index 278d43ef93d2f..a46a91811f40e 100644 --- a/pandas/tests/generic/methods/test_set_axis.py +++ b/pandas/tests/generic/methods/test_set_axis.py @@ -57,6 +57,28 @@ def test_set_axis_invalid_axis_name(self, axis, obj): with pytest.raises(ValueError, match="No axis named"): obj.set_axis(list("abc"), axis=axis) + def test_set_axis_setattr_index_not_collection(self, obj): + # wrong type + msg = ( + r"Index\(\.\.\.\) must be called with a collection of some " + r"kind, None was passed" + ) + with pytest.raises(TypeError, match=msg): + obj.index = None + + def test_set_axis_setattr_index_wrong_length(self, obj): + # wrong length + msg = ( + f"Length mismatch: Expected axis has {len(obj)} elements, " + f"new values have {len(obj)-1} elements" + ) + with pytest.raises(ValueError, match=msg): + obj.index = np.arange(len(obj) - 1) + + if obj.ndim == 2: + with pytest.raises(ValueError, match="Length mismatch"): + obj.columns = obj.columns[::2] + class TestDataFrameSetAxis(SharedSetAxisTests): @pytest.fixture diff --git a/pandas/tests/series/methods/test_set_name.py b/pandas/tests/series/methods/test_set_name.py new file mode 100644 index 0000000000000..cbc8ebde7a8ab --- /dev/null +++ b/pandas/tests/series/methods/test_set_name.py @@ -0,0 +1,21 @@ +from datetime import datetime + +from pandas import Series + + +class TestSetName: + def test_set_name(self): + ser = Series([1, 2, 3]) + ser2 = ser._set_name("foo") + assert ser2.name == "foo" + assert ser.name is None + assert ser is not ser2 + + def test_set_name_attribute(self): + ser = Series([1, 2, 3]) + ser2 = Series([1, 2, 3], name="bar") + for name in [7, 7.0, "name", datetime(2001, 1, 1), (1,), "\u05D0"]: + ser.name = name + assert ser.name == name + ser2.name = name + assert ser2.name == name diff --git a/pandas/tests/series/test_alter_axes.py b/pandas/tests/series/test_alter_axes.py deleted file mode 100644 index 181d7de43d945..0000000000000 --- a/pandas/tests/series/test_alter_axes.py +++ /dev/null @@ -1,55 +0,0 @@ -from datetime import datetime - -import numpy as np -import pytest - -from pandas import Index, Series -import pandas._testing as tm - - -class TestSeriesAlterAxes: - def test_setindex(self, string_series): - # wrong type - msg = ( - r"Index\(\.\.\.\) must be called with a collection of some " - r"kind, None was passed" - ) - with pytest.raises(TypeError, match=msg): - string_series.index = None - - # wrong length - msg = ( - "Length mismatch: Expected axis has 30 elements, " - "new values have 29 elements" - ) - with pytest.raises(ValueError, match=msg): - string_series.index = np.arange(len(string_series) - 1) - - # works - string_series.index = np.arange(len(string_series)) - assert isinstance(string_series.index, Index) - - # Renaming - - def test_set_name_attribute(self): - s = Series([1, 2, 3]) - s2 = Series([1, 2, 3], name="bar") - for name in [7, 7.0, "name", datetime(2001, 1, 1), (1,), "\u05D0"]: - s.name = name - assert s.name == name - s2.name = name - assert s2.name == name - - def test_set_name(self): - s = Series([1, 2, 3]) - s2 = s._set_name("foo") - assert s2.name == "foo" - assert s.name is None - assert s is not s2 - - def test_set_index_makes_timeseries(self): - idx = tm.makeDateIndex(10) - - s = Series(range(10)) - s.index = idx - assert s.index._is_all_dates