Skip to content

BUG: iloc with Series as indexer fails for __getitem__ but works with __setitem__ #60994

Closed
@luxedo

Description

@luxedo

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

# __getitem__
>>> a = pd.Series([0, 1, 2])
>>> a.iloc[pd.Series([True, False, False])]
---------------------------------------------------------------------------
NotImplementedError                       Traceback (most recent call last)
Cell In[8], line 1
----> 1 a.iloc[pd.Series([True, False, False])]

File ~/.local/share/hatch/env/virtual/lontras/VBVTu9RT/lontras/lib/python3.11/site-packages/pandas/core/indexing.py:1191, in _LocationIndexer.__getitem__(self, key)
   1189 maybe_callable = com.apply_if_callable(key, self.obj)
   1190 maybe_callable = self._check_deprecated_callable_usage(key, maybe_callable)
-> 1191 return self._getitem_axis(maybe_callable, axis=axis)

File ~/.local/share/hatch/env/virtual/lontras/VBVTu9RT/lontras/lib/python3.11/site-packages/pandas/core/indexing.py:1738, in _iLocIndexer._getitem_axis(self, key, axis)
   1735     key = np.asarray(key)
   1737 if com.is_bool_indexer(key):
-> 1738     self._validate_key(key, axis)
   1739     return self._getbool_axis(key, axis=axis)
   1741 # a list of integers

File ~/.local/share/hatch/env/virtual/lontras/VBVTu9RT/lontras/lib/python3.11/site-packages/pandas/core/indexing.py:1578, in _iLocIndexer._validate_key(self, key, axis)
   1576 if hasattr(key, "index") and isinstance(key.index, Index):
   1577     if key.index.inferred_type == "integer":
-> 1578         raise NotImplementedError(
   1579             "iLocation based boolean "
   1580             "indexing on an integer type "
   1581             "is not available"
   1582         )
   1583     raise ValueError(
   1584         "iLocation based boolean indexing cannot use "
   1585         "an indexable as a mask"
   1586     )
   1587 return

NotImplementedError: iLocation based boolean indexing on an integer type is not available

# __setitem__
>>> a.iloc[pd.Series([True, False, False])] = 10; a
0    10
1     1
2     2
dtype: int64

Issue Description

Behavior of loc with a Series as argument shows inconsistent behavior for __getitem__ and __setitem__

Expected Behavior

Either both __getitem__ and __setitem__ should work or both should fail

Installed Versions

INSTALLED VERSIONS ------------------ commit : 0691c5c python : 3.11.10 python-bits : 64 OS : Linux OS-release : 6.12.10-arch1-1 Version : #1 SMP PREEMPT_DYNAMIC Sat, 18 Jan 2025 02:26:57 +0000 machine : x86_64 processor : byteorder : little LC_ALL : None LANG : en_US.UTF-8 LOCALE : en_US.UTF-8

pandas : 2.2.3
numpy : 2.2.2
pytz : 2024.2
dateutil : 2.9.0.post0
pip : 24.3.1
Cython : None
sphinx : 8.1.3
IPython : 8.31.0
adbc-driver-postgresql: None
adbc-driver-sqlite : None
bs4 : None
blosc : None
bottleneck : None
dataframe-api-compat : None
fastparquet : None
fsspec : None
html5lib : None
hypothesis : None
gcsfs : None
jinja2 : 3.1.5
lxml.etree : None
matplotlib : None
numba : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
psycopg2 : None
pymysql : None
pyarrow : None
pyreadstat : None
pytest : None
python-calamine : None
pyxlsb : None
s3fs : None
scipy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlsxwriter : None
zstandard : None
tzdata : 2024.2
qtpy : None
pyqt5 : None

Metadata

Metadata

Assignees

Labels

BugIndexingRelated to indexing on series/frames, not to indexes themselves

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions