BUG: df.groupby(col, as_index=False).value_counts() returns a DataFrame but is annotated as Series
See original GitHub issueDescribe 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
- Provide a minimal runnable
pandas
example that is not properly checked by the stubs. - Indicate which type checker you are using (
mypy
orpyright
). - 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:
- Created a year ago
- Comments:11 (3 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I think
as_index
is kind of special and changes the return type of many functions, so there is also a third option of breakingSeriesGroupBy
andDataFrameGroupBy
into 2 different classes depending onas_index
and overloading the various functions which return them.Thanks, this issues was the cloest I got from Googling so I chime in here. 😝