Skip to content

Commit 0a5115d

Browse files
authored
Complete signature of value_counts (#995)
For Index, Series, DataFrame
1 parent 5bf4b58 commit 0a5115d

File tree

6 files changed

+60
-6
lines changed

6 files changed

+60
-6
lines changed

pandas-stubs/core/base.pyi

+17-3
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,14 @@ from typing import (
77
Generic,
88
Literal,
99
final,
10+
overload,
1011
)
1112

1213
import numpy as np
13-
from pandas import Index
14+
from pandas import (
15+
Index,
16+
Series,
17+
)
1418
from pandas.core.arraylike import OpsMixin
1519
from pandas.core.arrays import ExtensionArray
1620
from pandas.core.arrays.categorical import Categorical
@@ -76,14 +80,24 @@ class IndexOpsMixin(OpsMixin, Generic[S1]):
7680
def __iter__(self) -> Iterator[S1]: ...
7781
@property
7882
def hasnans(self) -> bool: ...
83+
@overload
84+
def value_counts(
85+
self,
86+
normalize: Literal[False] = ...,
87+
sort: bool = ...,
88+
ascending: bool = ...,
89+
bins=...,
90+
dropna: bool = ...,
91+
) -> Series[int]: ...
92+
@overload
7993
def value_counts(
8094
self,
81-
normalize: bool = ...,
95+
normalize: Literal[True],
8296
sort: bool = ...,
8397
ascending: bool = ...,
8498
bins=...,
8599
dropna: bool = ...,
86-
): ...
100+
) -> Series[float]: ...
87101
def nunique(self, dropna: bool = ...) -> int: ...
88102
@property
89103
def is_unique(self) -> bool: ...

pandas-stubs/core/frame.pyi

+11-1
Original file line numberDiff line numberDiff line change
@@ -993,14 +993,24 @@ class DataFrame(NDFrame, OpsMixin):
993993
ignore_index: _bool = ...,
994994
key: Callable | None = ...,
995995
) -> DataFrame | None: ...
996+
@overload
996997
def value_counts(
997998
self,
998999
subset: Sequence[Hashable] | None = ...,
999-
normalize: _bool = ...,
1000+
normalize: Literal[False] = ...,
10001001
sort: _bool = ...,
10011002
ascending: _bool = ...,
10021003
dropna: _bool = ...,
10031004
) -> Series[int]: ...
1005+
@overload
1006+
def value_counts(
1007+
self,
1008+
normalize: Literal[True],
1009+
subset: Sequence[Hashable] | None = ...,
1010+
sort: _bool = ...,
1011+
ascending: _bool = ...,
1012+
dropna: _bool = ...,
1013+
) -> Series[float]: ...
10041014
def nlargest(
10051015
self,
10061016
n: int,

pandas-stubs/core/series.pyi

+11-1
Original file line numberDiff line numberDiff line change
@@ -1477,14 +1477,24 @@ class Series(IndexOpsMixin[S1], NDFrame):
14771477
) -> Series[S1]: ...
14781478
def first_valid_index(self) -> Scalar: ...
14791479
def last_valid_index(self) -> Scalar: ...
1480+
@overload
14801481
def value_counts(
14811482
self,
1482-
normalize: _bool = ...,
1483+
normalize: Literal[False] = ...,
14831484
sort: _bool = ...,
14841485
ascending: _bool = ...,
14851486
bins: int | None = ...,
14861487
dropna: _bool = ...,
14871488
) -> Series[int]: ...
1489+
@overload
1490+
def value_counts(
1491+
self,
1492+
normalize: Literal[True],
1493+
sort: _bool = ...,
1494+
ascending: _bool = ...,
1495+
bins: int | None = ...,
1496+
dropna: _bool = ...,
1497+
) -> Series[float]: ...
14881498
def transpose(self, *args, **kwargs) -> Series[S1]: ...
14891499
@property
14901500
def T(self) -> Self: ...

tests/test_frame.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -610,7 +610,12 @@ def test_types_idxmax() -> None:
610610
# This was added in 1.1.0 https://pandas.pydata.org/docs/whatsnew/v1.1.0.html
611611
def test_types_value_counts() -> None:
612612
df = pd.DataFrame(data={"col1": [1, 2], "col2": [1, 4]})
613-
df.value_counts()
613+
check(assert_type(df.value_counts(), "pd.Series[int]"), pd.Series, np.integer)
614+
check(
615+
assert_type(df.value_counts(normalize=True), "pd.Series[float]"),
616+
pd.Series,
617+
float,
618+
)
614619

615620

616621
def test_types_unique() -> None:

tests/test_indexes.py

+10
Original file line numberDiff line numberDiff line change
@@ -1135,3 +1135,13 @@ def test_get_loc() -> None:
11351135
np.ndarray,
11361136
np.bool_,
11371137
)
1138+
1139+
1140+
def test_value_counts() -> None:
1141+
nmi = pd.Index(list("abcb"))
1142+
check(assert_type(nmi.value_counts(), "pd.Series[int]"), pd.Series, np.integer)
1143+
check(
1144+
assert_type(nmi.value_counts(normalize=True), "pd.Series[float]"),
1145+
pd.Series,
1146+
float,
1147+
)

tests/test_series.py

+5
Original file line numberDiff line numberDiff line change
@@ -557,6 +557,11 @@ def test_types_idxmax() -> None:
557557
def test_types_value_counts() -> None:
558558
s = pd.Series(["a", "b"])
559559
check(assert_type(s.value_counts(), "pd.Series[int]"), pd.Series, np.integer)
560+
check(
561+
assert_type(s.value_counts(normalize=True), "pd.Series[float]"),
562+
pd.Series,
563+
float,
564+
)
560565

561566

562567
def test_types_unique() -> None:

0 commit comments

Comments
 (0)