Skip to content

Commit 160bdc8

Browse files
fix Error Indexing DafaFrame with a 0-d array
closes pandas-dev#21946
1 parent 716efd3 commit 160bdc8

File tree

3 files changed

+9
-1
lines changed

3 files changed

+9
-1
lines changed

pandas/core/indexes/base.py

+2
Original file line numberDiff line numberDiff line change
@@ -4178,6 +4178,8 @@ def _validate_indexer(self, form, key, kind):
41784178
pass
41794179
elif is_integer(key):
41804180
pass
4181+
elif key.ndim == 0:
4182+
self._invalid_indexer(form, key)
41814183
elif kind in ['iloc', 'getitem']:
41824184
self._invalid_indexer(form, key)
41834185
return key

pandas/core/indexing.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -2693,7 +2693,7 @@ def is_nested_tuple(tup, labels):
26932693
def is_list_like_indexer(key):
26942694
# allow a list_like, but exclude NamedTuples which can be indexers
26952695
return is_list_like(key) and not (isinstance(key, tuple) and
2696-
type(key) is not tuple)
2696+
type(key) is not tuple) and not np.array(key).ndim == 0
26972697

26982698

26992699
def is_label_like(key):

pandas/tests/indexing/test_indexing.py

+6
Original file line numberDiff line numberDiff line change
@@ -890,6 +890,12 @@ def test_no_reference_cycle(self):
890890
del df
891891
assert wr() is None
892892

893+
def test_zero_index_iloc_raises(self):
894+
df = pd.DataFrame([[1, 2], [3, 4]], columns=['a', 'b'])
895+
ar = np.array(0)
896+
tm.assert_raises_regex(TypeError, 'Cannot index by location index with a non-integer key',
897+
lambda: df.iloc[ar])
898+
893899

894900
class TestSeriesNoneCoercion(object):
895901
EXPECTED_RESULTS = [

0 commit comments

Comments
 (0)