Skip to content

Commit b45325e

Browse files
topper-123jreback
authored andcommitted
BUG: Copy categorical codes if empty (fixes pandas-dev#18051) (pandas-dev#18436)
1 parent 41004d9 commit b45325e

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
@@ -136,6 +136,7 @@ Categorical
136136
- Bug in :meth:`DataFrame.astype` where casting to 'category' on an empty ``DataFrame`` causes a segmentation fault (:issue:`18004`)
137137
- Error messages in the testing module have been improved when items have different ``CategoricalDtype`` (:issue:`18069`)
138138
- ``CategoricalIndex`` can now correctly take a ``pd.api.types.CategoricalDtype`` as its dtype (:issue:`18116`)
139+
- Bug in ``Categorical.unique()`` returning read-only ``codes`` array when all categories were ``NaN`` (:issue:`18051`)
139140

140141
Other
141142
^^^^^

pandas/core/categorical.py

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

22772277
if len(old_categories) == 0:
22782278
# All null anyway, so just retain the nulls
2279-
return codes
2279+
return codes.copy()
22802280
indexer = coerce_indexer_dtype(new_categories.get_indexer(old_categories),
22812281
new_categories)
22822282
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)