Skip to content

Commit 0ae4dfd

Browse files
authored
BUG: DataFrame.update not operating in-place for datetime64[ns, UTC] dtype (#56228)
* inplace update * copy-on-write fixups * Update doc/source/whatsnew/v2.2.0.rst
1 parent d377cc9 commit 0ae4dfd

File tree

3 files changed

+23
-9
lines changed

3 files changed

+23
-9
lines changed

doc/source/whatsnew/v2.2.0.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -521,7 +521,7 @@ Indexing
521521

522522
Missing
523523
^^^^^^^
524-
-
524+
- Bug in :meth:`DataFrame.update` wasn't updating in-place for tz-aware datetime64 dtypes (:issue:`56227`)
525525
-
526526

527527
MultiIndex

pandas/core/frame.py

+6-8
Original file line numberDiff line numberDiff line change
@@ -8852,14 +8852,14 @@ def update(
88528852
in the original dataframe.
88538853
88548854
>>> df = pd.DataFrame({'A': [1, 2, 3],
8855-
... 'B': [400, 500, 600]})
8855+
... 'B': [400., 500., 600.]})
88568856
>>> new_df = pd.DataFrame({'B': [4, np.nan, 6]})
88578857
>>> df.update(new_df)
88588858
>>> df
8859-
A B
8860-
0 1 4
8861-
1 2 500
8862-
2 3 6
8859+
A B
8860+
0 1 4.0
8861+
1 2 500.0
8862+
2 3 6.0
88638863
"""
88648864
if not PYPY and using_copy_on_write():
88658865
if sys.getrefcount(self) <= REF_COUNT:
@@ -8876,8 +8876,6 @@ def update(
88768876
stacklevel=2,
88778877
)
88788878

8879-
from pandas.core.computation import expressions
8880-
88818879
# TODO: Support other joins
88828880
if join != "left": # pragma: no cover
88838881
raise NotImplementedError("Only left join is supported")
@@ -8911,7 +8909,7 @@ def update(
89118909
if mask.all():
89128910
continue
89138911

8914-
self.loc[:, col] = expressions.where(mask, this, that)
8912+
self.loc[:, col] = self[col].where(mask, that)
89158913

89168914
# ----------------------------------------------------------------------
89178915
# Data reshaping

pandas/tests/frame/methods/test_update.py

+16
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,22 @@ 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, warn_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+
with tm.assert_produces_warning(
149+
FutureWarning if warn_copy_on_write else None, match="Setting a value"
150+
):
151+
result.update(result + pd.Timedelta(days=1))
152+
expected = DataFrame([pd.Timestamp("2019-01-02", tz="UTC")])
153+
tm.assert_frame_equal(result, expected)
154+
if not using_copy_on_write:
155+
tm.assert_frame_equal(view, expected)
156+
else:
157+
tm.assert_frame_equal(view, orig)
158+
143159
def test_update_with_different_dtype(self, using_copy_on_write):
144160
# GH#3217
145161
df = DataFrame({"a": [1, 3], "b": [np.nan, 2]})

0 commit comments

Comments
 (0)