Skip to content

BUG: setting categorical values into object dtype DataFrame #39136

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 4 commits into from
Jan 14, 2021
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: 2 additions & 0 deletions doc/source/whatsnew/v1.3.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,8 @@ Categorical
- Bug in ``CategoricalIndex.reindex`` failed when ``Index`` passed with elements all in category (:issue:`28690`)
- Bug where constructing a :class:`Categorical` from an object-dtype array of ``date`` objects did not round-trip correctly with ``astype`` (:issue:`38552`)
- Bug in constructing a :class:`DataFrame` from an ``ndarray`` and a :class:`CategoricalDtype` (:issue:`38857`)
- Bug in :meth:`DataFrame.reindex` was throwing ``IndexError`` when new index contained duplicates and old index was :class:`CategoricalIndex` (:issue:`38906`)
- Bug in setting categorical values into an object-dtype column in a :class:`DataFrame` (:issue:`39136`)
- Bug in :meth:`DataFrame.reindex` was raising ``IndexError`` when new index contained duplicates and old index was :class:`CategoricalIndex` (:issue:`38906`)

Datetimelike
Expand Down
8 changes: 7 additions & 1 deletion pandas/core/internals/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -959,7 +959,13 @@ def setitem(self, indexer, value):
# GH25495 - If the current dtype is not categorical,
# we need to create a new categorical block
values[indexer] = value
return self.make_block(Categorical(self.values, dtype=arr_value.dtype))
if values.ndim == 2:
# TODO(EA2D): special case not needed with 2D EAs
if values.shape[-1] != 1:
# shouldn't get here (at least until 2D EAs)
raise NotImplementedError
values = values[:, 0]
return self.make_block(Categorical(values, dtype=arr_value.dtype))

elif exact_match and is_ea_value:
# GH#32395 if we're going to replace the values entirely, just
Expand Down
44 changes: 44 additions & 0 deletions pandas/tests/indexing/test_iloc.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,50 @@ def test_iloc_getitem_list_int(self):
class TestiLocBaseIndependent:
"""Tests Independent Of Base Class"""

@pytest.mark.parametrize(
"key",
[
slice(None),
slice(3),
range(3),
[0, 1, 2],
Index(range(3)),
np.asarray([0, 1, 2]),
],
)
@pytest.mark.parametrize("indexer", [tm.loc, tm.iloc])
def test_iloc_setitem_fullcol_categorical(self, indexer, key):
frame = DataFrame({0: range(3)}, dtype=object)

cat = Categorical(["alpha", "beta", "gamma"])
expected = DataFrame({0: cat})
# NB: pending GH#38896, the expected likely should become
# expected= DataFrame({"A": cat.astype(object)})
# and should remain a view on the original values

assert frame._mgr.blocks[0]._can_hold_element(cat)

df = frame.copy()
orig_vals = df.values
indexer(df)[key, 0] = cat

overwrite = not isinstance(key, slice)

tm.assert_frame_equal(df, expected)

# TODO: this inconsistency is likely undesired GH#39986
if overwrite:
# check that we overwrote underlying
tm.assert_numpy_array_equal(orig_vals, df.values)

# but we don't have a view on orig_vals
orig_vals[0, 0] = 19
assert df.iloc[0, 0] != 19

# check we dont have a view on cat (may be undesired GH#39986)
df.iloc[0, 0] = "gamma"
assert cat[0] != "gamma"

@pytest.mark.parametrize("box", [pd_array, Series])
def test_iloc_setitem_ea_inplace(self, frame_or_series, box):
# GH#38952 Case with not setting a full column
Expand Down