Skip to content

Commit e476938

Browse files
authored
BUG: iloc raising for ea series (#50171)
* BUG: iloc raising for ea series * Move test * Add test
1 parent 18be757 commit e476938

File tree

3 files changed

+29
-1
lines changed

3 files changed

+29
-1
lines changed

doc/source/whatsnew/v2.0.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -790,6 +790,7 @@ Indexing
790790
- Bug in :meth:`DataFrame.loc` raising ``ValueError`` with ``bool`` indexer and :class:`MultiIndex` (:issue:`47687`)
791791
- Bug in :meth:`DataFrame.__setitem__` raising ``ValueError`` when right hand side is :class:`DataFrame` with :class:`MultiIndex` columns (:issue:`49121`)
792792
- Bug in :meth:`DataFrame.reindex` casting dtype to ``object`` when :class:`DataFrame` has single extension array column when re-indexing ``columns`` and ``index`` (:issue:`48190`)
793+
- Bug in :meth:`DataFrame.iloc` raising ``IndexError`` when indexer is a :class:`Series` with numeric extension array dtype (:issue:`49521`)
793794
- Bug in :func:`~DataFrame.describe` when formatting percentiles in the resulting index showed more decimals than needed (:issue:`46362`)
794795
- Bug in :meth:`DataFrame.compare` does not recognize differences when comparing ``NA`` with value in nullable dtypes (:issue:`48939`)
795796
-

pandas/core/indexing.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -1481,7 +1481,12 @@ def _validate_key(self, key, axis: AxisInt):
14811481
# so don't treat a tuple as a valid indexer
14821482
raise IndexingError("Too many indexers")
14831483
elif is_list_like_indexer(key):
1484-
arr = np.array(key)
1484+
if isinstance(key, ABCSeries):
1485+
arr = key._values
1486+
elif is_array_like(key):
1487+
arr = key
1488+
else:
1489+
arr = np.array(key)
14851490
len_axis = len(self.obj._get_axis(axis))
14861491

14871492
# check that the key has a numeric dtype

pandas/tests/frame/indexing/test_indexing.py

+22
Original file line numberDiff line numberDiff line change
@@ -1437,6 +1437,28 @@ def test_loc_rhs_empty_warning(self):
14371437
df.loc[:, "a"] = rhs
14381438
tm.assert_frame_equal(df, expected)
14391439

1440+
def test_iloc_ea_series_indexer(self):
1441+
# GH#49521
1442+
df = DataFrame([[0, 1, 2, 3, 4], [5, 6, 7, 8, 9]])
1443+
indexer = Series([0, 1], dtype="Int64")
1444+
row_indexer = Series([1], dtype="Int64")
1445+
result = df.iloc[row_indexer, indexer]
1446+
expected = DataFrame([[5, 6]], index=[1])
1447+
tm.assert_frame_equal(result, expected)
1448+
1449+
result = df.iloc[row_indexer.values, indexer.values]
1450+
tm.assert_frame_equal(result, expected)
1451+
1452+
def test_iloc_ea_series_indexer_with_na(self):
1453+
# GH#49521
1454+
df = DataFrame([[0, 1, 2, 3, 4], [5, 6, 7, 8, 9]])
1455+
indexer = Series([0, pd.NA], dtype="Int64")
1456+
msg = "cannot convert"
1457+
with pytest.raises(ValueError, match=msg):
1458+
df.iloc[:, indexer]
1459+
with pytest.raises(ValueError, match=msg):
1460+
df.iloc[:, indexer.values]
1461+
14401462
@pytest.mark.parametrize("indexer", [True, (True,)])
14411463
@pytest.mark.parametrize("dtype", [bool, "boolean"])
14421464
def test_loc_bool_multiindex(self, dtype, indexer):

0 commit comments

Comments
 (0)