Skip to content

DataFrame.stack() needs to accept list for level argument #489

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

Closed
Dr-Irv opened this issue Dec 29, 2022 · 1 comment · Fixed by #491
Closed

DataFrame.stack() needs to accept list for level argument #489

Dr-Irv opened this issue Dec 29, 2022 · 1 comment · Fixed by #491
Labels
good first issue Reshaping Concat, Merge/Join, Stack/Unstack, Explode

Comments

@Dr-Irv
Copy link
Collaborator

Dr-Irv commented Dec 29, 2022

Describe the bug

For DataFrame.stack(), level is allowed to be a list

To Reproduce

  1. Provide a minimal runnable pandas example that is not properly checked by the stubs.
import pandas as pd
multicol2 = pd.MultiIndex.from_tuples([('weight', 'kg'),
                                       ('height', 'm')])
df_multi_level_cols2 = pd.DataFrame([[1.0, 2.0], [3.0, 4.0]],
                                    index=['cat', 'dog'],
                                    columns=multicol2)
df_multi_level_cols2.stack([0, 1])
  1. Indicate which type checker you are using (mypy or pyright).

pyright 1.1.286

  1. Show the error message received from that type checker while checking your example.
    pyright reports:
stack.py:7:28 - error: Argument of type "list[int]" cannot be assigned to parameter "level" of type "Level" in function "stack"
    Type "list[int]" cannot be assigned to type "Level"
      "list[int]" is incompatible with protocol "Hashable"
        "__hash__" is an incompatible type
          Type "None" cannot be assigned to type "(self: list[int]) -> int"
      "list[int]" is incompatible with "int" (reportGeneralTypeIssues)

Please complete the following information:

  • OS: Windows 10
  • OS Version: 10
  • python version: 3.9
  • version of type checker: pyright 1.1.286
  • version of installed pandas-stubs 1.5.2.221213

Additional context

Change

def stack(self, level: Level = ..., dropna: _bool = ...) -> DataFrame | Series: ...

to allow Level | list[Level] for the level argument.

PR welcome with tests

@Dr-Irv Dr-Irv added good first issue Reshaping Concat, Merge/Join, Stack/Unstack, Explode labels Dec 29, 2022
@muddi900
Copy link
Contributor

I'd like to work on this.

Thank you

muddi900 added a commit to muddi900/pandas-stubs that referenced this issue Dec 31, 2022
muddi900 added a commit to muddi900/pandas-stubs that referenced this issue Jan 4, 2023
added a generic Any for series output

created tests for stack

wrapping generics in quotes for compatibility

style update
Dr-Irv pushed a commit that referenced this issue Jan 5, 2023
added list[Levels] as per #489 and added appropriate tests

added a generic Any for series output

created tests for stack

wrapping generics in quotes for compatibility

style update
twoertwein pushed a commit to twoertwein/pandas-stubs that referenced this issue Apr 1, 2023
added list[Levels] as per pandas-dev#489 and added appropriate tests

added a generic Any for series output

created tests for stack

wrapping generics in quotes for compatibility

style update
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Reshaping Concat, Merge/Join, Stack/Unstack, Explode
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants