Skip to content

Commit 6016720

Browse files
committed
BUG: Have object dtype for empty Categorical ctor
Previously we had a `Float64Index`, which is inconsistent with, e.g., the regular Index constructor.
1 parent 0fafd4f commit 6016720

File tree

3 files changed

+17
-1
lines changed

3 files changed

+17
-1
lines changed

doc/source/whatsnew/v0.21.0.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,9 @@ Numeric
379379
Categorical
380380
^^^^^^^^^^^
381381
- Bug in :func:`Series.isin` when called with a categorical (:issue`16639`)
382+
- Bug in the categorical constructor with empty values and categories causing
383+
the ``.categories`` to be an empty ``Float64Index`` rather than an empty
384+
``Index`` with object dtype (:issue:`17248`)
382385

383386

384387
Other

pandas/core/categorical.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,10 @@ def __init__(self, values, categories=None, ordered=False, fastpath=False):
290290
# On list with NaNs, int values will be converted to float. Use
291291
# "object" dtype to prevent this. In the end objects will be
292292
# casted to int/... in the category assignment step.
293-
dtype = 'object' if isna(values).any() else None
293+
if len(values) == 0 or isna(values).any():
294+
dtype = 'object'
295+
else:
296+
dtype = None
294297
values = _sanitize_array(values, None, dtype=dtype)
295298

296299
if categories is None:

pandas/tests/test_categorical.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,16 @@ def test_setitem_listlike(self):
112112
result = c.codes[np.array([100000]).astype(np.int64)]
113113
tm.assert_numpy_array_equal(result, np.array([5], dtype='int8'))
114114

115+
def test_constructor_empty(self):
116+
# GH 17248
117+
c = Categorical([])
118+
expected = Index([])
119+
tm.assert_index_equal(c.categories, expected)
120+
121+
c = Categorical([], categories=[1, 2, 3])
122+
expected = pd.Int64Index([1, 2, 3])
123+
tm.assert_index_equal(c.categories, expected)
124+
115125
def test_constructor_unsortable(self):
116126

117127
# it works!

0 commit comments

Comments
 (0)