Skip to content

Fix and test scalar extension dtype op corner case #22378

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
Aug 18, 2018
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
4 changes: 2 additions & 2 deletions pandas/core/ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -1228,8 +1228,8 @@ def wrapper(left, right):
"{op}".format(typ=type(left).__name__, op=str_rep))

elif (is_extension_array_dtype(left) or
is_extension_array_dtype(right)):
# TODO: should this include `not is_scalar(right)`?
(is_extension_array_dtype(right) and not is_scalar(right))):
Copy link
Member

Choose a reason for hiding this comment

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

Is checking that it is a string a bit more logical?

Copy link
Member Author

Choose a reason for hiding this comment

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

I guess that would work. Elsewhere the same check is done with not is_scalar

Copy link
Member

Choose a reason for hiding this comment

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

then ignore my comment and go for consistency

# GH#22378 disallow scalar to exclude e.g. "category", "Int64"
return dispatch_to_extension_op(op, left, right)

elif is_datetime64_dtype(left) or is_datetime64tz_dtype(left):
Expand Down
16 changes: 16 additions & 0 deletions pandas/tests/arithmetic/test_object.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,22 @@ def test_more_na_comparisons(self, dtype):

class TestArithmetic(object):

@pytest.mark.parametrize("op", [operator.add, ops.radd])
@pytest.mark.parametrize("other", ["category", "Int64"])
def test_add_extension_scalar(self, other, box, op):
# GH#22378
# Check that scalars satisfying is_extension_array_dtype(obj)
# do not incorrectly try to dispatch to an ExtensionArray operation

arr = pd.Series(['a', 'b', 'c'])
expected = pd.Series([op(x, other) for x in arr])

arr = tm.box_expected(arr, box)
expected = tm.box_expected(expected, box)

result = op(arr, other)
tm.assert_equal(result, expected)

@pytest.mark.parametrize('box', [
pytest.param(pd.Index,
marks=pytest.mark.xfail(reason="Does not mask nulls",
Expand Down