Skip to content

Commit 9199791

Browse files
Backport PR pandas-dev#34048 on branch 1.0.x (Bug in DataFrame.replace casts columns to object dtype if items in to_replace not in values)
1 parent 64f206f commit 9199791

File tree

3 files changed

+20
-5
lines changed

3 files changed

+20
-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

+19
Original file line numberDiff line numberDiff line change
@@ -1363,3 +1363,22 @@ 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+
def test_replace_invalid_to_replace(self):
1368+
# GH 18634
1369+
# API: replace() should raise an exception if invalid argument is given
1370+
df = pd.DataFrame({"one": ["a", "b ", "c"], "two": ["d ", "e ", "f "]})
1371+
msg = (
1372+
r"Expecting 'to_replace' to be either a scalar, array-like, "
1373+
r"dict or None, got invalid type.*"
1374+
)
1375+
with pytest.raises(TypeError, match=msg):
1376+
df.replace(lambda x: x.strip())
1377+
1378+
@pytest.mark.parametrize("dtype", ["float", "float64", "int64", "Int64", "boolean"])
1379+
@pytest.mark.parametrize("value", [np.nan, pd.NA])
1380+
def test_replace_no_replacement_dtypes(self, dtype, value):
1381+
# https://github.com/pandas-dev/pandas/issues/32988
1382+
df = pd.DataFrame(np.eye(2), dtype=dtype)
1383+
result = df.replace(to_replace=[None, -np.inf, np.inf], value=value)
1384+
tm.assert_frame_equal(result, df)

0 commit comments

Comments
 (0)