Skip to content

Commit 55ababc

Browse files
author
rjfs
committed
BUG: Fixed strange behaviour of pd.DataFrame.drop() with inplace argument (pandas-dev#30484)
1 parent 710df21 commit 55ababc

File tree

2 files changed

+21
-42
lines changed

2 files changed

+21
-42
lines changed

pandas/core/ops/methods.py

-42
Original file line numberDiff line numberDiff line change
@@ -83,48 +83,6 @@ def add_special_arithmetic_methods(cls):
8383
new_methods = _create_methods(
8484
cls, arith_method, comp_method, bool_method, special=True
8585
)
86-
# inplace operators (I feel like these should get passed an `inplace=True`
87-
# or just be removed
88-
89-
def _wrap_inplace_method(method):
90-
"""
91-
return an inplace wrapper for this method
92-
"""
93-
94-
def f(self, other):
95-
result = method(self, other)
96-
97-
# this makes sure that we are aligned like the input
98-
# we are updating inplace so we want to ignore is_copy
99-
self._update_inplace(
100-
result.reindex_like(self, copy=False)._data, verify_is_copy=False
101-
)
102-
103-
return self
104-
105-
f.__name__ = "__i{name}__".format(name=method.__name__.strip("__"))
106-
return f
107-
108-
new_methods.update(
109-
dict(
110-
__iadd__=_wrap_inplace_method(new_methods["__add__"]),
111-
__isub__=_wrap_inplace_method(new_methods["__sub__"]),
112-
__imul__=_wrap_inplace_method(new_methods["__mul__"]),
113-
__itruediv__=_wrap_inplace_method(new_methods["__truediv__"]),
114-
__ifloordiv__=_wrap_inplace_method(new_methods["__floordiv__"]),
115-
__imod__=_wrap_inplace_method(new_methods["__mod__"]),
116-
__ipow__=_wrap_inplace_method(new_methods["__pow__"]),
117-
)
118-
)
119-
120-
new_methods.update(
121-
dict(
122-
__iand__=_wrap_inplace_method(new_methods["__and__"]),
123-
__ior__=_wrap_inplace_method(new_methods["__or__"]),
124-
__ixor__=_wrap_inplace_method(new_methods["__xor__"]),
125-
)
126-
)
127-
12886
_add_methods(cls, new_methods=new_methods)
12987

13088

pandas/tests/base/test_ops.py

+21
Original file line numberDiff line numberDiff line change
@@ -767,6 +767,27 @@ def test_drop_duplicates_series_vs_dataframe(self):
767767
dropped_series = df[column].drop_duplicates(keep=keep)
768768
tm.assert_frame_equal(dropped_frame, dropped_series.to_frame())
769769

770+
def test_inplace_drop_and_add(self):
771+
# GH 30484
772+
# Get expected df
773+
expected = pd.DataFrame({})
774+
expected["x1"] = [1, 2, 3, 4, 5]
775+
expected["x2"] = [0, 0, 0, 1, 1]
776+
expected["target"] = [10, 20, 30, 40, 50]
777+
y = expected["target"]
778+
expected.drop("target", axis=1, inplace=True)
779+
y = y + np.min(y)
780+
# Get tested df
781+
df = pd.DataFrame({})
782+
df["x1"] = [1, 2, 3, 4, 5]
783+
df["x2"] = [0, 0, 0, 1, 1]
784+
df["target"] = [10, 20, 30, 40, 50]
785+
y = df["target"]
786+
df.drop("target", axis=1, inplace=True)
787+
y += np.min(y)
788+
# compare
789+
tm.assert_frame_equal(df, expected)
790+
770791
def test_fillna(self):
771792
# # GH 11343
772793
# though Index.fillna and Series.fillna has separate impl,

0 commit comments

Comments
 (0)