Skip to content

Commit 110ff56

Browse files
committed
ENH: add inplace-kwarg to df.update
1 parent 25e6a21 commit 110ff56

File tree

3 files changed

+30
-7
lines changed

3 files changed

+30
-7
lines changed

doc/source/whatsnew/v0.24.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@ Other Enhancements
183183
- :class:`Series` and :class:`DataFrame` now support :class:`Iterable` in constructor (:issue:`2193`)
184184
- :class:`DatetimeIndex` gained :attr:`DatetimeIndex.timetz` attribute. Returns local time with timezone information. (:issue:`21358`)
185185
- :class:`Resampler` now is iterable like :class:`GroupBy` (:issue:`15314`).
186+
- :meth:`DataFrame.update` has gained an ``inplace``-kwarg, which defaults to True (same behavior as before). (:issue:`21858`)
186187
- :ref:`Series.resample` and :ref:`DataFrame.resample` have gained the :meth:`Resampler.quantile` (:issue:`15023`).
187188

188189
.. _whatsnew_0240.api_breaking:

pandas/core/frame.py

+17-4
Original file line numberDiff line numberDiff line change
@@ -5078,7 +5078,7 @@ def combiner(x, y, needs_i8_conversion=False):
50785078
return self.combine(other, combiner, overwrite=False)
50795079

50805080
def update(self, other, join='left', overwrite=True, filter_func=None,
5081-
raise_conflict=False):
5081+
raise_conflict=False, inplace=True):
50825082
"""
50835083
Modify in place using non-NA values from another DataFrame.
50845084
@@ -5108,6 +5108,11 @@ def update(self, other, join='left', overwrite=True, filter_func=None,
51085108
raise_conflict : bool, default False
51095109
If True, will raise a ValueError if the DataFrame and `other`
51105110
both contain non-NA data in the same place.
5111+
inplace : bool, default True
5112+
If True, updates the DataFrame inplace. If False, returns a new
5113+
DataFrame.
5114+
5115+
.. versionadded:: 0.24.0
51115116
51125117
Raises
51135118
------
@@ -5189,8 +5194,13 @@ def update(self, other, join='left', overwrite=True, filter_func=None,
51895194

51905195
other = other.reindex_like(self)
51915196

5192-
for col in self.columns:
5193-
this = self[col].values
5197+
if inplace:
5198+
frame = self
5199+
else:
5200+
frame = self.copy()
5201+
5202+
for col in frame.columns:
5203+
this = frame[col].values
51945204
that = other[col].values
51955205
if filter_func is not None:
51965206
with np.errstate(all='ignore'):
@@ -5211,7 +5221,10 @@ def update(self, other, join='left', overwrite=True, filter_func=None,
52115221
if mask.all():
52125222
continue
52135223

5214-
self[col] = expressions.where(mask, this, that)
5224+
frame[col] = expressions.where(mask, this, that)
5225+
5226+
if not inplace:
5227+
return frame
52155228

52165229
# ----------------------------------------------------------------------
52175230
# Data reshaping

pandas/tests/frame/test_combine_concat.py

+12-3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
from datetime import datetime
66

7+
import pytest
8+
79
import numpy as np
810
from numpy import nan
911

@@ -198,7 +200,8 @@ def test_append_dtypes(self):
198200
expected = DataFrame({'bar': Series([Timestamp('20130101'), 1])})
199201
assert_frame_equal(result, expected)
200202

201-
def test_update(self):
203+
@pytest.mark.parametrize('inplace', [True, False])
204+
def test_update(self, inplace):
202205
df = DataFrame([[1.5, nan, 3.],
203206
[1.5, nan, 3.],
204207
[1.5, nan, 3],
@@ -207,13 +210,19 @@ def test_update(self):
207210
other = DataFrame([[3.6, 2., np.nan],
208211
[np.nan, np.nan, 7]], index=[1, 3])
209212

210-
df.update(other)
213+
if inplace:
214+
df.update(other, inplace=inplace)
215+
result = df
216+
else:
217+
orig = df.copy()
218+
result = df.update(other, inplace=inplace)
219+
assert_frame_equal(df, orig)
211220

212221
expected = DataFrame([[1.5, nan, 3],
213222
[3.6, 2, 3],
214223
[1.5, nan, 3],
215224
[1.5, nan, 7.]])
216-
assert_frame_equal(df, expected)
225+
assert_frame_equal(result, expected)
217226

218227
def test_update_dtypes(self):
219228

0 commit comments

Comments
 (0)