Skip to content

Commit ef4ab0f

Browse files
Backport PR #34048 on branch 1.0.x (Bug in DataFrame.replace casts columns to object dtype if items in to_replace not in values) (#34115)
1 parent 82c5ce2 commit ef4ab0f

File tree

3 files changed

+9
-5
lines changed

3 files changed

+9
-5
lines changed

doc/source/whatsnew/v1.0.4.rst

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ Fixed regressions
2323
- Bug where an ordered :class:`Categorical` containing only ``NaN`` values would raise rather than returning ``NaN`` when taking the minimum or maximum (:issue:`33450`)
2424
- Bug in :meth:`DataFrameGroupBy.agg` with dictionary input losing ``ExtensionArray`` dtypes (:issue:`32194`)
2525
- Fix to preserve the ability to index with the "nearest" method with xarray's CFTimeIndex, an :class:`Index` subclass (`pydata/xarray#3751 <https://github.com/pydata/xarray/issues/3751>`_, :issue:`32905`).
26+
- Bug in :meth:`DataFrame.replace` casts columns to ``object`` dtype if items in ``to_replace`` not in values (:issue:`32988`)
2627
-
2728

2829
.. _whatsnew_104.bug_fixes:

pandas/core/internals/blocks.py

-5
Original file line numberDiff line numberDiff line change
@@ -765,11 +765,6 @@ def replace(
765765
filtered_out = ~self.mgr_locs.isin(filter)
766766
mask[filtered_out.nonzero()[0]] = False
767767

768-
if not mask.any():
769-
if inplace:
770-
return [self]
771-
return [self.copy()]
772-
773768
try:
774769
blocks = self.putmask(mask, value, inplace=inplace)
775770
# Note: it is _not_ the case that self._can_hold_element(value)

pandas/tests/frame/methods/test_replace.py

+8
Original file line numberDiff line numberDiff line change
@@ -1363,3 +1363,11 @@ def test_replace_after_convert_dtypes(self):
13631363
result = df.replace(1, 10)
13641364
expected = pd.DataFrame({"grp": [10, 2, 3, 4, 5]}, dtype="Int64")
13651365
tm.assert_frame_equal(result, expected)
1366+
1367+
@pytest.mark.parametrize("dtype", ["float", "float64", "int64", "Int64", "boolean"])
1368+
@pytest.mark.parametrize("value", [np.nan, pd.NA])
1369+
def test_replace_no_replacement_dtypes(self, dtype, value):
1370+
# https://github.com/pandas-dev/pandas/issues/32988
1371+
df = pd.DataFrame(np.eye(2), dtype=dtype)
1372+
result = df.replace(to_replace=[None, -np.inf, np.inf], value=value)
1373+
tm.assert_frame_equal(result, df)

0 commit comments

Comments
 (0)