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 Series.reset_index() returning wrong type when index of Series is a MultiIndex #393

Closed
Dr-Irv opened this issue Sep 18, 2020 · 3 comments
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 18, 2020

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

Expected behaviour

No error

Actual behaviour

mi3: pd.Index = pd.MultiIndex.from_product(
    [[1, 2], [3, 4], [5, 6]], names=["a", "b", "c"]
)
s = pd.Series([i for i in range(len(mi3))], index=mi3)
dr: pd.DataFrame = s.reset_index(-1)
print("dr")
print(dr)
print(type(dr))

reports for line 5

Expression of type "Series[Unknown]" cannot be assigned to declared type "DataFrame"
  "Series[Unknown]" is incompatible with "DataFrame"

The issue here is that reset_index() can return a Series or DataFrame dependent on whether the parameter drop is True or False. (It is False by default). In the example above, it is returning a DataFrame

@Dr-Irv
Copy link
Author

Dr-Irv commented Sep 18, 2020

Here's a fix:

diff series.pyi.ORIG series.pyi
189c190
<         self, level: Optional[Level] = ..., drop: _bool = ..., name: Optional[object] = ..., inplace: _bool = ...,
---
>         self, level: Optional[Level] = ..., drop: _bool = Literal[True], name: Optional[object] = ..., inplace: _bool = ...,
191a193,196
>     def reset_index(
>         self, level: Optional[Level] = ..., drop: _bool = ..., name: Optional[object] = ..., inplace: _bool = ...,
>     ) -> DataFrame: ...
>     @overload

@jakebailey jakebailey added the bug Something isn't working label Sep 18, 2020
@github-actions github-actions bot removed the triage label Sep 18, 2020
@gramster
Copy link
Member

Thanks for all these reports and suggested fixes; that's awesome!

@judej judej added typestub Issue relating to our bundled type stubs and removed bug Something isn't working labels Sep 21, 2020
@gramster gramster added the fixed in next version (main) A fix has been implemented and will appear in an upcoming version label Sep 22, 2020
@jakebailey
Copy link
Member

This issue has been fixed in version 2020.9.6, which we've just released. You can find the changelog here: https://github.com/microsoft/pylance-release/blob/master/CHANGELOG.md#202096-23-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

4 participants