question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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.

Issue Analytics

  • State:open
  • Created a year ago
  • Comments:11 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
amotzopcommented, Aug 19, 2022

I think as_index is kind of special and changes the return type of many functions, so there is also a third option of breaking SeriesGroupBy and DataFrameGroupBy into 2 different classes depending on as_index and overloading the various functions which return them.

0reactions
leeseicommented, Nov 25, 2022

Thanks, this issues was the cloest I got from Googling so I chime in here. 😝

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to reset index after Groupby pandas?
Output: Resetting the index after grouping data, using reset_index(), it is a function provided by python to add indexes to the data.
Read more >
pandas groupby without turning grouped by column into ...
Solution 1: df.groupby(['A', 'B'], as_index=False).sum() ... can put back the indices of the dataframe as columns and use a default index.
Read more >
ENH: guarantee pandas.Series.value_counts "sort=False" ...
Hello, I'm trying to make a new DataFrame that contains the value counts of a column of an existing DataFrame (spreadsheet.xlsx), but I...
Read more >
What's new in 1.4.0 (January 22, 2022)
Series.ewm() and DataFrame.ewm() now support a method argument with a ... based algos such as DataFrameGroupBy.value_counts() , DataFrameGroupBy.count() and ...
Read more >
How to Use Pandas GroupBy, Counts and Value Counts
In this post, you'll learn how to use Pandas groupby, counts, and value_counts on your Pandas DataFrames for fast and powerful data ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found