Skip to content

Commit b73c38e

Browse files
BUG: Series[int].loc setitem with Series[int] results in Series[float] (#41644)
1 parent 9d77a56 commit b73c38e

File tree

3 files changed

+3
-9
lines changed

3 files changed

+3
-9
lines changed

pandas/core/dtypes/cast.py

+2
Original file line numberDiff line numberDiff line change
@@ -2166,6 +2166,8 @@ def can_hold_element(arr: ArrayLike, element: Any) -> bool:
21662166
if dtype.kind in ["i", "u"]:
21672167
if tipo is not None:
21682168
if tipo.kind not in ["i", "u"]:
2169+
if is_float(element) and element.is_integer():
2170+
return True
21692171
# Anything other than integer we cannot hold
21702172
return False
21712173
elif dtype.itemsize < tipo.itemsize:

pandas/core/internals/blocks.py

-8
Original file line numberDiff line numberDiff line change
@@ -781,14 +781,6 @@ def _replace_list(
781781
# so un-tile here
782782
return self.replace(src_list, dest_list[0], inplace, regex)
783783

784-
# https://github.com/pandas-dev/pandas/issues/40371
785-
# the following pairs check code caused a regression so we catch that case here
786-
# until the issue is fixed properly in can_hold_element
787-
788-
# error: "Iterable[Any]" has no attribute "tolist"
789-
if hasattr(src_list, "tolist"):
790-
src_list = src_list.tolist() # type: ignore[attr-defined]
791-
792784
# Exclude anything that we know we won't contain
793785
pairs = [
794786
(x, y) for x, y in zip(src_list, dest_list) if self._can_hold_element(x)

pandas/tests/series/indexing/test_setitem.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ def test_setitem_series_object_dtype(self, indexer, ser_index):
158158
expected = Series([Series([42], index=[ser_index]), 0], dtype="object")
159159
tm.assert_series_equal(ser, expected)
160160

161-
@pytest.mark.parametrize("index, exp_value", [(0, 42.0), (1, np.nan)])
161+
@pytest.mark.parametrize("index, exp_value", [(0, 42), (1, np.nan)])
162162
def test_setitem_series(self, index, exp_value):
163163
# GH#38303
164164
ser = Series([0, 0])

0 commit comments

Comments
 (0)