Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
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
79 changes: 25 additions & 54 deletions pandas-stubs/core/reshape/pivot.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,6 @@ from pandas.core.series import Series
from pandas._typing import (
AnyArrayLike,
ArrayLike,
HashableT1,
HashableT2,
HashableT3,
Label,
Scalar,
ScalarT,
Expand All @@ -33,12 +30,16 @@ from pandas._typing import (
)

_PivotAggCallable: TypeAlias = Callable[[Series], ScalarT]

_PivotAggFunc: TypeAlias = (
_PivotAggCallable[ScalarT]
| np.ufunc
| Literal["mean", "sum", "count", "min", "max", "median", "std", "var"]
)
_AggFuncTypes: TypeAlias = (
_PivotAggFunc[ScalarT]
| Sequence[_PivotAggFunc[ScalarT]]
| Mapping[Any, _PivotAggFunc[ScalarT]]
)

_NonIterableHashable: TypeAlias = (
str
Expand All @@ -53,32 +54,22 @@ _NonIterableHashable: TypeAlias = (
| pd.Timedelta
)

_PivotTableIndexTypes: TypeAlias = (
Label | Sequence[HashableT1] | Series | Grouper | None
)
_PivotTableIndexTypes: TypeAlias = Label | Sequence[Hashable] | Series | Grouper | None
_PivotTableColumnsTypes: TypeAlias = (
Label | Sequence[HashableT2] | Series | Grouper | None
Label | Sequence[Hashable] | Series | Grouper | None
)
_PivotTableValuesTypes: TypeAlias = Label | Sequence[HashableT3] | None
_PivotTableValuesTypes: TypeAlias = Label | Sequence[Hashable] | None

_ExtendedAnyArrayLike: TypeAlias = AnyArrayLike | ArrayLike
_Values: TypeAlias = SequenceNotStr[Any] | _ExtendedAnyArrayLike

@overload
def pivot_table(
data: DataFrame,
values: _PivotTableValuesTypes[
Hashable # ty: ignore[invalid-type-arguments]
] = None,
index: _PivotTableIndexTypes[Hashable] = None, # ty: ignore[invalid-type-arguments]
columns: _PivotTableColumnsTypes[
Hashable # ty: ignore[invalid-type-arguments]
] = None,
aggfunc: (
_PivotAggFunc[Scalar]
| Sequence[_PivotAggFunc[Scalar]]
| Mapping[Any, _PivotAggFunc[Scalar]]
) = "mean",
values: _PivotTableValuesTypes = None,
index: _PivotTableIndexTypes = None,
columns: _PivotTableColumnsTypes = None,
aggfunc: _AggFuncTypes[Scalar] = "mean",
fill_value: Scalar | None = None,
margins: bool = False,
dropna: bool = True,
Expand All @@ -91,21 +82,11 @@ def pivot_table(
@overload
def pivot_table(
data: DataFrame,
values: _PivotTableValuesTypes[
Hashable # ty: ignore[invalid-type-arguments]
] = None,
values: _PivotTableValuesTypes = None,
*,
index: Grouper,
columns: (
_PivotTableColumnsTypes[Hashable] # ty: ignore[invalid-type-arguments]
| np_ndarray
| Index[Any]
) = None,
aggfunc: (
_PivotAggFunc[Scalar]
| Sequence[_PivotAggFunc[Scalar]]
| Mapping[Any, _PivotAggFunc[Scalar]]
) = "mean",
columns: _PivotTableColumnsTypes | np_ndarray | Index[Any] = None,
aggfunc: _AggFuncTypes[Scalar] = "mean",
fill_value: Scalar | None = None,
margins: bool = False,
dropna: bool = True,
Expand All @@ -116,21 +97,11 @@ def pivot_table(
@overload
def pivot_table(
data: DataFrame,
values: _PivotTableValuesTypes[
Hashable # ty: ignore[invalid-type-arguments]
] = None,
index: (
_PivotTableIndexTypes[Hashable] # ty: ignore[invalid-type-arguments]
| np_ndarray
| Index[Any]
) = None,
values: _PivotTableValuesTypes = None,
index: _PivotTableIndexTypes | np_ndarray | Index[Any] = None,
*,
columns: Grouper,
aggfunc: (
_PivotAggFunc[Scalar]
| Sequence[_PivotAggFunc[Scalar]]
| Mapping[Any, _PivotAggFunc[Scalar]]
) = "mean",
aggfunc: _AggFuncTypes[Scalar] = "mean",
fill_value: Scalar | None = None,
margins: bool = False,
dropna: bool = True,
Expand All @@ -141,17 +112,17 @@ def pivot_table(
def pivot(
data: DataFrame,
*,
index: _NonIterableHashable | Sequence[HashableT1] = ...,
columns: _NonIterableHashable | Sequence[HashableT2] = ...,
values: _NonIterableHashable | Sequence[HashableT3] = ...,
index: _NonIterableHashable | Sequence[Hashable] = ...,
columns: _NonIterableHashable | Sequence[Hashable] = ...,
values: _NonIterableHashable | Sequence[Hashable] = ...,
Comment on lines +115 to +117
Copy link
Contributor Author

@cmp0xff cmp0xff Dec 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see the three different type variables HashableT1 ... HashableT3 being helpful in anyway here. Changing to Hashable.

) -> DataFrame: ...
@overload
def crosstab(
index: _Values | list[_Values],
columns: _Values | list[_Values],
values: _Values,
rownames: list[HashableT1] | None = ...,
colnames: list[HashableT2] | None = ...,
rownames: SequenceNotStr[Hashable] | None = ...,
colnames: SequenceNotStr[Hashable] | None = ...,
*,
aggfunc: str | np.ufunc | Callable[[Series], float],
margins: bool = ...,
Expand All @@ -164,8 +135,8 @@ def crosstab(
index: _Values | list[_Values],
columns: _Values | list[_Values],
values: None = None,
rownames: list[HashableT1] | None = ...,
colnames: list[HashableT2] | None = ...,
rownames: SequenceNotStr[Hashable] | None = ...,
colnames: SequenceNotStr[Hashable] | None = ...,
aggfunc: None = None,
margins: bool = ...,
margins_name: str = ...,
Expand Down
3 changes: 1 addition & 2 deletions tests/test_pandas.py
Original file line number Diff line number Diff line change
Expand Up @@ -1716,8 +1716,7 @@ def m2(x: pd.Series) -> int:
colnames: list[tuple[str]] = [("a",)]
check(
assert_type(
pd.crosstab(a, b, colnames=colnames, rownames=rownames),
pd.DataFrame,
pd.crosstab(a, b, colnames=colnames, rownames=rownames), pd.DataFrame
),
pd.DataFrame,
)
Expand Down