Skip to content

BUG: add_categories losing dtype information #48847

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v1.6.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ Bug fixes

Categorical
^^^^^^^^^^^
-
- Bug in :meth:`Categorical.set_categories` losing dtype information (:issue:`48812`)
-

Datetimelike
Expand Down
19 changes: 17 additions & 2 deletions pandas/core/arrays/categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,10 @@
from pandas.util._exceptions import find_stack_level
from pandas.util._validators import validate_bool_kwarg

from pandas.core.dtypes.cast import coerce_indexer_dtype
from pandas.core.dtypes.cast import (
coerce_indexer_dtype,
find_common_type,
)
from pandas.core.dtypes.common import (
ensure_int64,
ensure_platform_int,
Expand Down Expand Up @@ -1292,7 +1295,19 @@ def add_categories(
raise ValueError(
f"new categories must not include old categories: {already_included}"
)
new_categories = list(self.dtype.categories) + list(new_categories)

if hasattr(new_categories, "dtype"):
from pandas import Series

dtype = find_common_type(
[self.dtype.categories.dtype, new_categories.dtype]
)
new_categories = Series(
list(self.dtype.categories) + list(new_categories), dtype=dtype
)
else:
new_categories = list(self.dtype.categories) + list(new_categories)

new_dtype = CategoricalDtype(new_categories, self.ordered)

cat = self if inplace else self.copy()
Expand Down
20 changes: 20 additions & 0 deletions pandas/tests/arrays/categorical/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
DataFrame,
Index,
Series,
StringDtype,
)
import pandas._testing as tm
from pandas.core.arrays.categorical import recode_for_categories
Expand Down Expand Up @@ -237,6 +238,25 @@ def test_add_categories_existing_raises(self):
with pytest.raises(ValueError, match=msg):
cat.add_categories(["d"])

def test_add_categories_losing_dtype_information(self):
# GH#48812
cat = Categorical(Series([1, 2], dtype="Int64"))
ser = Series([4], dtype="Int64")
result = cat.add_categories(ser)
expected = Categorical(
Series([1, 2], dtype="Int64"), categories=Series([1, 2, 4], dtype="Int64")
)
tm.assert_categorical_equal(result, expected)

cat = Categorical(Series(["a", "b", "a"], dtype=StringDtype()))
ser = Series(["d"], dtype=StringDtype())
result = cat.add_categories(ser)
expected = Categorical(
Series(["a", "b", "a"], dtype=StringDtype()),
categories=Series(["a", "b", "d"], dtype=StringDtype()),
)
tm.assert_categorical_equal(result, expected)

def test_set_categories(self):
cat = Categorical(["a", "b", "c", "a"], ordered=True)
exp_categories = Index(["c", "b", "a"])
Expand Down