Skip to content

Commit

Permalink
REGR: DataFrame.update emits spurious warning about downcasting (pand…
Browse files Browse the repository at this point in the history
  • Loading branch information
rhshadrach authored Feb 18, 2024
1 parent ab8541c commit b79fe7e
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 6 deletions.
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

0 comments on commit b79fe7e

Please sign in to comment.