Skip to content

REGR: DataFrame.update emits spurious warning about downcasting #57485

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v2.2.1.rst
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ Fixed regressions
- Fixed regression in :meth:`DataFrame.to_dict` with ``orient='list'`` and datetime or timedelta types returning integers (:issue:`54824`)
- Fixed regression in :meth:`DataFrame.to_json` converting nullable integers to floats (:issue:`57224`)
- Fixed regression in :meth:`DataFrame.to_sql` when ``method="multi"`` is passed and the dialect type is not Oracle (:issue:`57310`)
- Fixed regression in :meth:`DataFrame.update` emitting incorrect warnings about downcasting (:issue:`57124`)
- Fixed regression in :meth:`DataFrameGroupBy.idxmin`, :meth:`DataFrameGroupBy.idxmax`, :meth:`SeriesGroupBy.idxmin`, :meth:`SeriesGroupBy.idxmax` ignoring the ``skipna`` argument (:issue:`57040`)
- 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`)
- Fixed regression in :meth:`ExtensionArray.to_numpy` raising for non-numeric masked dtypes (:issue:`56991`)
Expand Down
13 changes: 12 additions & 1 deletion pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -8962,6 +8962,7 @@ def update(
1 2 500.0
2 3 6.0
"""

if not PYPY and using_copy_on_write():
if sys.getrefcount(self) <= REF_COUNT:
warnings.warn(
Expand Down Expand Up @@ -9010,7 +9011,17 @@ def update(
if mask.all():
continue

self.loc[:, col] = self[col].where(mask, that)
with warnings.catch_warnings():
warnings.filterwarnings(
"ignore",
message="Downcasting behavior",
category=FutureWarning,
)
# GH#57124 - `that` might get upcasted because of NA values, and then
# downcasted in where because of the mask. Ignoring the warning
# is a stopgap, will replace with a new implementation of update
# in 3.0.
self.loc[:, col] = self[col].where(mask, that)

# ----------------------------------------------------------------------
# Data reshaping
Expand Down
12 changes: 7 additions & 5 deletions pandas/tests/frame/methods/test_update.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,18 @@ def test_update(self):
def test_update_dtypes(self):
# gh 3016
df = DataFrame(
[[1.0, 2.0, False, True], [4.0, 5.0, True, False]],
columns=["A", "B", "bool1", "bool2"],
[[1.0, 2.0, 1, False, True], [4.0, 5.0, 2, True, False]],
columns=["A", "B", "int", "bool1", "bool2"],
)

other = DataFrame([[45, 45]], index=[0], columns=["A", "B"])
other = DataFrame(
[[45, 45, 3, True]], index=[0], columns=["A", "B", "int", "bool1"]
)
df.update(other)

expected = DataFrame(
[[45.0, 45.0, False, True], [4.0, 5.0, True, False]],
columns=["A", "B", "bool1", "bool2"],
[[45.0, 45.0, 3, True, True], [4.0, 5.0, 2, True, False]],
columns=["A", "B", "int", "bool1", "bool2"],
)
tm.assert_frame_equal(df, expected)

Expand Down