Skip to content

Commit 626efcc

Browse files
committed
inplace update
1 parent 5ad9abd commit 626efcc

File tree

3 files changed

+17
-6
lines changed

3 files changed

+17
-6
lines changed

doc/source/whatsnew/v2.2.0.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -520,7 +520,7 @@ Indexing
520520

521521
Missing
522522
^^^^^^^
523-
-
523+
- Bug in :meth:`DataFrame.update` wasn't updating in-place for datetime64 dtypes (:issue:`56227`)
524524
-
525525

526526
MultiIndex

pandas/core/frame.py

+3-5
Original file line numberDiff line numberDiff line change
@@ -8862,8 +8862,6 @@ def update(
88628862
stacklevel=2,
88638863
)
88648864

8865-
from pandas.core.computation import expressions
8866-
88678865
# TODO: Support other joins
88688866
if join != "left": # pragma: no cover
88698867
raise NotImplementedError("Only left join is supported")
@@ -8876,8 +8874,8 @@ def update(
88768874
other = other.reindex(self.index)
88778875

88788876
for col in self.columns.intersection(other.columns):
8879-
this = self[col]._values
8880-
that = other[col]._values
8877+
this = self[col]
8878+
that = other[col]
88818879

88828880
if filter_func is not None:
88838881
mask = ~filter_func(this) | isna(that)
@@ -8897,7 +8895,7 @@ def update(
88978895
if mask.all():
88988896
continue
88998897

8900-
self.loc[:, col] = expressions.where(mask, this, that)
8898+
self.loc[:, col] = this.where(mask, that)
89018899

89028900
# ----------------------------------------------------------------------
89038901
# Data reshaping

pandas/tests/frame/methods/test_update.py

+13
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,19 @@ def test_update_datetime_tz(self):
140140
expected = DataFrame([pd.Timestamp("2019", tz="UTC")])
141141
tm.assert_frame_equal(result, expected)
142142

143+
def test_update_datetime_tz_in_place(self, using_copy_on_write):
144+
# https://github.com/pandas-dev/pandas/issues/56227
145+
result = DataFrame([pd.Timestamp("2019", tz="UTC")])
146+
orig = result.copy()
147+
view = result[:]
148+
result.update(result + pd.Timedelta(days=1))
149+
expected = DataFrame([pd.Timestamp("2019-01-02", tz="UTC")])
150+
tm.assert_frame_equal(result, expected)
151+
if not using_copy_on_write:
152+
tm.assert_frame_equal(view, expected)
153+
else:
154+
tm.assert_frame_equal(view, orig)
155+
143156
def test_update_with_different_dtype(self, using_copy_on_write):
144157
# GH#3217
145158
df = DataFrame({"a": [1, 3], "b": [np.nan, 2]})

0 commit comments

Comments
 (0)