Skip to content

Commit 933c600

Browse files
committed
ENH: add inplace-kwarg to df.update
1 parent 0370740 commit 933c600

File tree

3 files changed

+24
-6
lines changed

3 files changed

+24
-6
lines changed

doc/source/whatsnew/v0.24.0.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,7 @@ Other API Changes
468468
- :meth:`PeriodIndex.tz_convert` and :meth:`PeriodIndex.tz_localize` have been removed (:issue:`21781`)
469469
- :class:`Index` subtraction will attempt to operate element-wise instead of raising ``TypeError`` (:issue:`19369`)
470470
- :class:`pandas.io.formats.style.Styler` supports a ``number-format`` property when using :meth:`~pandas.io.formats.style.Styler.to_excel` (:issue:`22015`)
471+
- :meth:~DataFrame.update has gained an ``inplace``-kwarg, which defaults to True (same behavior as before).
471472

472473
.. _whatsnew_0240.deprecations:
473474

pandas/core/frame.py

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5184,7 +5184,7 @@ def combiner(x, y, needs_i8_conversion=False):
51845184
return self.combine(other, combiner, overwrite=False)
51855185

51865186
def update(self, other, join='left', overwrite=True, filter_func=None,
5187-
raise_conflict=False):
5187+
raise_conflict=False, inplace=True):
51885188
"""
51895189
Modify in place using non-NA values from another DataFrame.
51905190
@@ -5214,6 +5214,9 @@ def update(self, other, join='left', overwrite=True, filter_func=None,
52145214
raise_conflict : bool, default False
52155215
If True, will raise a ValueError if the DataFrame and `other`
52165216
both contain non-NA data in the same place.
5217+
inplace : bool, default True
5218+
If True, follows the convention by ``dict`` of updating inplace. If
5219+
False, returns a new object.
52175220
52185221
Raises
52195222
------
@@ -5295,8 +5298,13 @@ def update(self, other, join='left', overwrite=True, filter_func=None,
52955298

52965299
other = other.reindex_like(self)
52975300

5298-
for col in self.columns:
5299-
this = self[col].values
5301+
if inplace:
5302+
frame = self
5303+
else:
5304+
frame = self.copy()
5305+
5306+
for col in frame.columns:
5307+
this = frame[col].values
53005308
that = other[col].values
53015309
if filter_func is not None:
53025310
with np.errstate(all='ignore'):
@@ -5317,7 +5325,10 @@ def update(self, other, join='left', overwrite=True, filter_func=None,
53175325
if mask.all():
53185326
continue
53195327

5320-
self[col] = expressions.where(mask, this, that)
5328+
frame[col] = expressions.where(mask, this, that)
5329+
5330+
if not inplace:
5331+
return frame
53215332

53225333
# ----------------------------------------------------------------------
53235334
# Data reshaping

pandas/tests/frame/test_combine_concat.py

Lines changed: 8 additions & 2 deletions
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,7 +210,10 @@ 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+
else:
216+
df = df.update(other, inplace=inplace)
211217

212218
expected = DataFrame([[1.5, nan, 3],
213219
[3.6, 2, 3],

0 commit comments

Comments
 (0)