Skip to content

Commit e5cbaec

Browse files
authored
parametrize set_axis tests (#37619)
1 parent 831320f commit e5cbaec

File tree

4 files changed

+43
-71
lines changed

4 files changed

+43
-71
lines changed

pandas/tests/frame/test_alter_axes.py

-16
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
from datetime import datetime
22

33
import numpy as np
4-
import pytest
54

65
from pandas.core.dtypes.common import (
76
is_categorical_dtype,
@@ -24,15 +23,6 @@
2423

2524

2625
class TestDataFrameAlterAxes:
27-
def test_set_index_directly(self, float_string_frame):
28-
df = float_string_frame
29-
idx = Index(np.arange(len(df))[::-1])
30-
31-
df.index = idx
32-
tm.assert_index_equal(df.index, idx)
33-
with pytest.raises(ValueError, match="Length mismatch"):
34-
df.index = idx[::2]
35-
3626
def test_convert_dti_to_series(self):
3727
# don't cast a DatetimeIndex WITH a tz, leave as object
3828
# GH 6032
@@ -101,12 +91,6 @@ def test_convert_dti_to_series(self):
10191
df.pop("ts")
10292
tm.assert_frame_equal(df, expected)
10393

104-
def test_set_columns(self, float_string_frame):
105-
cols = Index(np.arange(len(float_string_frame.columns)))
106-
float_string_frame.columns = cols
107-
with pytest.raises(ValueError, match="Length mismatch"):
108-
float_string_frame.columns = cols[::2]
109-
11094
def test_dti_set_index_reindex(self):
11195
# GH 6631
11296
df = DataFrame(np.random.random(6))

pandas/tests/generic/methods/test_set_axis.py

+22
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,28 @@ def test_set_axis_invalid_axis_name(self, axis, obj):
5757
with pytest.raises(ValueError, match="No axis named"):
5858
obj.set_axis(list("abc"), axis=axis)
5959

60+
def test_set_axis_setattr_index_not_collection(self, obj):
61+
# wrong type
62+
msg = (
63+
r"Index\(\.\.\.\) must be called with a collection of some "
64+
r"kind, None was passed"
65+
)
66+
with pytest.raises(TypeError, match=msg):
67+
obj.index = None
68+
69+
def test_set_axis_setattr_index_wrong_length(self, obj):
70+
# wrong length
71+
msg = (
72+
f"Length mismatch: Expected axis has {len(obj)} elements, "
73+
f"new values have {len(obj)-1} elements"
74+
)
75+
with pytest.raises(ValueError, match=msg):
76+
obj.index = np.arange(len(obj) - 1)
77+
78+
if obj.ndim == 2:
79+
with pytest.raises(ValueError, match="Length mismatch"):
80+
obj.columns = obj.columns[::2]
81+
6082

6183
class TestDataFrameSetAxis(SharedSetAxisTests):
6284
@pytest.fixture
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
from datetime import datetime
2+
3+
from pandas import Series
4+
5+
6+
class TestSetName:
7+
def test_set_name(self):
8+
ser = Series([1, 2, 3])
9+
ser2 = ser._set_name("foo")
10+
assert ser2.name == "foo"
11+
assert ser.name is None
12+
assert ser is not ser2
13+
14+
def test_set_name_attribute(self):
15+
ser = Series([1, 2, 3])
16+
ser2 = Series([1, 2, 3], name="bar")
17+
for name in [7, 7.0, "name", datetime(2001, 1, 1), (1,), "\u05D0"]:
18+
ser.name = name
19+
assert ser.name == name
20+
ser2.name = name
21+
assert ser2.name == name

pandas/tests/series/test_alter_axes.py

-55
This file was deleted.

0 commit comments

Comments
 (0)