Skip to content

Commit fc7e38a

Browse files
committed
add test for .loc indexing Index type preservation
1 parent 0de9955 commit fc7e38a

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

pandas/tests/frame/test_indexing.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1944,6 +1944,29 @@ def test_set_value_resize(self, float_frame):
19441944
with pytest.raises(ValueError, match=msg):
19451945
res3._set_value("foobar", "baz", "sam")
19461946

1947+
def test_loc_indexing_preserves_index_dtype(self):
1948+
# GH 15166
1949+
df = DataFrame(
1950+
data=np.arange(2, 22, 2),
1951+
index=pd.MultiIndex(
1952+
levels=[pd.CategoricalIndex(["a", "b"]), range(10)],
1953+
codes=[[0] * 5 + [1] * 5, range(10)],
1954+
names=["Index1", "Index2"],
1955+
),
1956+
)
1957+
1958+
result_1 = df.index.levels[0]
1959+
result_2 = df.loc[["a"]].index.levels[0]
1960+
expected = pd.CategoricalIndex(
1961+
["a", "b"],
1962+
categories=["a", "b"],
1963+
ordered=False,
1964+
name="Index1",
1965+
dtype="category",
1966+
)
1967+
tm.assert_index_equal(result_1, expected)
1968+
tm.assert_index_equal(result_2, expected)
1969+
19471970
def test_set_value_with_index_dtype_change(self):
19481971
df_orig = DataFrame(np.random.randn(3, 3), index=range(3), columns=list("ABC"))
19491972

0 commit comments

Comments
 (0)