Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GH1025 Typehint DataFrame.groupby.value_counts method #1027

Merged
merged 2 commits into from
Nov 8, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions pandas-stubs/core/groupby/generic.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -300,9 +300,10 @@ class DataFrameGroupBy(GroupBy[DataFrame], Generic[ByT, _TT]):
backend: str | None = ...,
**kwargs,
) -> PlotAxes | Series: ... # Series[PlotAxes]
# def size(self: DataFrameGroupBy[ByT, Literal[True]]) -> Series[int]: ...
loicdiridollou marked this conversation as resolved.
Show resolved Hide resolved
@overload
def value_counts(
self,
self: DataFrameGroupBy[ByT, Literal[True]],
subset: ListLike | None = ...,
normalize: Literal[False] = ...,
sort: bool = ...,
Expand All @@ -311,13 +312,31 @@ class DataFrameGroupBy(GroupBy[DataFrame], Generic[ByT, _TT]):
) -> Series[int]: ...
@overload
def value_counts(
self,
self: DataFrameGroupBy[ByT, Literal[True]],
subset: ListLike | None,
normalize: Literal[True],
sort: bool = ...,
ascending: bool = ...,
dropna: bool = ...,
) -> Series[float]: ...
@overload
def value_counts(
self: DataFrameGroupBy[ByT, Literal[False]],
subset: ListLike | None = ...,
normalize: Literal[False] = ...,
sort: bool = ...,
ascending: bool = ...,
dropna: bool = ...,
) -> DataFrame: ...
@overload
def value_counts(
self: DataFrameGroupBy[ByT, Literal[False]],
subset: ListLike | None,
normalize: Literal[True],
sort: bool = ...,
ascending: bool = ...,
dropna: bool = ...,
) -> DataFrame: ...
def take(
self, indices: TakeIndexer, axis: Axis | None | NoDefault = ..., **kwargs
) -> DataFrame: ...
Expand Down
20 changes: 20 additions & 0 deletions tests/test_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -1026,6 +1026,7 @@ def test_types_pivot_table() -> None:


def test_types_groupby_as_index() -> None:
"""Test type of groupby.size method depending on `as_index`."""
df = pd.DataFrame({"a": [1, 2, 3]})
check(
assert_type(
Expand All @@ -1043,6 +1044,25 @@ def test_types_groupby_as_index() -> None:
)


def test_types_groupby_as_index_value_counts() -> None:
"""Test type of groupby.value_counts method depending on `as_index`."""
df = pd.DataFrame({"a": [1, 2, 3]})
check(
assert_type(
df.groupby("a", as_index=False).value_counts(),
pd.DataFrame,
),
pd.DataFrame,
)
check(
assert_type(
df.groupby("a", as_index=True).value_counts(),
"pd.Series[int]",
),
pd.Series,
)


def test_types_groupby_size() -> None:
"""Test for GH886."""
data = [
Expand Down
Loading