Skip to content

REGR: fix inplace operations for EAs with non-EA arg #37986

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 15 commits into from
Nov 24, 2020
Merged
Show file tree
Hide file tree
Changes from 2 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
7 changes: 6 additions & 1 deletion pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
is_datetime64_any_dtype,
is_datetime64tz_dtype,
is_dict_like,
is_dtype_equal,
is_extension_array_dtype,
is_float,
is_list_like,
Expand Down Expand Up @@ -11266,7 +11267,11 @@ def _inplace_method(self, other, op):
"""
result = op(self, other)

if self.ndim == 1 and result._indexed_same(self) and result.dtype == self.dtype:
if (
self.ndim == 1
and result._indexed_same(self)
and is_dtype_equal(result.dtype, self.dtype)
):
# GH#36498 this inplace op can _actually_ be inplace.
self._values[:] = result._values
return self
Expand Down
60 changes: 60 additions & 0 deletions pandas/tests/series/methods/test_inplace_ops.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import pytest

from pandas import Series
import pandas._testing as tm


@pytest.mark.parametrize(
"ser1, ser2, expected_add, expected_sub, expected_mul",
(
[
Series([1], dtype="Int64"),
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 do the Series construction inside the test and just have the dtypes in the parameters?

Copy link
Member Author

Choose a reason for hiding this comment

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

Done

Series([2], dtype="Int64"),
Series([3], dtype="Int64"),
Series([-1], dtype="Int64"),
Series([2], dtype="Int64"),
],
[
Series([1], dtype="float"),
Series([2.0], dtype="float"),
Series([3.0], dtype="float"),
Series([-1.0], dtype="float"),
Series([2.0], dtype="float"),
],
[
Series([1], dtype="Int64"),
Series([2.0], dtype="float"),
Series([3.0], dtype="float"),
Series([-1.0], dtype="float"),
Series([2], dtype="float"),
],
[
Series([1.0], dtype="float"),
Series([2], dtype="Int64"),
Series([3.0], dtype="float"),
Series([-1.0], dtype="float"),
Series([2], dtype="float"),
],
pytest.param(
Series([1], dtype="Int64"),
Series([2.0], dtype="Float64"),
Series([3.0], dtype="Float64"),
Series([-1.0], dtype="Float64"),
Series([2], dtype="Float64"),
marks=pytest.mark.xfail(reason="Not implemented yet"),
),
),
)
def test_series_inplace_ops(ser1, ser2, expected_add, expected_sub, expected_mul):

Copy link
Member

Choose a reason for hiding this comment

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

GH ref to either this PR or original issue

Copy link
Member Author

Choose a reason for hiding this comment

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

Done

res = ser1.copy()
res += ser2
tm.assert_series_equal(res, expected_add)

res = ser1.copy()
res -= ser2
tm.assert_series_equal(res, expected_sub)

res = ser1.copy()
res *= ser2
tm.assert_series_equal(res, expected_mul)