Skip to content

Commit db4dbb1

Browse files
committed
move test to frame/test_alter_axes.py
1 parent b097a32 commit db4dbb1

File tree

2 files changed

+46
-42
lines changed

2 files changed

+46
-42
lines changed

pandas/tests/frame/test_alter_axes.py

+46-15
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,12 @@
1212

1313
from pandas import (
1414
Categorical,
15+
CategoricalIndex,
1516
DataFrame,
1617
DatetimeIndex,
1718
Index,
1819
IntervalIndex,
20+
MultiIndex,
1921
Series,
2022
Timestamp,
2123
cut,
@@ -171,21 +173,6 @@ def test_assign_columns(self, float_frame):
171173
tm.assert_series_equal(float_frame["C"], df["baz"], check_names=False)
172174
tm.assert_series_equal(float_frame["hi"], df["foo2"], check_names=False)
173175

174-
def test_set_index_preserve_categorical_dtype(self):
175-
# GH13743, GH13854
176-
df = DataFrame(
177-
{
178-
"A": [1, 2, 1, 1, 2],
179-
"B": [10, 16, 22, 28, 34],
180-
"C1": Categorical(list("abaab"), categories=list("bac"), ordered=False),
181-
"C2": Categorical(list("abaab"), categories=list("bac"), ordered=True),
182-
}
183-
)
184-
for cols in ["C1", "C2", ["A", "C1"], ["A", "C2"], ["C1", "C2"]]:
185-
result = df.set_index(cols).reset_index()
186-
result = result.reindex(columns=df.columns)
187-
tm.assert_frame_equal(result, df)
188-
189176
def test_rename_signature(self):
190177
sig = inspect.signature(DataFrame.rename)
191178
parameters = set(sig.parameters)
@@ -266,3 +253,47 @@ def test_set_reset_index(self):
266253
df = df.set_index("B")
267254

268255
df = df.reset_index()
256+
257+
258+
class TestCategoricalIndex:
259+
def test_set_index_preserve_categorical_dtype(self):
260+
# GH13743, GH13854
261+
df = DataFrame(
262+
{
263+
"A": [1, 2, 1, 1, 2],
264+
"B": [10, 16, 22, 28, 34],
265+
"C1": Categorical(list("abaab"), categories=list("bac"), ordered=False),
266+
"C2": Categorical(list("abaab"), categories=list("bac"), ordered=True),
267+
}
268+
)
269+
for cols in ["C1", "C2", ["A", "C1"], ["A", "C2"], ["C1", "C2"]]:
270+
result = df.set_index(cols).reset_index()
271+
result = result.reindex(columns=df.columns)
272+
tm.assert_frame_equal(result, df)
273+
274+
@pytest.mark.parametrize(
275+
"codes", ([[0, 0, 1, 1], [0, 1, 0, 1]], [[0, 0, -1, 1], [0, 1, 0, 1]])
276+
)
277+
def test_reindexing_with_missing_values(self, codes):
278+
# GH 24206
279+
280+
index = MultiIndex(
281+
[CategoricalIndex(["A", "B"]), CategoricalIndex(["a", "b"])], codes
282+
)
283+
data = {"col": range(len(index))}
284+
df = DataFrame(data=data, index=index)
285+
286+
expected = DataFrame(
287+
{
288+
"level_0": Categorical.from_codes(codes[0], categories=["A", "B"]),
289+
"level_1": Categorical.from_codes(codes[1], categories=["a", "b"]),
290+
"col": range(4),
291+
}
292+
)
293+
294+
res = df.reset_index()
295+
tm.assert_frame_equal(res, expected)
296+
297+
# roundtrip
298+
res = expected.set_index(["level_0", "level_1"]).reset_index()
299+
tm.assert_frame_equal(res, expected)

pandas/tests/indexing/test_categorical.py

-27
Original file line numberDiff line numberDiff line change
@@ -650,33 +650,6 @@ def test_loc_slice(self):
650650
expected = self.df.iloc[[2, 3, 4]]
651651
tm.assert_frame_equal(result, expected)
652652

653-
@pytest.mark.parametrize(
654-
"codes", ([[0, 0, 1, 1], [0, 1, 0, 1]], [[0, 0, -1, 1], [0, 1, 0, 1]])
655-
)
656-
def test_reindexing_with_missing_values(self, codes):
657-
# GH 24206
658-
659-
index = pd.MultiIndex(
660-
[pd.CategoricalIndex(["A", "B"]), pd.CategoricalIndex(["a", "b"])], codes
661-
)
662-
data = {"col": range(len(index))}
663-
df = DataFrame(data=data, index=index)
664-
665-
expected = pd.DataFrame(
666-
{
667-
"level_0": pd.Categorical.from_codes(codes[0], categories=["A", "B"]),
668-
"level_1": pd.Categorical.from_codes(codes[1], categories=["a", "b"]),
669-
"col": range(4),
670-
}
671-
)
672-
673-
res = df.reset_index()
674-
tm.assert_frame_equal(res, expected)
675-
676-
# roundtrip
677-
res = expected.set_index(["level_0", "level_1"]).reset_index()
678-
tm.assert_frame_equal(res, expected)
679-
680653
def test_loc_and_at_with_categorical_index(self):
681654
# GH 20629
682655
s = Series([1, 2, 3], index=pd.CategoricalIndex(["A", "B", "C"]))

0 commit comments

Comments
 (0)