8000 BUG: inconsistent replace (#36444) · pandas-dev/pandas@04e07d0 · GitHub
[go: up one dir, main page]

Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 04e07d0

Browse files
authored
BUG: inconsistent replace (#36444)
1 parent e088ea3 commit 04e07d0

File tree

3 files changed

+30
-1
lines changed

3 files changed

+30
-1
lines changed

doc/source/whatsnew/v1.1.3.rst

Lines changed: 1 addition & 0 deletions
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

Lines changed: 4 additions & 1 deletion
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,
@@ -2064,7 +2065,9 @@ def _can_hold_element(self, element: Any) -> bool:
20642065
and not issubclass(tipo.type, (np.datetime64, np.timedelta64))
20652066
and self.dtype.itemsize >= tipo.itemsize
20662067
)
2067-
return is_integer(element)
2068+
# We have not inferred an integer from the dtype
2069+
# check if we have a builtin int or a float equal to an int
2070+
return is_integer(element) or (is_float(element) and element.is_integer())
20682071

20692072

20702073
class DatetimeLikeBlockMixin:

pandas/tests/frame/methods/test_replace.py

Lines changed: 25 additions & 0 deletions
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)
0