Skip to content

FIX: REGR: setting numeric value in Categorical Series with enlargement raise internal error #47751

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

Closed
wants to merge 12 commits into from
Closed
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.5.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -962,6 +962,7 @@ Indexing
- Bug in :meth:`DataFrame.sum` min_count changes dtype if input contains NaNs (:issue:`46947`)
- Bug in :class:`IntervalTree` that lead to an infinite recursion. (:issue:`46658`)
- Bug in :class:`PeriodIndex` raising ``AttributeError`` when indexing on ``NA``, rather than putting ``NaT`` in its place. (:issue:`46673`)
- Bug in :meth:`DataFrame.loc` when enlarging a :class:`Series` with dtype :class:`CategoricalDtype` with a scalar (:issue:`47677`)
-

Missing
Expand Down
10 changes: 9 additions & 1 deletion pandas/core/dtypes/cast.py
Original file line number Diff line number Diff line change
Expand Up @@ -591,7 +591,9 @@ def _maybe_promote(dtype: np.dtype, fill_value=np.nan):
return dtype, fv

elif isna(fill_value):
dtype = _dtype_obj
# preserve dtype in case of categoricaldtype
if not isinstance(dtype, CategoricalDtype):
dtype = _dtype_obj
if fill_value is None:
# but we retain e.g. pd.NA
fill_value = np.nan
Expand Down Expand Up @@ -646,6 +648,12 @@ def _maybe_promote(dtype: np.dtype, fill_value=np.nan):

return np.dtype("object"), fill_value

elif isinstance(dtype, CategoricalDtype):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you move this before the isna check and add the Categorical handling of nan values into this block?

Keeps the Categorical specific code together

Ohterwise this looks good

if fill_value in dtype.categories:
return dtype, fill_value
else:
return object, ensure_object(fill_value)

elif is_float(fill_value):
if issubclass(dtype.type, np.bool_):
dtype = np.dtype(np.object_)
Expand Down
41 changes: 41 additions & 0 deletions pandas/tests/indexing/test_loc.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import pandas as pd
from pandas import (
Categorical,
CategoricalDtype,
CategoricalIndex,
DataFrame,
DatetimeIndex,
Expand Down Expand Up @@ -1820,6 +1821,46 @@ def test_loc_getitem_sorted_index_level_with_duplicates(self):
result = df.loc[("foo", "bar")]
tm.assert_frame_equal(result, expected)

def test_additional_element_to_categorical_series_loc(self):
# GH#47677
result = Series(["a", "b", "c"], dtype="category")
result.loc[3] = 0
expected = Series(["a", "b", "c", 0], dtype="object")
tm.assert_series_equal(result, expected)

def test_additional_categorical_element_loc(self):
# GH#47677
result = Series(["a", "b", "c"], dtype="category")
result.loc[3] = "a"
expected = Series(["a", "b", "c", "a"], dtype="category")
tm.assert_series_equal(result, expected)

def test_loc_set_nan_in_categorical_series(self, any_numeric_ea_dtype):
# GH#47677
srs = Series(
[1, 2, 3],
dtype=CategoricalDtype(Index([1, 2, 3], dtype=any_numeric_ea_dtype)),
)
# enlarge
srs.loc[3] = np.nan
assert srs.values.dtype._categories.dtype == any_numeric_ea_dtype
# set into
srs.loc[1] = np.nan
assert srs.values.dtype._categories.dtype == any_numeric_ea_dtype

@pytest.mark.parametrize("na", (np.nan, pd.NA, None))
def test_loc_consistency_series_enlarge_set_into(self, na):
# GH#47677
srs_enlarge = Series(["a", "b", "c"], dtype="category")
srs_enlarge.loc[3] = na

srs_setinto = Series(["a", "b", "c", "a"], dtype="category")
srs_setinto.loc[3] = na

tm.assert_series_equal(srs_enlarge, srs_setinto)
expected = Series(["a", "b", "c", na], dtype="category")
tm.assert_series_equal(srs_enlarge, expected)

def test_loc_getitem_preserves_index_level_category_dtype(self):
# GH#15166
df = DataFrame(
Expand Down