TYP:  Update type results for Groupby.count() and Groupby.size()
State: closed

- [x] closes #45875
- [ ] [Tests added and passed] - None - it is a typing issue
- [x] All [code checks passed](https://pandas.pydata.org/pandas-docs/dev/development/contributing_codebase.html#pre-commit).
- [ ] Added an entry in the latest `doc/source/whatsnew/vX.X.X.rst` file if fixing a bug or adding a new feature.
   N/A 

Here's a test using `mypy` that reveals the issue.  
```python
import pandas as pd

ind = pd.MultiIndex.from_product([["a", "b"], ["x", "y"]], names=["ab", "xy"])
df = pd.DataFrame({"x": [1, 2, 3, 4], "y": [4, 5, 6, 7]})
s = pd.Series([1, 2, 3, 4], index=ind)

cnt_df = df.groupby("x").count()

siz_df = df.groupby("x").size()

app_df = df.groupby("x").apply(sum)

cnt_s = s.groupby("ab").count()

siz_s = s.groupby("xy").size()

app_s = s.groupby("xy").apply(sum)

reveal_type(cnt_df)
reveal_type(siz_df)
reveal_type(app_df)
reveal_type(cnt_s)
reveal_type(siz_s)
reveal_type(app_s)
```

Before this PR, the result of `mypy` on the `reveal_type` lines is:
```
issue45875.py:17: note: Revealed type is "Union[pandas.core.series.Series, pandas.core.frame.DataFrame]"
issue45875.py:18: note: Revealed type is "Union[pandas.core.frame.DataFrame, pandas.core.series.Series]"
issue45875.py:19: note: Revealed type is "Any"
issue45875.py:20: note: Revealed type is "Union[pandas.core.series.Series, pandas.core.frame.DataFrame]"
issue45875.py:21: note: Revealed type is "Union[pandas.core.frame.DataFrame, pandas.core.series.Series]"
issue45875.py:22: note: Revealed type is "Any"
```

After this PR, the result is:
```
issue45875.py:19: note: Revealed type is "pandas.core.frame.DataFrame"
issue45875.py:20: note: Revealed type is "pandas.core.frame.DataFrame"
issue45875.py:21: note: Revealed type is "pandas.core.frame.DataFrame"
issue45875.py:22: note: Revealed type is "pandas.core.series.Series"
issue45875.py:23: note: Revealed type is "pandas.core.series.Series"
issue45875.py:24: note: Revealed type is "pandas.core.series.Series"
```

This will be needed as we work on the general typing issues.



Labels:
name: Typing
description: type annotations, mypy/pyright type checking

Comments:
> Here's a test using `mypy` that reveals the issue.

This code sample raises `ValueError: No axis named 1 for object type Series` in the python interpreter
> > Here's a test using `mypy` that reveals the issue.
> 
> This code sample raises `ValueError: No axis named 1 for object type Series` in the python interpreter

I fixed it.  Edited above.

@simonjayhawkins @jbrockmendel just did a newer commit that takes advantage of the generic aspect of the `GroupBy` class.  Requires a couple of casts, but I think this is cleaner.
> After this PR, the result is:
> 
> ```
> issue45875.py:19: note: Revealed type is "pandas.core.frame.DataFrame"
> issue45875.py:20: note: Revealed type is "pandas.core.frame.DataFrame"
> issue45875.py:21: note: Revealed type is "pandas.core.frame.DataFrame"
> issue45875.py:22: note: Revealed type is "pandas.core.series.Series"
> issue45875.py:23: note: Revealed type is "pandas.core.series.Series"
> issue45875.py:24: note: Revealed type is "pandas.core.series.Series"
> ```

running code sample through interpreter gives...
```
   y
x   
1  1
2  1
3  1
4  1
<class 'pandas.core.frame.DataFrame'>
x
1    1
2    1
3    1
4    1
dtype: int64
<class 'pandas.core.series.Series'>
   x  y
x      
1  1  4
2  2  5
3  3  6
4  4  7
<class 'pandas.core.frame.DataFrame'>
ab
a    2
b    2
dtype: int64
<class 'pandas.core.series.Series'>
xy
x    2
y    2
dtype: int64
<class 'pandas.core.series.Series'>
xy
x    4
y    6
dtype: int64
<class 'pandas.core.series.Series'>
```
For the record, here is what I used for testing:
```python
import pandas as pd
from typing import TYPE_CHECKING

if not TYPE_CHECKING:

    def reveal_type(*args, **kwargs):
        pass


ind = pd.MultiIndex.from_product([["a", "b"], ["x", "y"]], names=["ab", "xy"])
df = pd.DataFrame({"x": [1, 2, 3, 4], "y": [4, 5, 6, 7]})
s = pd.Series([1, 2, 3, 4], index=ind)

cnt_df = df.groupby("x").count()
print(type(cnt_df))

cnt_df_s = df.groupby("x").y.count()
print(type(cnt_df_s))

# siz_df = df.groupby("x").size()
# print(type(siz_df))

app_df = df.groupby("x").apply(sum)
print(type(app_df))

cnt_df_a = df.groupby("x", as_index=False).count()
print(type(cnt_df_a))

cnt_df_s_a = df.groupby("x", as_index=False).y.count()
print(type(cnt_df_s_a))

# siz_df_a = df.groupby("x", as_index=False).size()
# print(type(siz_df_a))

app_df_a = df.groupby("x", as_index=False).apply(sum)
print(type(app_df_a))

cnt_s = s.groupby("ab").count()
print(type(cnt_s))

# siz_s = s.groupby("xy").size()
# print(type(siz_s))

app_s = s.groupby("xy").apply(sum)
print(type(app_s))

reveal_type(cnt_df)
reveal_type(cnt_df_s)
# reveal_type(siz_df)
reveal_type(app_df)
reveal_type(cnt_df_a)
reveal_type(cnt_df_s_a)
# reveal_type(siz_df_a)
reveal_type(app_df_a)
reveal_type(cnt_s)
# reveal_type(siz_s)
reveal_type(app_s)
```

I took out the `size()` test because it is dynamically typed based on `as_index`.  Running this program yields:

```
<class 'pandas.core.frame.DataFrame'>
<class 'pandas.core.series.Series'>
<class 'pandas.core.frame.DataFrame'>
<class 'pandas.core.frame.DataFrame'>
<class 'pandas.core.frame.DataFrame'>
<class 'pandas.core.frame.DataFrame'>
<class 'pandas.core.series.Series'>
<class 'pandas.core.series.Series'>
```
`mypy` reports:
```
issue45875.py:47: note: Revealed type is "pandas.core.frame.DataFrame*"
issue45875.py:48: note: Revealed type is "Any"
issue45875.py:50: note: Revealed type is "pandas.core.frame.DataFrame*"
issue45875.py:51: note: Revealed type is "pandas.core.frame.DataFrame*"
issue45875.py:52: note: Revealed type is "Any"
issue45875.py:54: note: Revealed type is "pandas.core.frame.DataFrame*"
issue45875.py:55: note: Revealed type is "pandas.core.series.Series*"
issue45875.py:57: note: Revealed type is "pandas.core.series.Series"
```

The two `Any` values from `mypy` are because we can't type `df.groupby("x").y` or `df.groupby("x", as_index=False).y`

@Dr-Irv status here
@jreback all green now - merged with latest main