Skip to content

BUG: "boolean value of NA is ambiguous" when doing iloc on nullable integer series #62473

@MarcoGorelli

Description

@MarcoGorelli

Pandas version checks

  • I have checked that this issue has not already been reported.

  • I have confirmed this bug exists on the latest version of pandas.

  • I have confirmed this bug exists on the main branch of pandas.

Reproducible Example

import pandas as pd

ser = pd.Series([4,6,9,None,10,13,15],index=[6,1,5,0,3,2,4],dtype='Int64')
indices = pd.Series([6,1,5,0,3,2,4],index=[6,1,5,0,3,2,4],dtype='int64')
values = pd.Series([4,6,9,None,10,13,15],index=[4,1,2,6,0,5,3],dtype='Int64')

ser.iloc[indices] = values
print(ser)

Issue Description

Traceback (most recent call last):
  File "/home/marcogorelli/scratch/t.py", line 8, in <module>
    ser.iloc[indices] = values
    ~~~~~~~~^^^^^^^^^
  File "/home/marcogorelli/scratch/.venv/lib/python3.12/site-packages/pandas/core/indexing.py", line 911, in __setitem__
    iloc._setitem_with_indexer(indexer, value, self.name)
  File "/home/marcogorelli/scratch/.venv/lib/python3.12/site-packages/pandas/core/indexing.py", line 1944, in _setitem_with_indexer
    self._setitem_single_block(indexer, value, name)
  File "/home/marcogorelli/scratch/.venv/lib/python3.12/site-packages/pandas/core/indexing.py", line 2218, in _setitem_single_block
    self.obj._mgr = self.obj._mgr.setitem(indexer=indexer, value=value)
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/marcogorelli/scratch/.venv/lib/python3.12/site-packages/pandas/core/internals/managers.py", line 415, in setitem
    return self.apply("setitem", indexer=indexer, value=value)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/marcogorelli/scratch/.venv/lib/python3.12/site-packages/pandas/core/internals/managers.py", line 363, in apply
    applied = getattr(b, f)(**kwargs)
              ^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/marcogorelli/scratch/.venv/lib/python3.12/site-packages/pandas/core/internals/blocks.py", line 2129, in setitem
    values[indexer] = value
    ~~~~~~^^^^^^^^^
  File "/home/marcogorelli/scratch/.venv/lib/python3.12/site-packages/pandas/core/arrays/masked.py", line 320, in __setitem__
    value, mask = self._coerce_to_array(value, dtype=self.dtype)
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/marcogorelli/scratch/.venv/lib/python3.12/site-packages/pandas/core/arrays/numeric.py", line 272, in _coerce_to_array
    values, mask, _, _ = _coerce_to_data_and_mask(
                         ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/marcogorelli/scratch/.venv/lib/python3.12/site-packages/pandas/core/arrays/numeric.py", line 209, in _coerce_to_data_and_mask
    if int(values[idx]) != original[idx]:
       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "pandas/_libs/missing.pyx", line 392, in pandas._libs.missing.NAType.__bool__
TypeError: boolean value of NA is ambiguous

Note that for Int64[pyarrow] or 'int64`, it'"s fine

Expected Behavior

6    <NA>
1       6
5      13
0      10
3      15
2       9
4       4
dtype: Int

Installed Versions

INSTALLED VERSIONS

commit : 4665c10
python : 3.12.5
python-bits : 64
OS : Linux
OS-release : 6.6.87.2-microsoft-standard-WSL2
Version : #1 SMP PREEMPT_DYNAMIC Thu Jun 5 18:30:46 UTC 2025
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : C.UTF-8
LOCALE : C.UTF-8

pandas : 2.3.2
numpy : 2.3.3
pytz : 2025.2
dateutil : 2.9.0.post0
pip : 25.0.1
Cython : None
sphinx : None
IPython : 9.2.0
adbc-driver-postgresql: None
adbc-driver-sqlite : None
bs4 : 4.13.4
blosc : None
bottleneck : None
dataframe-api-compat : None
fastparquet : None
fsspec : 2025.9.0
html5lib : None
hypothesis : None
gcsfs : 2025.2.0
jinja2 : 3.1.6
lxml.etree : None
matplotlib : 3.10.3
numba : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
psycopg2 : None
pymysql : None
pyarrow : 21.0.0
pyreadstat : None
pytest : 8.3.3
python-calamine : None
pyxlsb : None
s3fs : None
scipy : 1.16.0
sqlalchemy : None
tables : None
tabulate : 0.9.0
xarray : None
xlrd : None
xlsxwriter : None
zstandard : None
tzdata : 2025.2
qtpy : None
pyqt5 : None

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugNA - MaskedArraysRelated to pd.NA and nullable extension arrays

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions