Skip to content

Commit cb98d2a

Browse files
committed
ENH: df.iloc accepts zero dim integer np.array as int
1 parent 539c54f commit cb98d2a

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

pandas/core/indexing.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -2222,7 +2222,12 @@ def _getitem_axis(self, key, axis=None):
22222222

22232223
# a single integer
22242224
else:
2225-
if not is_integer(key):
2225+
# zero_dim integer np.array is converted to int
2226+
if isinstance(key, np.ndarray) and key.ndim == 0 \
2227+
and issubclass(key.dtype.type, np.integer):
2228+
key = key.item()
2229+
2230+
elif not is_integer(key):
22262231
raise TypeError("Cannot index by location index with a "
22272232
"non-integer key")
22282233

pandas/tests/indexing/test_iloc.py

+7
Original file line numberDiff line numberDiff line change
@@ -675,3 +675,10 @@ def test_identity_slice_returns_new_object(self):
675675
# should also be a shallow copy
676676
original_series[:3] = [7, 8, 9]
677677
assert all(sliced_series[:3] == [7, 8, 9])
678+
679+
def test_indexing_zero_dim_np_array(self):
680+
# GH24919
681+
df = DataFrame([[1, 2], [3, 4]])
682+
683+
# should not raise an error
684+
df.iloc[np.array(0)]

0 commit comments

Comments
 (0)