Skip to content

Commit de3df9c

Browse files
test: add for allow duplicate labels false
1 parent 2d73d62 commit de3df9c

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

pandas/tests/frame/methods/test_rename_axis.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,20 @@ def test_rename_axis_inplace(self, float_frame):
2828
assert no_return is None
2929
tm.assert_frame_equal(result, expected)
3030

31+
def test_rename_axis_with_allows_duplicate_labels_false(self):
32+
# GH#44958
33+
# Test that rename_axis() works correctly when allows_duplicate_labels=False
34+
df = DataFrame([[1, 2], [3, 4]], columns=["a", "b"]).set_flags(
35+
allows_duplicate_labels=False
36+
)
37+
38+
# Test renaming index (axis=0) with allows_duplicate_labels=False
39+
result = df.rename_axis("idx", axis=0)
40+
expected = DataFrame(
41+
[[1, 2], [3, 4]], index=Index([0, 1], name="idx"), columns=["a", "b"]
42+
).set_flags(allows_duplicate_labels=False)
43+
tm.assert_frame_equal(result, expected)
44+
3145
def test_rename_axis_raises(self):
3246
# GH#17833
3347
df = DataFrame({"A": [1, 2], "B": [1, 2]})

pandas/tests/frame/methods/test_set_axis.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,20 @@ def obj(self):
112112
)
113113
return df
114114

115+
def test_set_axis_with_allows_duplicate_labels_false(self):
116+
# GH#44958
117+
# Test that set_axis() works correctly when allows_duplicate_labels=False
118+
df = DataFrame([[1, 2], [3, 4]], columns=["a", "b"]).set_flags(
119+
allows_duplicate_labels=False
120+
)
121+
122+
# Test setting index with allows_duplicate_labels=False
123+
result = df.set_axis(labels=["x", "y"], axis=0)
124+
expected = DataFrame(
125+
[[1, 2], [3, 4]], index=["x", "y"], columns=["a", "b"]
126+
).set_flags(allows_duplicate_labels=False)
127+
tm.assert_frame_equal(result, expected)
128+
115129

116130
class TestSeriesSetAxis(SharedSetAxisTests):
117131
@pytest.fixture

0 commit comments

Comments
 (0)