Skip to content

Commit 5aca87d

Browse files
topper-123TomAugspurger
authored andcommitted
BUG: Copy categorical codes if empty (fixes pandas-dev#18051) (pandas-dev#18436)
(cherry picked from commit b45325e)
1 parent 7f9dac8 commit 5aca87d

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
@@ -138,6 +138,7 @@ Categorical
138138
- Error messages in the testing module have been improved when items have
139139
different ``CategoricalDtype`` (:issue:`18069`)
140140
- ``CategoricalIndex`` can now correctly take a ``pd.api.types.CategoricalDtype`` as its dtype (:issue:`18116`)
141+
- Bug in ``Categorical.unique()`` returning read-only ``codes`` array when all categories were ``NaN`` (:issue:`18051`)
141142

142143
Other
143144
^^^^^

pandas/core/categorical.py

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

22692269
if len(old_categories) == 0:
22702270
# All null anyway, so just retain the nulls
2271-
return codes
2271+
return codes.copy()
22722272
indexer = coerce_indexer_dtype(new_categories.get_indexer(old_categories),
22732273
new_categories)
22742274
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
@@ -920,6 +926,14 @@ def test_drop_duplicates(self):
920926
sc.drop_duplicates(keep=False, inplace=True)
921927
assert_series_equal(sc, s[~expected])
922928

929+
# GH 18051
930+
s = pd.Series(pd.Categorical([]))
931+
tm.assert_categorical_equal(s.unique(), pd.Categorical([]),
932+
check_dtype=False)
933+
s = pd.Series(pd.Categorical([np.nan]))
934+
tm.assert_categorical_equal(s.unique(), pd.Categorical([np.nan]),
935+
check_dtype=False)
936+
923937
def test_clip(self):
924938
val = self.ts.median()
925939

0 commit comments

Comments
 (0)