Skip to content
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
Contributor 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
Contributor 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)