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 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
4 changes: 4 additions & 0 deletions 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 @@ -155,6 +156,9 @@ AxisType: TypeAlias = Literal["columns", "index", 0, 1]
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
42 changes: 38 additions & 4 deletions pandas-stubs/core/frame.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ from pandas._typing import (
Label,
Level,
ListLike,
ListLikeExceptSeriesAndStr,
ListLikeU,
MaskType,
MergeHow,
Expand Down Expand Up @@ -1087,12 +1088,22 @@ class DataFrame(NDFrame, OpsMixin):
**kwargs,
) -> DataFrame: ...
@overload
def apply(
self,
f: Callable[..., ListLikeExceptSeriesAndStr],
axis: AxisType = ...,
raw: _bool = ...,
result_type: Literal[None] = ...,
args=...,
**kwargs,
) -> DataFrame: ...
@overload
def apply(
self,
f: Callable[..., Series],
axis: AxisType = ...,
raw: _bool = ...,
result_type: Literal["expand", "reduce", "broadcast"] | None = ...,
result_type: Literal[None] = ...,
args=...,
**kwargs,
) -> DataFrame: ...
Expand All @@ -1102,18 +1113,41 @@ class DataFrame(NDFrame, OpsMixin):
f: Callable[..., Scalar],
axis: AxisType = ...,
raw: _bool = ...,
result_type: Literal["expand", "reduce"] | None = ...,
result_type: Literal[None] = ...,
args=...,
**kwargs,
) -> Series: ...
@overload
def apply(
self,
f: Callable[..., Scalar],
result_type: Literal["broadcast"],
f: Callable[..., ListLikeExceptSeriesAndStr | Series | str | Scalar],
axis: AxisType = ...,
raw: _bool = ...,
args=...,
*,
result_type: Literal["reduce"],
**kwargs,
) -> Series: ...
@overload
def apply(
self,
f: Callable[..., ListLikeExceptSeriesAndStr | Series],
axis: AxisType = ...,
raw: _bool = ...,
args=...,
*,
result_type: Literal["expand"],
**kwargs,
) -> DataFrame: ...
@overload
def apply(
self,
f: Callable[..., ListLikeExceptSeriesAndStr | Series | str | Scalar],
axis: AxisType = ...,
raw: _bool = ...,
args=...,
*,
result_type: Literal["broadcast"],
**kwargs,
) -> DataFrame: ...
def applymap(
Expand Down
10 changes: 10 additions & 0 deletions tests/test_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,16 @@ def gethead(s: pd.Series, y: int) -> pd.Series:

check(assert_type(df.apply(gethead, args=(4,)), pd.DataFrame), pd.DataFrame)

def returns_tuple(x: pd.Series) -> tuple[str, str]:
return ("a", "b")

check(
assert_type(
df.apply(returns_tuple, axis=1, result_type="expand"), pd.DataFrame
),
pd.DataFrame,
)


def test_types_applymap() -> None:
df = pd.DataFrame(data={"col1": [2, 1], "col2": [3, 4]})
Expand Down