Skip to content

Commit e5ea65d

Browse files
committed
BUG: Fix boolean value of NA is ambiguous in iloc assignment for nullable integer Series
1 parent 1863adb commit e5ea65d

File tree

2 files changed

+31
-10
lines changed

2 files changed

+31
-10
lines changed

pandas/core/arrays/numeric.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
pandas_dtype,
2525
)
2626

27+
import pandas as pd
2728
from pandas.core.arrays.masked import (
2829
BaseMaskedArray,
2930
BaseMaskedDtype,
@@ -231,16 +232,17 @@ def _coerce_to_data_and_mask(
231232
values = np.ones(values.shape, dtype=dtype)
232233
else:
233234
idx = np.nanargmax(values)
234-
if int(values[idx]) != original[idx]:
235-
# We have ints that lost precision during the cast.
236-
inferred_type = lib.infer_dtype(original, skipna=True)
237-
if (
238-
inferred_type not in ["floating", "mixed-integer-float"]
239-
and not mask.any()
240-
):
241-
values = np.asarray(original, dtype=dtype)
242-
else:
243-
values = np.asarray(original, dtype="object")
235+
if not (pd.isna(values[idx]) or pd.isna(original[idx])):
236+
if int(values[idx]) != original[idx]:
237+
# We have ints that lost precision during the cast.
238+
inferred_type = lib.infer_dtype(original, skipna=True)
239+
if (
240+
inferred_type not in ["floating", "mixed-integer-float"]
241+
and not mask.any()
242+
):
243+
values = np.asarray(original, dtype=dtype)
244+
else:
245+
values = np.asarray(original, dtype="object")
244246

245247
# we copy as need to coerce here
246248
if mask.any():

pandas/tests/indexing/test_iloc.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1090,6 +1090,25 @@ def test_iloc_setitem_pure_position_based(self, indexer, has_ref):
10901090
expected = DataFrame({"a": [1, 2, 3], "b": [11, 12, 13], "c": [7, 8, 9]})
10911091
tm.assert_frame_equal(df2, expected)
10921092

1093+
def test_iloc_assignment_nullable_int_with_na(self):
1094+
# GH#62473
1095+
ser = Series(
1096+
[4, 6, 9, None, 10, 13, 15], index=[6, 1, 5, 0, 3, 2, 4], dtype="Int64"
1097+
)
1098+
indices = Series(
1099+
[6, 1, 5, 0, 3, 2, 4], index=[6, 1, 5, 0, 3, 2, 4], dtype="int64"
1100+
)
1101+
values = Series(
1102+
[4, 6, 9, None, 10, 13, 15], index=[4, 1, 2, 6, 0, 5, 3], dtype="Int64"
1103+
)
1104+
1105+
ser.iloc[indices] = values
1106+
1107+
expected = Series(
1108+
[None, 6, 13, 15, 4, 9, 10], index=[6, 1, 5, 0, 3, 2, 4], dtype="Int64"
1109+
)
1110+
tm.assert_series_equal(ser.sort_index(), expected.sort_index())
1111+
10931112
@pytest.mark.parametrize("has_ref", [True, False])
10941113
def test_iloc_setitem_dictionary_value(self, has_ref):
10951114
# GH#37728

0 commit comments

Comments
 (0)