Skip to content

BUG: categorical-with-nas to float64 instead of object #45359

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 6 commits into from
Jan 16, 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.5.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ Groupby/resample/rolling

Reshaping
^^^^^^^^^
-
- Bug in :func:`concat` between a :class:`Series` with integer dtype and another with :class:`CategoricalDtype` with integer categories and containing ``NaN`` values casting to object dtype instead of ``float64`` (:issue:`45359`)
-

Sparse
Expand Down
20 changes: 9 additions & 11 deletions pandas/core/dtypes/concat.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
)

if TYPE_CHECKING:
from pandas import Categorical
from pandas.core.arrays.sparse import SparseArray


Expand All @@ -40,17 +41,14 @@ def cast_to_common_type(arr: ArrayLike, dtype: DtypeObj) -> ArrayLike:
"""
if is_dtype_equal(arr.dtype, dtype):
return arr
if (
is_categorical_dtype(arr.dtype)
and isinstance(dtype, np.dtype)
and np.issubdtype(dtype, np.integer)
):
# problem case: categorical of int -> gives int as result dtype,
# but categorical can contain NAs -> fall back to object dtype
try:
return arr.astype(dtype, copy=False)
except ValueError:
return arr.astype(object, copy=False)

if isinstance(dtype, np.dtype) and dtype.kind in ["i", "u"]:

if is_categorical_dtype(arr.dtype) and cast("Categorical", arr)._hasnans:
# problem case: categorical of int -> gives int as result dtype,
# but categorical can contain NAs -> float64 instead
# GH#45359
dtype = np.dtype(np.float64)

if is_sparse(arr) and not is_sparse(dtype):
# problem case: SparseArray.astype(dtype) doesn't follow the specified
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/indexes/category.py
Original file line number Diff line number Diff line change
Expand Up @@ -576,7 +576,7 @@ def _concat(self, to_concat: list[Index], name: Hashable) -> Index:
# not all to_concat elements are among our categories (or NA)
from pandas.core.dtypes.concat import concat_compat

res = concat_compat(to_concat)
res = concat_compat([x._values for x in to_concat])
return Index(res, name=name)
else:
cat = self._data._from_backing_data(codes)
Expand Down
10 changes: 5 additions & 5 deletions pandas/tests/reshape/concat/test_append_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -508,7 +508,7 @@ def test_concat_categorical(self):
s1 = Series([10, 11, np.nan], dtype="category")
s2 = Series([np.nan, 1, 3, 2], dtype="category")

exp = Series([10, 11, np.nan, np.nan, 1, 3, 2], dtype="object")
exp = Series([10, 11, np.nan, np.nan, 1, 3, 2], dtype=np.float64)
tm.assert_series_equal(pd.concat([s1, s2], ignore_index=True), exp)
tm.assert_series_equal(s1._append(s2, ignore_index=True), exp)

Expand All @@ -529,12 +529,12 @@ def test_concat_categorical_coercion(self):
s1 = Series([1, 2, np.nan], dtype="category")
s2 = Series([2, 1, 2])

exp = Series([1, 2, np.nan, 2, 1, 2], dtype="object")
exp = Series([1, 2, np.nan, 2, 1, 2], dtype=np.float64)
tm.assert_series_equal(pd.concat([s1, s2], ignore_index=True), exp)
tm.assert_series_equal(s1._append(s2, ignore_index=True), exp)

# result shouldn't be affected by 1st elem dtype
exp = Series([2, 1, 2, 1, 2, np.nan], dtype="object")
exp = Series([2, 1, 2, 1, 2, np.nan], dtype=np.float64)
tm.assert_series_equal(pd.concat([s2, s1], ignore_index=True), exp)
tm.assert_series_equal(s2._append(s1, ignore_index=True), exp)

Expand All @@ -554,11 +554,11 @@ def test_concat_categorical_coercion(self):
s1 = Series([10, 11, np.nan], dtype="category")
s2 = Series([1, 3, 2])

exp = Series([10, 11, np.nan, 1, 3, 2], dtype="object")
exp = Series([10, 11, np.nan, 1, 3, 2], dtype=np.float64)
tm.assert_series_equal(pd.concat([s1, s2], ignore_index=True), exp)
tm.assert_series_equal(s1._append(s2, ignore_index=True), exp)

exp = Series([1, 3, 2, 10, 11, np.nan], dtype="object")
exp = Series([1, 3, 2, 10, 11, np.nan], dtype=np.float64)
tm.assert_series_equal(pd.concat([s2, s1], ignore_index=True), exp)
tm.assert_series_equal(s2._append(s1, ignore_index=True), exp)

Expand Down