8000 np.array([pd.Series({1:1})]) threw KeyError in numpy 1.17.4 · Issue #18023 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

np.array([pd.Series({1:1})]) threw KeyError in numpy 1.17.4 #18023

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
MarcoGorelli opened this issue Dec 18, 2020 · 1 comment
Closed

np.array([pd.Series({1:1})]) threw KeyError in numpy 1.17.4 #18023

MarcoGorelli opened this issue Dec 18, 2020 · 1 comment

Comments

@MarcoGorelli
Copy link
Member

Hi - this was reported here pandas-dev/pandas#38543 in pandas, am just posting here in case it wasn't reported to you previously.

In short, np.array([pd.Series({1:1})]) works in numpy 1.19.4 but not in numpy 1.17.4

$ mkdir tmp
$ cd tmp/
$ virtualenv venv
$ . venv/bin/activate
$ pip install numpy==1.17.4 pandas==1.1.5
$ python -c 'import numpy; import pandas; print(numpy.array([pandas.Series({1:1})]))'
Traceback (most recent call last):
  File "/home/marco/tmp/venv/lib/python3.8/site-packages/pandas/core/indexes/base.py", line 2898, in get_loc
    return self._engine.get_loc(casted_key)
  File "pandas/_libs/index.pyx", line 70, in pandas._libs.index.IndexEngine.get_loc
  File "pandas/_libs/index.pyx", line 101, in pandas._libs.index.IndexEngine.get_loc
  File "pandas/_libs/hashtable_class_helper.pxi", line 1032, in pandas._libs.hashtable.Int64HashTable.get_item
  File "pandas/_libs/hashtable_class_helper.pxi", line 1039, in pandas._libs.hashtable.Int64HashTable.get_item
KeyError: 0

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/home/marco/tmp/venv/lib/python3.8/site-packages/pandas/core/series.py", line 882, in __getitem__
    return self._get_value(key)
  File "/home/marco/tmp/venv/lib/python3.8/site-packages/pandas/core/series.py", line 990, in _get_value
    loc = self.index.get_loc(label)
  File "/home/marco/tmp/venv/lib/python3.8/site-packages/pandas/core/indexes/base.py", line 2900, in get_loc
    raise KeyError(key) from err
KeyError: 0

$ pip install numpy==1.19.4
$ python -c 'import numpy; import pandas; print(numpy.array([pandas.Series({1:1})]))'
[[1]]
@seberg
Copy link
Member
seberg commented Dec 18, 2020

Effectively changed in gh-14995 (although the 1.20 code is completely different). There is a test ensuring that object.__getitem__ is never called when that object also defined __array__. So this is effectively tested (which seemed like the reason for opening this issue).

Of course more tests are always good, so more could be added around this behaviour change probably!

Anyway, going to close this, it doesn't seem like something to put on the TODO list explicitly. The new NumPy always prioritizes __array__, etc. which is a change. Pre 1.19.x had a strange mix of things where there was one place where it did not use the __array__ protocol.

Or am I missing that you actually prefer the old behaviour?!

@seberg seberg closed this as completed Dec 18, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants
0