Skip to content

Commit cbd1103

Browse files
authored
REF: set_axis tests (#33189)
1 parent f148cb7 commit cbd1103

File tree

3 files changed

+75
-68
lines changed

3 files changed

+75
-68
lines changed

pandas/tests/frame/test_alter_axes.py

-32
Original file line numberDiff line numberDiff line change
@@ -776,35 +776,3 @@ def test_set_reset_index(self):
776776
df = df.set_index("B")
777777

778778
df = df.reset_index()
779-
780-
def test_set_axis_inplace(self):
781-
# GH14636
782-
df = DataFrame(
783-
{"A": [1.1, 2.2, 3.3], "B": [5.0, 6.1, 7.2], "C": [4.4, 5.5, 6.6]},
784-
index=[2010, 2011, 2012],
785-
)
786-
787-
expected = {0: df.copy(), 1: df.copy()}
788-
expected[0].index = list("abc")
789-
expected[1].columns = list("abc")
790-
expected["index"] = expected[0]
791-
expected["columns"] = expected[1]
792-
793-
for axis in expected:
794-
result = df.copy()
795-
result.set_axis(list("abc"), axis=axis, inplace=True)
796-
tm.assert_frame_equal(result, expected[axis])
797-
798-
# inplace=False
799-
result = df.set_axis(list("abc"), axis=axis)
800-
tm.assert_frame_equal(expected[axis], result)
801-
802-
# omitting the "axis" parameter
803-
with tm.assert_produces_warning(None):
804-
result = df.set_axis(list("abc"))
805-
tm.assert_frame_equal(result, expected[0])
806-
807-
# wrong values for the "axis" parameter
808-
for axis in 3, "foo":
809-
with pytest.raises(ValueError, match="No axis named"):
810-
df.set_axis(list("abc"), axis=axis)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
import numpy as np
2+
import pytest
3+
4+
from pandas import DataFrame, Series
5+
import pandas._testing as tm
6+
7+
8+
class SharedSetAxisTests:
9+
@pytest.fixture
10+
def obj(self):
11+
raise NotImplementedError("Implemented by subclasses")
12+
13+
def test_set_axis(self, obj):
14+
# GH14636; this tests setting index for both Series and DataFrame
15+
new_index = list("abcd")[: len(obj)]
16+
17+
expected = obj.copy()
18+
expected.index = new_index
19+
20+
# inplace=False
21+
result = obj.set_axis(new_index, axis=0, inplace=False)
22+
tm.assert_equal(expected, result)
23+
24+
@pytest.mark.parametrize("axis", [0, "index", 1, "columns"])
25+
def test_set_axis_inplace_axis(self, axis, obj):
26+
# GH#14636
27+
if obj.ndim == 1 and axis in [1, "columns"]:
28+
# Series only has [0, "index"]
29+
return
30+
31+
new_index = list("abcd")[: len(obj)]
32+
33+
expected = obj.copy()
34+
if axis in [0, "index"]:
35+
expected.index = new_index
36+
else:
37+
expected.columns = new_index
38+
39+
result = obj.copy()
40+
result.set_axis(new_index, axis=axis, inplace=True)
41+
tm.assert_equal(result, expected)
42+
43+
def test_set_axis_unnamed_kwarg_warns(self, obj):
44+
# omitting the "axis" parameter
45+
new_index = list("abcd")[: len(obj)]
46+
47+
expected = obj.copy()
48+
expected.index = new_index
49+
50+
with tm.assert_produces_warning(None):
51+
result = obj.set_axis(new_index, inplace=False)
52+
tm.assert_equal(result, expected)
53+
54+
@pytest.mark.parametrize("axis", [3, "foo"])
55+
def test_set_axis_invalid_axis_name(self, axis, obj):
56+
# wrong values for the "axis" parameter
57+
with pytest.raises(ValueError, match="No axis named"):
58+
obj.set_axis(list("abc"), axis=axis)
59+
60+
61+
class TestDataFrameSetAxis(SharedSetAxisTests):
62+
@pytest.fixture
63+
def obj(self):
64+
df = DataFrame(
65+
{"A": [1.1, 2.2, 3.3], "B": [5.0, 6.1, 7.2], "C": [4.4, 5.5, 6.6]},
66+
index=[2010, 2011, 2012],
67+
)
68+
return df
69+
70+
71+
class TestSeriesSetAxis(SharedSetAxisTests):
72+
@pytest.fixture
73+
def obj(self):
74+
ser = Series(np.arange(4), index=[1, 3, 5, 7], dtype="int64")
75+
return ser

pandas/tests/series/test_alter_axes.py

-36
Original file line numberDiff line numberDiff line change
@@ -53,39 +53,3 @@ def test_set_index_makes_timeseries(self):
5353
s = Series(range(10))
5454
s.index = idx
5555
assert s.index.is_all_dates
56-
57-
def test_set_axis_inplace_axes(self, axis_series):
58-
# GH14636
59-
ser = Series(np.arange(4), index=[1, 3, 5, 7], dtype="int64")
60-
61-
expected = ser.copy()
62-
expected.index = list("abcd")
63-
64-
# inplace=True
65-
# The FutureWarning comes from the fact that we would like to have
66-
# inplace default to False some day
67-
result = ser.copy()
68-
result.set_axis(list("abcd"), axis=axis_series, inplace=True)
69-
tm.assert_series_equal(result, expected)
70-
71-
def test_set_axis_inplace(self):
72-
# GH14636
73-
74-
s = Series(np.arange(4), index=[1, 3, 5, 7], dtype="int64")
75-
76-
expected = s.copy()
77-
expected.index = list("abcd")
78-
79-
# inplace=False
80-
result = s.set_axis(list("abcd"), axis=0, inplace=False)
81-
tm.assert_series_equal(expected, result)
82-
83-
# omitting the "axis" parameter
84-
with tm.assert_produces_warning(None):
85-
result = s.set_axis(list("abcd"), inplace=False)
86-
tm.assert_series_equal(result, expected)
87-
88-
# wrong values for the "axis" parameter
89-
for axis in [2, "foo"]:
90-
with pytest.raises(ValueError, match="No axis named"):
91-
s.set_axis(list("abcd"), axis=axis, inplace=False)

0 commit comments

Comments
 (0)