Skip to content

Incorrect type for Series.unstack #1256

Closed
@stephenlrandall

Description

@stephenlrandall
Contributor

Describe the bug

As noted in #703, providing a list of ints or strings to unstack leads to a type error:

I've identified a few places (see attachment). However, I am not sure the patch is correct/complete. It is interesting to see that stack is annotated with level: Level | list[Level] = ... while unstack has level: Level = ... while IMHO both should be IndexLabel.

It seems as though the fix in #837 only touched groupby.

Activity

Dr-Irv

Dr-Irv commented on Jun 18, 2025

@Dr-Irv
Collaborator

Yes, but we need an example that illustrates the issue with unstack(). Can you provide that?

stephenlrandall

stephenlrandall commented on Jun 19, 2025

@stephenlrandall
ContributorAuthor

@Dr-Irv Sure thing --

import pandas as pd

df = pd.DataFrame([[1, 3, 5], [2, 4, 6]])

vector = df.transpose().stack([*range(df.index.nlevels)])

display(vector)

matrix = vector.unstack([*range(vector.index.nlevels // 2)])

display(matrix)
Image
Dr-Irv

Dr-Irv commented on Jun 19, 2025

@Dr-Irv
Collaborator

Thanks for the report. PR welcome. It's an easy fix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Participants

      @Dr-Irv@stephenlrandall

      Issue actions

        Incorrect type for `Series.unstack` · Issue #1256 · pandas-dev/pandas-stubs