Skip to content

Commit 2c1e981

Browse files
authored
TST: add test to verify column does not lose categorical type when using loc (pandas-dev#37988)
1 parent 25a1d91 commit 2c1e981

File tree

1 file changed

+8
-0
lines changed

1 file changed

+8
-0
lines changed

pandas/tests/indexing/test_loc.py

+8
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
import pandas as pd
1414
from pandas import (
15+
Categorical,
1516
CategoricalIndex,
1617
DataFrame,
1718
Index,
@@ -1319,6 +1320,13 @@ def test_loc_setitem_datetime_keys_cast(self):
13191320
expected = DataFrame({"one": [100.0, 200.0]}, index=[dt1, dt2])
13201321
tm.assert_frame_equal(df, expected)
13211322

1323+
def test_loc_setitem_categorical_column_retains_dtype(self, ordered):
1324+
# GH16360
1325+
result = DataFrame({"A": [1]})
1326+
result.loc[:, "B"] = Categorical(["b"], ordered=ordered)
1327+
expected = DataFrame({"A": [1], "B": Categorical(["b"], ordered=ordered)})
1328+
tm.assert_frame_equal(result, expected)
1329+
13221330

13231331
class TestLocCallable:
13241332
def test_frame_loc_getitem_callable(self):

0 commit comments

Comments
 (0)