Skip to content

Commit 8e4b65c

Browse files
committed
inplace update
1 parent 5ad9abd commit 8e4b65c

File tree

3 files changed

+22
-11
lines changed

3 files changed

+22
-11
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

+8-10
Original file line numberDiff line numberDiff line change
@@ -8838,14 +8838,14 @@ def update(
88388838
in the original dataframe.
88398839
88408840
>>> df = pd.DataFrame({'A': [1, 2, 3],
8841-
... 'B': [400, 500, 600]})
8841+
... 'B': [400., 500., 600.]})
88428842
>>> new_df = pd.DataFrame({'B': [4, np.nan, 6]})
88438843
>>> df.update(new_df)
88448844
>>> df
8845-
A B
8846-
0 1 4
8847-
1 2 500
8848-
2 3 6
8845+
A B
8846+
0 1 4.0
8847+
1 2 500.0
8848+
2 3 6.0
88498849
"""
88508850
if not PYPY and using_copy_on_write():
88518851
if sys.getrefcount(self) <= REF_COUNT:
@@ -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)