Skip to content

BUG: Categorical setitem, comparison with tuple category #36623

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 3 commits into from
Oct 2, 2020
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.2.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ Bug fixes
Categorical
^^^^^^^^^^^
- :meth:`Categorical.fillna` will always return a copy, will validate a passed fill value regardless of whether there are any NAs to fill, and will disallow a ``NaT`` as a fill value for numeric categories (:issue:`36530`)
-
- Bug in :meth:`Categorical.__setitem__` that incorrectly raised when trying to set a tuple value (:issue:`20439`)
-

Datetimelike
Expand Down
11 changes: 7 additions & 4 deletions pandas/core/arrays/categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
is_dict_like,
is_dtype_equal,
is_extension_array_dtype,
is_hashable,
is_integer_dtype,
is_list_like,
is_object_dtype,
Expand Down Expand Up @@ -61,8 +62,9 @@ def _cat_compare_op(op):

@unpack_zerodim_and_defer(opname)
def func(self, other):
if is_list_like(other) and len(other) != len(self):
# TODO: Could this fail if the categories are listlike objects?
hashable = is_hashable(other)
if is_list_like(other) and len(other) != len(self) and not hashable:
# in hashable case we may have a tuple that is itself a category
raise ValueError("Lengths must match.")

if not self.ordered:
Expand Down Expand Up @@ -90,7 +92,7 @@ def func(self, other):
ret[mask] = fill_value
return ret

if is_scalar(other):
if hashable:
if other in self.categories:
i = self._unbox_scalar(other)
ret = op(self._codes, i)
Expand Down Expand Up @@ -1883,7 +1885,8 @@ def _validate_setitem_value(self, value):
new_codes = self._validate_listlike(value)
value = Categorical.from_codes(new_codes, dtype=self.dtype)

rvalue = value if is_list_like(value) else [value]
# wrap scalars and hashable-listlikes in list
rvalue = value if not is_hashable(value) else [value]
Copy link
Contributor

Choose a reason for hiding this comment

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

can use maybe_make_list (but certainly can followup as may need to change that definition slightly)


from pandas import Index

Expand Down
10 changes: 9 additions & 1 deletion pandas/tests/arrays/categorical/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,22 @@ def test_setitem_different_unordered_raises(self, other):
pd.Categorical(["b", "a"], categories=["a", "b", "c"], ordered=True),
],
)
def test_setitem_same_ordered_rasies(self, other):
def test_setitem_same_ordered_raises(self, other):
# Gh-24142
target = pd.Categorical(["a", "b"], categories=["a", "b"], ordered=True)
mask = np.array([True, False])
msg = "Cannot set a Categorical with another, without identical categories"
with pytest.raises(ValueError, match=msg):
target[mask] = other[mask]

def test_setitem_tuple(self):
# GH#20439
cat = pd.Categorical([(0, 1), (0, 2), (0, 1)])

# This should not raise
cat[1] = cat[0]
assert cat[1] == (0, 1)


class TestCategoricalIndexing:
def test_getitem_slice(self):
Expand Down
14 changes: 14 additions & 0 deletions pandas/tests/arrays/categorical/test_operators.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,20 @@ def test_comparison_with_unknown_scalars(self):
tm.assert_numpy_array_equal(cat == 4, np.array([False, False, False]))
tm.assert_numpy_array_equal(cat != 4, np.array([True, True, True]))

def test_comparison_with_tuple(self):
cat = pd.Categorical(np.array(["foo", (0, 1), 3, (0, 1)], dtype=object))

result = cat == "foo"
expected = np.array([True, False, False, False], dtype=bool)
tm.assert_numpy_array_equal(result, expected)

result = cat == (0, 1)
expected = np.array([False, True, False, True], dtype=bool)
tm.assert_numpy_array_equal(result, expected)

result = cat != (0, 1)
tm.assert_numpy_array_equal(result, ~expected)

def test_comparison_of_ordered_categorical_with_nan_to_scalar(
self, compare_operators_no_eq_ne
):
Expand Down