Skip to content

Commit a3097b5

Browse files
authored
BUG: Fixed strange behaviour of pd.DataFrame.drop() with inplace argu… (#30501)
1 parent 8a5f291 commit a3097b5

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

doc/source/whatsnew/v1.1.0.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,7 @@ Reshaping
411411
- Bug in :func:`concat` where the resulting indices are not copied when ``copy=True`` (:issue:`29879`)
412412
- :meth:`Series.append` will now raise a ``TypeError`` when passed a DataFrame or a sequence containing Dataframe (:issue:`31413`)
413413
- :meth:`DataFrame.replace` and :meth:`Series.replace` will raise a ``TypeError`` if ``to_replace`` is not an expected type. Previously the ``replace`` would fail silently (:issue:`18634`)
414+
- Bug on inplace operation of a Series that was adding a column to the DataFrame from where it was originally dropped from (using inplace=True) (:issue:`30484`)
414415
- Bug in :meth:`DataFrame.apply` where callback was called with :class:`Series` parameter even though ``raw=True`` requested. (:issue:`32423`)
415416
- Bug in :meth:`DataFrame.pivot_table` losing timezone information when creating a :class:`MultiIndex` level from a column with timezone-aware dtype (:issue:`32558`)
416417
- Bug in :meth:`concat` where when passing a non-dict mapping as ``objs`` would raise a ``TypeError`` (:issue:`32863`)

pandas/core/ops/methods.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,8 @@ def _wrap_inplace_method(method):
9393

9494
def f(self, other):
9595
result = method(self, other)
96-
96+
# Delete cacher
97+
self._reset_cacher()
9798
# this makes sure that we are aligned like the input
9899
# we are updating inplace so we want to ignore is_copy
99100
self._update_inplace(

pandas/tests/frame/test_axis_select_reindex.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -716,3 +716,24 @@ def test_reindex_multi_categorical_time(self):
716716
result = df2.reindex(midx)
717717
expected = pd.DataFrame({"a": [0, 1, 2, 3, 4, 5, 6, np.nan, 8]}, index=midx)
718718
tm.assert_frame_equal(result, expected)
719+
720+
@pytest.mark.parametrize(
721+
"operation", ["__iadd__", "__isub__", "__imul__", "__ipow__"]
722+
)
723+
@pytest.mark.parametrize("inplace", [False, True])
724+
def test_inplace_drop_and_operation(self, operation, inplace):
725+
# GH 30484
726+
df = pd.DataFrame({"x": range(5)})
727+
expected = df.copy()
728+
df["y"] = range(5)
729+
y = df["y"]
730+
731+
with tm.assert_produces_warning(None):
732+
if inplace:
733+
df.drop("y", axis=1, inplace=inplace)
734+
else:
735+
df = df.drop("y", axis=1, inplace=inplace)
736+
737+
# Perform operation and check result
738+
getattr(y, operation)(1)
739+
tm.assert_frame_equal(df, expected)

0 commit comments

Comments
 (0)