Skip to content

fix Error Indexing DafaFrame with a 0-d array #22032

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.24.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -501,6 +501,7 @@ Indexing
- Fixed ``DataFrame[np.nan]`` when columns are non-unique (:issue:`21428`)
- Bug when indexing :class:`DatetimeIndex` with nanosecond resolution dates and timezones (:issue:`11679`)
- Bug where indexing with a Numpy array containing negative values would mutate the indexer (:issue:`21867`)
- Bug where indexing with a 0-dimensional array would error with an unhelpful stack trace (:issue: `21946`)

Missing
^^^^^^^
Expand Down
2 changes: 2 additions & 0 deletions pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -4178,6 +4178,8 @@ def _validate_indexer(self, form, key, kind):
pass
elif is_integer(key):
pass
elif np.array(key).ndim == 0:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pls don’t do this
you end up materializing the key which could be expensive

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yea I wasn't really happy with it as is anyways and since it seems to break some tests I'll probably change it in the AM.

self._invalid_indexer(form, key)
elif kind in ['iloc', 'getitem']:
self._invalid_indexer(form, key)
return key
Expand Down
5 changes: 3 additions & 2 deletions pandas/core/indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -2692,8 +2692,9 @@ def is_nested_tuple(tup, labels):

def is_list_like_indexer(key):
# allow a list_like, but exclude NamedTuples which can be indexers
return is_list_like(key) and not (isinstance(key, tuple) and
type(key) is not tuple)
return (is_list_like(key)
and not (isinstance(key, tuple) and type(key) is not tuple)
and not np.array(key).ndim == 0)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're really just checking if you have a scalar. Is it possible to use (and potentially modify) is_scalar from pandas.core.dtypes.inference?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Possibly! I'll look into that more tomorrow.



def is_label_like(key):
Expand Down
7 changes: 7 additions & 0 deletions pandas/tests/indexing/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -890,6 +890,13 @@ def test_no_reference_cycle(self):
del df
assert wr() is None

def test_zero_index_iloc_raises(self):
df = pd.DataFrame([[1, 2], [3, 4]], columns=['a', 'b'])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reference issue as a comment below the function def.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can do.

ar = np.array(0)
msg = 'Cannot index by location index with a non-integer key'
with assert_raises_regex(TypeError, msg):
df.iloc[ar]


class TestSeriesNoneCoercion(object):
EXPECTED_RESULTS = [
Expand Down