Skip to content

Conversation

MarcoGorelli
Copy link
Member

  • closes #xxxx (Replace xxxx with the Github issue number)
  • Tests added and passed if fixing a bug or adding a new feature
  • All code checks passed.
  • Added type annotations to new arguments/methods/functions.
  • Added an entry in the latest doc/source/whatsnew/vX.X.X.rst file if fixing a bug or adding a new feature.

Noticed these in #47497

I have a question about the _ixs docstring in frame.py - it says that if a slice is passed, then
the result is a view. However, the arguments are annotated to be int only. Is the docstring
correct, or is the type annotation too tight?

I presume it's the former, since I'm not able to pass a slice:

>>> df = pd.DataFrame({'a': [1,2, 3], 'b': [4,5,6]})
>>> df
   a  b
0  1  4
1  2  5
2  3  6
>>> df._ixs(0, axis=1)
0    1
1    2
2    3
Name: a, dtype: int64
>>> df._ixs(slice(0, 2, None), axis=1)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/megorelli/pandas-dev/pandas/core/frame.py", line 3588, in _ixs
    col_mgr = self._mgr.iget(i)
  File "/home/megorelli/pandas-dev/pandas/core/internals/managers.py", line 988, in iget
    block = self.blocks[self.blknos[i]]
TypeError: only integer scalar arrays can be converted to a scalar index

Also, typing _ixs as that's what's called within get_item_cache.
For Series._ixs, I've annotated the return type as Any, as that's what the return
type of Series.pop is.

@MarcoGorelli MarcoGorelli added the Typing type annotations, mypy/pyright type checking label Jun 25, 2022
return self.take(indices=indices, axis=axis)

def _ixs(self, i: int, axis: int = 0):
def _ixs(self, i: int, axis: int = 0) -> Any:
Copy link
Member

Choose a reason for hiding this comment

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

Wouldn't mind leaving it un-annotated but I'm also fine using Any.

Ideally Series becomes generic as done by @Dr-Irv in the pandas-stubs but that is probably something for the future.

@phofl
Copy link
Member

phofl commented Jun 25, 2022

I think slice support was removed in c633579.

Looks like this was forgotten back then

@mroeschke mroeschke added this to the 1.5 milestone Jun 27, 2022
@mroeschke mroeschke merged commit cb67837 into pandas-dev:main Jun 27, 2022
@mroeschke
Copy link
Member

Thanks @MarcoGorelli

yehoshuadimarsky pushed a commit to yehoshuadimarsky/pandas that referenced this pull request Jul 13, 2022
* typ: _item_cache and _ixs

* fixup docstring

* linting
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Typing type annotations, mypy/pyright type checking
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants