Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 12 additions & 10 deletions pandas/core/arrays/numeric.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
pandas_dtype,
)

import pandas as pd
from pandas.core.arrays.masked import (
BaseMaskedArray,
BaseMaskedDtype,
Expand Down Expand Up @@ -231,16 +232,17 @@ def _coerce_to_data_and_mask(
values = np.ones(values.shape, dtype=dtype)
else:
idx = np.nanargmax(values)
if int(values[idx]) != original[idx]:
# We have ints that lost precision during the cast.
inferred_type = lib.infer_dtype(original, skipna=True)
if (
inferred_type not in ["floating", "mixed-integer-float"]
and not mask.any()
):
values = np.asarray(original, dtype=dtype)
else:
values = np.asarray(original, dtype="object")
if not (pd.isna(values[idx]) or pd.isna(original[idx])):
if int(values[idx]) != original[idx]:
# We have ints that lost precision during the cast.
inferred_type = lib.infer_dtype(original, skipna=True)
if (
inferred_type not in ["floating", "mixed-integer-float"]
and not mask.any()
):
values = np.asarray(original, dtype=dtype)
else:
values = np.asarray(original, dtype="object")

# we copy as need to coerce here
if mask.any():
Expand Down
19 changes: 19 additions & 0 deletions pandas/tests/indexing/test_iloc.py
Original file line number Diff line number Diff line change
Expand Up @@ -1090,6 +1090,25 @@ def test_iloc_setitem_pure_position_based(self, indexer, has_ref):
expected = DataFrame({"a": [1, 2, 3], "b": [11, 12, 13], "c": [7, 8, 9]})
tm.assert_frame_equal(df2, expected)

def test_iloc_assignment_nullable_int_with_na(self):
# GH#62473
ser = Series(
[4, 6, 9, None, 10, 13, 15], index=[6, 1, 5, 0, 3, 2, 4], dtype="Int64"
)
indices = Series(
[6, 1, 5, 0, 3, 2, 4], index=[6, 1, 5, 0, 3, 2, 4], dtype="int64"
)
values = Series(
[4, 6, 9, None, 10, 13, 15], index=[4, 1, 2, 6, 0, 5, 3], dtype="Int64"
)

ser.iloc[indices] = values

expected = Series(
[NA, 6, 13, 10, 15, 9, 4], index=[6, 1, 5, 0, 3, 2, 4], dtype="Int64"
)
tm.assert_series_equal(ser, expected)

@pytest.mark.parametrize("has_ref", [True, False])
def test_iloc_setitem_dictionary_value(self, has_ref):
# GH#37728
Expand Down
Loading