BUG: wrong/confusing error message in DataFrame.take with a scalar indexer
See original GitHub issueIn [4]: df = pd.DataFrame({'a': [1, 2, 3]})
In [5]: df.take(1)
...
~/scipy/pandas/pandas/core/indexes/range.py in take(self, indices, axis, allow_fill, fill_value, **kwargs)
433 ) -> Int64Index:
434 with rewrite_exception("Int64Index", type(self).__name__):
--> 435 return self._int64index.take(
436 indices,
437 axis=axis,
~/scipy/pandas/pandas/core/indexes/base.py in take(self, indices, axis, allow_fill, fill_value, **kwargs)
962 self._values, indices, allow_fill=allow_fill, fill_value=self._na_value
963 )
--> 964 return type(self)._simple_new(taken, name=self.name)
965
966 @final
~/scipy/pandas/pandas/core/indexes/base.py in _simple_new(cls, values, name)
611 Must be careful not to recurse.
612 """
--> 613 assert isinstance(values, np.ndarray), type(values)
614
615 result = object.__new__(cls)
AssertionError: <class 'numpy.int64'>
take
requires an array-like for the indexer, but when passing a scalar you get a not very useful error message as above. We should raise a ValueError (or rather TypeError) instead with an informative message.
It actually comes from the Index take implementation:
In [9]: pd.Index([1, 2, 3]).take(1)
---------------------------------------------------------------------------
AssertionError Traceback (most recent call last)
<ipython-input-9-ce9f87ba8bd2> in <module>
----> 1 pd.Index([1, 2, 3]).take(1)
~/scipy/pandas/pandas/core/indexes/base.py in take(self, indices, axis, allow_fill, fill_value, **kwargs)
962 self._values, indices, allow_fill=allow_fill, fill_value=self._na_value
963 )
--> 964 return type(self)._simple_new(taken, name=self.name)
965
966 @final
~/scipy/pandas/pandas/core/indexes/base.py in _simple_new(cls, values, name)
611 Must be careful not to recurse.
612 """
--> 613 assert isinstance(values, np.ndarray), type(values)
614
615 result = object.__new__(cls)
AssertionError: <class 'numpy.int64'>
Issue Analytics
- State:
- Created 2 years ago
- Comments:20 (6 by maintainers)
Top Results From Across the Web
Python DataFrame TypeError: only integer scalar arrays can ...
I have this part of code and i want to print the column "y" of the Dataframe df. The following error occurs: TypeError:...
Read more >How to Fix: if using all scalar values, you must pass an index
This error occurs when you attempt to create a pandas DataFrame by passing all scalar values, yet fail to pass an index as...
Read more >Indexing and selecting data — pandas 1.5.2 documentation
If you only want to access a scalar value, the fastest way is to use the at and iat methods, which are implemented...
Read more >Intro to data structures — pandas 1.5.2 documentation
If data is a scalar value, an index must be provided. ... pandas knows how to take an ExtensionArray and store it in...
Read more >Indexing and Selecting Data — pandas 0.15.0 documentation
You may access an index on a Series, column on a DataFrame, and a item on a ... If you only want to...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@radioactive11 got it. I am viewing the discussion on #42886 now. thanks.
Yes, certainly!