Skip to content

Commit c5bd38d

Browse files
Backport PR #36444: BUG: inconsistent replace (#36658)
Co-authored-by: Number42 <[email protected]>
1 parent 52f8b9a commit c5bd38d

File tree

3 files changed

+30
-1
lines changed

3 files changed

+30
-1
lines changed

doc/source/whatsnew/v1.1.3.rst

+1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ Fixed regressions
3434
- Fixed regression in :meth:`Series.__getitem__` incorrectly raising when the input was a tuple (:issue:`35534`)
3535
- Fixed regression in :meth:`Series.__getitem__` incorrectly raising when the input was a frozenset (:issue:`35747`)
3636
- Fixed regression in :meth:`read_excel` with ``engine="odf"`` caused ``UnboundLocalError`` in some cases where cells had nested child nodes (:issue:`36122`, :issue:`35802`)
37+
- Fixed regression in :meth:`DataFrame.replace` inconsistent replace when using a float in the replace method (:issue:`35376`)
3738
- Fixed regression in :class:`DataFrame` and :class:`Series` comparisons between numeric arrays and strings (:issue:`35700`, :issue:`36377`)
3839
- Fixed regression in :meth:`DataFrame.apply` with ``raw=True`` and user-function returning string (:issue:`35940`)
3940
- Fixed regression when setting empty :class:`DataFrame` column to a :class:`Series` in preserving name of index in frame (:issue:`36527`)

pandas/core/internals/blocks.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
is_datetime64tz_dtype,
3737
is_dtype_equal,
3838
is_extension_array_dtype,
39+
is_float,
3940
is_float_dtype,
4041
is_integer,
4142
is_integer_dtype,
@@ -1996,7 +1997,9 @@ def _can_hold_element(self, element: Any) -> bool:
19961997
and not issubclass(tipo.type, (np.datetime64, np.timedelta64))
19971998
and self.dtype.itemsize >= tipo.itemsize
19981999
)
1999-
return is_integer(element)
2000+
# We have not inferred an integer from the dtype
2001+
# check if we have a builtin int or a float equal to an int
2002+
return is_integer(element) or (is_float(element) and element.is_integer())
20002003

20012004

20022005
class DatetimeLikeBlockMixin:

pandas/tests/frame/methods/test_replace.py

+25
Original file line numberDiff line numberDiff line change
@@ -974,6 +974,31 @@ def test_replace_for_new_dtypes(self, datetime_frame):
974974
}
975975
),
976976
),
977+
# GH 35376
978+
(
979+
DataFrame([[1, 1.0], [2, 2.0]]),
980+
1.0,
981+
5,
982+
DataFrame([[5, 5.0], [2, 2.0]]),
983+
),
984+
(
985+
DataFrame([[1, 1.0], [2, 2.0]]),
986+
1,
987+
5,
988+
DataFrame([[5, 5.0], [2, 2.0]]),
989+
),
990+
(
991+
DataFrame([[1, 1.0], [2, 2.0]]),
992+
1.0,
993+
5.0,
994+
DataFrame([[5, 5.0], [2, 2.0]]),
995+
),
996+
(
997+
DataFrame([[1, 1.0], [2, 2.0]]),
998+
1,
999+
5.0,
1000+
DataFrame([[5, 5.0], [2, 2.0]]),
1001+
),
9771002
],
9781003
)
9791004
def test_replace_dtypes(self, frame, to_replace, value, expected):

0 commit comments

Comments
 (0)