Skip to content

Commit 62422e1

Browse files
author
tp
committed
BUG: Copy categorical codes if empty (fixes pandas-dev#18051)
1 parent 151c40c commit 62422e1

File tree

3 files changed

+16
-1
lines changed

3 files changed

+16
-1
lines changed

doc/source/whatsnew/v0.21.1.txt

+1
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ Categorical
130130
- Error messages in the testing module have been improved when items have
131131
different ``CategoricalDtype`` (:issue:`18069`)
132132
- ``CategoricalIndex`` can now correctly take a ``pd.api.types.CategoricalDtype`` as its dtype (:issue:`18116`)
133+
- Bug in ``Categorical.unique()`` returning read-only ``codes`` array when all categories were ``NaN`` (:issue:`18051`)
133134

134135
Other
135136
^^^^^

pandas/core/categorical.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -2255,7 +2255,7 @@ def _recode_for_categories(codes, old_categories, new_categories):
22552255

22562256
if len(old_categories) == 0:
22572257
# All null anyway, so just retain the nulls
2258-
return codes
2258+
return codes.copy()
22592259
indexer = coerce_indexer_dtype(new_categories.get_indexer(old_categories),
22602260
new_categories)
22612261
new_codes = take_1d(indexer, codes.copy(), fill_value=-1)

pandas/tests/series/test_analytics.py

+14
Original file line numberDiff line numberDiff line change
@@ -848,6 +848,12 @@ def test_value_counts_nunique(self):
848848
result = series.nunique()
849849
assert result == 11
850850

851+
# GH 18051
852+
s = pd.Series(pd.Categorical([]))
853+
assert s.nunique() == 0
854+
s = pd.Series(pd.Categorical([np.nan]))
855+
assert s.nunique() == 0
856+
851857
def test_unique(self):
852858

853859
# 714 also, dtype=float
@@ -873,6 +879,14 @@ def test_unique(self):
873879
expected = np.array([1, 2, 3, None], dtype=object)
874880
tm.assert_numpy_array_equal(result, expected)
875881

882+
# GH 18051
883+
s = pd.Series(pd.Categorical([]))
884+
tm.assert_categorical_equal(s.unique(), pd.Categorical([]),
885+
check_dtype=False)
886+
s = pd.Series(pd.Categorical([np.nan]))
887+
tm.assert_categorical_equal(s.unique(), pd.Categorical([np.nan]),
888+
check_dtype=False)
889+
876890
@pytest.mark.parametrize(
877891
"tc1, tc2",
878892
[

0 commit comments

Comments
 (0)