Skip to content

Commit

Permalink
BUG: Have object dtype for empty Categorical ctor
Browse files Browse the repository at this point in the history
Previously we had a `Float64Index`, which is inconsistent with, e.g., the
regular Index constructor.
  • Loading branch information
TomAugspurger committed Aug 14, 2017
1 parent 0fafd4f commit 6016720
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
3 changes: 3 additions & 0 deletions doc/source/whatsnew/v0.21.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,9 @@ Numeric
Categorical
^^^^^^^^^^^
- Bug in :func:`Series.isin` when called with a categorical (:issue`16639`)
- Bug in the categorical constructor with empty values and categories causing
the ``.categories`` to be an empty ``Float64Index`` rather than an empty
``Index`` with object dtype (:issue:`17248`)


Other
Expand Down
5 changes: 4 additions & 1 deletion pandas/core/categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,10 @@ def __init__(self, values, categories=None, ordered=False, fastpath=False):
# On list with NaNs, int values will be converted to float. Use
# "object" dtype to prevent this. In the end objects will be
# casted to int/... in the category assignment step.
dtype = 'object' if isna(values).any() else None
if len(values) == 0 or isna(values).any():
dtype = 'object'
else:
dtype = None
values = _sanitize_array(values, None, dtype=dtype)

if categories is None:
Expand Down
10 changes: 10 additions & 0 deletions pandas/tests/test_categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,16 @@ def test_setitem_listlike(self):
result = c.codes[np.array([100000]).astype(np.int64)]
tm.assert_numpy_array_equal(result, np.array([5], dtype='int8'))

def test_constructor_empty(self):
# GH 17248
c = Categorical([])
expected = Index([])
tm.assert_index_equal(c.categories, expected)

c = Categorical([], categories=[1, 2, 3])
expected = pd.Int64Index([1, 2, 3])
tm.assert_index_equal(c.categories, expected)

def test_constructor_unsortable(self):

# it works!
Expand Down

0 comments on commit 6016720

Please sign in to comment.