Skip to content

Fix annotations for DataFrame.apply() including additional overloads #448

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

Merged
merged 5 commits into from
Dec 5, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
8 changes: 7 additions & 1 deletion pandas-stubs/_typing.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ from typing import (
Iterator,
Literal,
Mapping,
MutableSequence,
Optional,
Protocol,
Sequence,
Expand Down Expand Up @@ -151,10 +152,15 @@ num: TypeAlias = complex
SeriesAxisType: TypeAlias = Literal[
"index", 0
] # Restricted subset of _AxisType for series
AxisType: TypeAlias = Literal["columns", "index", 0, 1]
AxisTypeIndex: TypeAlias = Literal["index", 0]
AxisTypeColumn: TypeAlias = Literal["columns", 1]
AxisType: TypeAlias = AxisTypeIndex | AxisTypeColumn
DtypeNp = TypeVar("DtypeNp", bound=np.dtype[np.generic])
KeysArgType: TypeAlias = Any
ListLike = TypeVar("ListLike", Sequence, np.ndarray, "Series", "Index")
ListLikeExceptSeriesAndStr = TypeVar(
"ListLikeExceptSeriesAndStr", MutableSequence, np.ndarray, tuple, "Index"
)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I agree, the name is awful, but do we have a better way to exclude str from list-likes?

Copy link
Collaborator

Choose a reason for hiding this comment

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

I don't know of any

ListLikeU: TypeAlias = Union[Sequence, np.ndarray, Series, Index]
StrLike: TypeAlias = Union[str, np.str_]
Scalar: TypeAlias = Union[
Expand Down
116 changes: 110 additions & 6 deletions pandas-stubs/core/frame.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ from pandas._typing import (
Axes,
Axis,
AxisType,
AxisTypeColumn,
AxisTypeIndex,
CalculationMethod,
ColspaceArgType,
CompressionOptions,
Expand All @@ -82,6 +84,7 @@ from pandas._typing import (
Label,
Level,
ListLike,
ListLikeExceptSeriesAndStr,
ListLikeU,
MaskType,
MergeHow,
Expand Down Expand Up @@ -1086,36 +1089,137 @@ class DataFrame(NDFrame, OpsMixin):
*args,
**kwargs,
) -> DataFrame: ...

# apply() overloads with default result_type of None, and is indifferent to axis
@overload
def apply(
self,
f: Callable[..., Series],
f: Callable[..., ListLikeExceptSeriesAndStr | Series],
axis: AxisTypeIndex = ...,
raw: _bool = ...,
result_type: Literal[None] = ...,
args=...,
**kwargs,
) -> DataFrame: ...
@overload
def apply(
self,
f: Callable[..., S1],
axis: AxisTypeIndex = ...,
raw: _bool = ...,
result_type: Literal[None] = ...,
args=...,
**kwargs,
) -> Series[S1]: ...

# apply() overloads with keyword result_type, and axis does not matter
@overload
def apply(
self,
f: Callable[..., S1],
axis: AxisType = ...,
raw: _bool = ...,
args=...,
*,
result_type: Literal["expand", "reduce"],
**kwargs,
) -> Series[S1]: ...
@overload
def apply(
self,
f: Callable[..., ListLikeExceptSeriesAndStr | Series],
axis: AxisType = ...,
raw: _bool = ...,
result_type: Literal["expand", "reduce", "broadcast"] | None = ...,
args=...,
*,
result_type: Literal["expand"],
**kwargs,
) -> DataFrame: ...
@overload
def apply(
self,
f: Callable[..., Scalar],
f: Callable[..., ListLikeExceptSeriesAndStr],
axis: AxisType = ...,
raw: _bool = ...,
result_type: Literal["expand", "reduce"] | None = ...,
args=...,
*,
result_type: Literal["reduce"],
**kwargs,
) -> Series: ...
@overload
def apply(
self,
f: Callable[..., Scalar],
result_type: Literal["broadcast"],
f: Callable[..., ListLikeExceptSeriesAndStr | Series | Scalar],
axis: AxisType = ...,
raw: _bool = ...,
args=...,
*,
result_type: Literal["broadcast"],
**kwargs,
) -> DataFrame: ...

# apply() overloads with keyword result_type, and axis does matter
@overload
def apply(
self,
f: Callable[..., Series],
axis: AxisTypeIndex = ...,
raw: _bool = ...,
args=...,
*,
result_type: Literal["reduce"],
**kwargs,
) -> Series: ...

# apply() overloads with default result_type of None, and keyword axis=1 matters
@overload
def apply(
self,
f: Callable[..., S1],
raw: _bool = ...,
result_type: Literal[None] = ...,
args=...,
*,
axis: AxisTypeColumn,
**kwargs,
) -> Series[S1]: ...
@overload
def apply(
self,
f: Callable[..., ListLikeExceptSeriesAndStr],
raw: _bool = ...,
result_type: Literal[None] = ...,
args=...,
*,
axis: AxisTypeColumn,
**kwargs,
) -> Series: ...
@overload
def apply(
self,
f: Callable[..., Series],
raw: _bool = ...,
result_type: Literal[None] = ...,
args=...,
*,
axis: AxisTypeColumn,
**kwargs,
) -> DataFrame: ...

# apply() overloads with keyword axis=1 and keyword result_type
@overload
def apply(
self,
f: Callable[..., Series],
raw: _bool = ...,
args=...,
*,
axis: AxisTypeColumn,
result_type: Literal["reduce"],
**kwargs,
) -> DataFrame: ...

# Add spacing between apply() overloads and remaining annotations
def applymap(
self, func: Callable, na_action: Literal["ignore"] | None = ..., **kwargs
) -> DataFrame: ...
Expand Down
Loading