Skip to content

BUG: df.groupby(col, as_index=False).value_counts() returns a DataFrame but is annotated as Series #203

Closed
@phofl

Description

@phofl

Describe the bug

Using

df = pd.DataFrame({"a": [1, 2, 3], "b": 1})

x: pd.DataFrame = df.groupby("a", as_index=False).value_counts()

raises an error, because mypy thinks the return is a Series

 Incompatible types in assignment (expression has type "Series[int]", variable has type "DataFrame")  [assignment]

To Reproduce

  1. Provide a minimal runnable pandas example that is not properly checked by the stubs.
  2. Indicate which type checker you are using (mypy or pyright).
  3. Show the error message received from that type checker while checking your example.
df = pd.DataFrame({"a": [1, 2, 3], "b": 1})

x: pd.DataFrame = df.groupby("a", as_index=False).value_counts()

In theory this should be ok, but not sure if this as_index=False can be typed?

Please complete the following information:

  • OS: ubuntu
  • OS Version 22.04
  • python version 3.10
  • version of type checker mypy 0.971
  • version of installed pandas-stubs newest 20220815

Additional context
Add any other context about the problem here.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions