Skip to content
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

pandas MultiIndex.levels return type not correct #412

Closed
Dr-Irv opened this issue Sep 24, 2020 · 1 comment
Closed

pandas MultiIndex.levels return type not correct #412

Dr-Irv opened this issue Sep 24, 2020 · 1 comment
Assignees
Labels
fixed in next version (main) A fix has been implemented and will appear in an upcoming version typestub Issue relating to our bundled type stubs

Comments

@Dr-Irv
Copy link

Dr-Irv commented Sep 24, 2020

Environment data

  • Language Server version: 2020.9.6
  • OS and version: Windows 10
  • Python version (& distribution if applicable, e.g. Anaconda): Anaconda Python 3.7.5

Expected behaviour

No error

Actual behaviour

from typing import cast
import pandas as pd

df = pd.DataFrame.from_records([[1, 2, 3], [4, 5, 6]], columns=["a", "b", "c"])
dfmi = df.set_index(["a", "b"])
print(type(dfmi.index))
ind = cast(pd.MultiIndex, dfmi.index)
print(ind.levels[1])

reports

Object of type "() -> Unknown" cannot be subscripted

Issue here is that MultiIndex.levels acts like a python property with the internal decorator of @cache_readonly, except you are not allowed to set it. Not sure what to do about that part, but below is a fix that works for the most common usage. It just would not report a typing error on a line such as ind.levels = [3, 4], which is not allowed.

Possible fix:

diff core/indexes/multi.pyi.ORIG core/indexes/multi.pyi
3c3,4
< from typing import Hashable, Sequence, Union
---
> from typing import Hashable, Sequence, Union, List
20c21,22
<     def levels(self): ...
---
>     @property  # is cache_readonly in pandas source
>     def levels(self) -> List[Index]: ...
@jakebailey jakebailey added the typestub Issue relating to our bundled type stubs label Sep 24, 2020
@github-actions github-actions bot removed the triage label Sep 24, 2020
@jakebailey jakebailey added the fixed in next version (main) A fix has been implemented and will appear in an upcoming version label Sep 25, 2020
@jakebailey
Copy link
Member

This issue has been fixed in version 2020.9.7, which we've just released. You can find the changelog here: https://github.com/microsoft/pylance-release/blob/master/CHANGELOG.md#202097-30-september-2020

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
fixed in next version (main) A fix has been implemented and will appear in an upcoming version typestub Issue relating to our bundled type stubs
Projects
None yet
Development

No branches or pull requests

3 participants