Skip to content

Commit b79fe7e

Browse files
authored
REGR: DataFrame.update emits spurious warning about downcasting (#57485)
1 parent ab8541c commit b79fe7e

File tree

3 files changed

+20
-6
lines changed

3 files changed

+20
-6
lines changed

doc/source/whatsnew/v2.2.1.rst

+1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ Fixed regressions
3232
- Fixed regression in :meth:`DataFrame.to_dict` with ``orient='list'`` and datetime or timedelta types returning integers (:issue:`54824`)
3333
- Fixed regression in :meth:`DataFrame.to_json` converting nullable integers to floats (:issue:`57224`)
3434
- Fixed regression in :meth:`DataFrame.to_sql` when ``method="multi"`` is passed and the dialect type is not Oracle (:issue:`57310`)
35+
- Fixed regression in :meth:`DataFrame.update` emitting incorrect warnings about downcasting (:issue:`57124`)
3536
- Fixed regression in :meth:`DataFrameGroupBy.idxmin`, :meth:`DataFrameGroupBy.idxmax`, :meth:`SeriesGroupBy.idxmin`, :meth:`SeriesGroupBy.idxmax` ignoring the ``skipna`` argument (:issue:`57040`)
3637
- Fixed regression in :meth:`DataFrameGroupBy.idxmin`, :meth:`DataFrameGroupBy.idxmax`, :meth:`SeriesGroupBy.idxmin`, :meth:`SeriesGroupBy.idxmax` where values containing the minimum or maximum value for the dtype could produce incorrect results (:issue:`57040`)
3738
- Fixed regression in :meth:`ExtensionArray.to_numpy` raising for non-numeric masked dtypes (:issue:`56991`)

pandas/core/frame.py

+12-1
Original file line numberDiff line numberDiff line change
@@ -8962,6 +8962,7 @@ def update(
89628962
1 2 500.0
89638963
2 3 6.0
89648964
"""
8965+
89658966
if not PYPY and using_copy_on_write():
89668967
if sys.getrefcount(self) <= REF_COUNT:
89678968
warnings.warn(
@@ -9010,7 +9011,17 @@ def update(
90109011
if mask.all():
90119012
continue
90129013

9013-
self.loc[:, col] = self[col].where(mask, that)
9014+
with warnings.catch_warnings():
9015+
warnings.filterwarnings(
9016+
"ignore",
9017+
message="Downcasting behavior",
9018+
category=FutureWarning,
9019+
)
9020+
# GH#57124 - `that` might get upcasted because of NA values, and then
9021+
# downcasted in where because of the mask. Ignoring the warning
9022+
# is a stopgap, will replace with a new implementation of update
9023+
# in 3.0.
9024+
self.loc[:, col] = self[col].where(mask, that)
90149025

90159026
# ----------------------------------------------------------------------
90169027
# Data reshaping

pandas/tests/frame/methods/test_update.py

+7-5
Original file line numberDiff line numberDiff line change
@@ -48,16 +48,18 @@ def test_update(self):
4848
def test_update_dtypes(self):
4949
# gh 3016
5050
df = DataFrame(
51-
[[1.0, 2.0, False, True], [4.0, 5.0, True, False]],
52-
columns=["A", "B", "bool1", "bool2"],
51+
[[1.0, 2.0, 1, False, True], [4.0, 5.0, 2, True, False]],
52+
columns=["A", "B", "int", "bool1", "bool2"],
5353
)
5454

55-
other = DataFrame([[45, 45]], index=[0], columns=["A", "B"])
55+
other = DataFrame(
56+
[[45, 45, 3, True]], index=[0], columns=["A", "B", "int", "bool1"]
57+
)
5658
df.update(other)
5759

5860
expected = DataFrame(
59-
[[45.0, 45.0, False, True], [4.0, 5.0, True, False]],
60-
columns=["A", "B", "bool1", "bool2"],
61+
[[45.0, 45.0, 3, True, True], [4.0, 5.0, 2, True, False]],
62+
columns=["A", "B", "int", "bool1", "bool2"],
6163
)
6264
tm.assert_frame_equal(df, expected)
6365

0 commit comments

Comments
 (0)