question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

BUG: wrong/confusing error message in DataFrame.take with a scalar indexer

See original GitHub issue
In [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:closed
  • Created 2 years ago
  • Comments:20 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
a-n-622commented, Aug 18, 2021

@radioactive11 got it. I am viewing the discussion on #42886 now. thanks.

1reaction
jorisvandenbosschecommented, Aug 4, 2021

Yes, certainly!

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found