Skip to content

Commit 1c0f351

Browse files
committed
1 parent 76ee8bb commit 1c0f351

File tree

2 files changed

+43
-28
lines changed

2 files changed

+43
-28
lines changed

pandas-stubs/_typing.pyi

Lines changed: 6 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -824,30 +824,8 @@ MaskType: TypeAlias = Series[bool] | np_ndarray_bool | list[bool]
824824

825825
# Scratch types for generics
826826

827-
S1 = TypeVar(
828-
"S1",
829-
bound=str
830-
| bytes
831-
| datetime.date
832-
| datetime.time
833-
| bool
834-
| int
835-
| float
836-
| complex
837-
| Dtype
838-
| datetime.datetime # includes pd.Timestamp
839-
| datetime.timedelta # includes pd.Timedelta
840-
| Period
841-
| Interval
842-
| CategoricalDtype
843-
| BaseOffset
844-
| list[str],
845-
default=Any,
846-
)
847-
848-
S2 = TypeVar(
849-
"S2",
850-
bound=str
827+
SeriesDType: TypeAlias = (
828+
str
851829
| bytes
852830
| datetime.date
853831
| datetime.time
@@ -862,8 +840,11 @@ S2 = TypeVar(
862840
| Interval
863841
| CategoricalDtype
864842
| BaseOffset
865-
| list[str],
843+
| list[str]
866844
)
845+
S1 = TypeVar("S1", bound=SeriesDType, default=Any)
846+
# Like S1, but without `default=Any`.
847+
S2 = TypeVar("S2", bound=SeriesDType)
867848

868849
IndexingInt: TypeAlias = (
869850
int | np.int_ | np.integer | np.unsignedinteger | np.signedinteger | np.int8

pandas-stubs/core/frame.pyi

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ from pandas._typing import (
136136
ScalarT,
137137
SequenceNotStr,
138138
SeriesByT,
139+
SeriesDType,
139140
SortKind,
140141
StataDateFormat,
141142
StorageOptions,
@@ -1391,13 +1392,23 @@ class DataFrame(NDFrame, OpsMixin, _GetItemHack):
13911392
@overload
13921393
def apply(
13931394
self,
1394-
f: Callable[..., S2 | NAType],
1395+
f: Callable[..., S2],
13951396
axis: AxisIndex = ...,
13961397
raw: _bool = ...,
13971398
result_type: None = ...,
13981399
args: Any = ...,
13991400
**kwargs: Any,
14001401
) -> Series[S2]: ...
1402+
@overload
1403+
def apply(
1404+
self,
1405+
f: Callable[..., SeriesDType | NAType],
1406+
axis: AxisIndex = ...,
1407+
raw: _bool = ...,
1408+
result_type: None = ...,
1409+
args: Any = ...,
1410+
**kwargs: Any,
1411+
) -> Series: ...
14011412
# Since non-scalar type T is not supported in Series[T],
14021413
# we separate this overload from the above one
14031414
@overload
@@ -1415,7 +1426,7 @@ class DataFrame(NDFrame, OpsMixin, _GetItemHack):
14151426
@overload
14161427
def apply(
14171428
self,
1418-
f: Callable[..., S2 | NAType],
1429+
f: Callable[..., S2],
14191430
axis: Axis = ...,
14201431
raw: _bool = ...,
14211432
args: Any = ...,
@@ -1424,6 +1435,17 @@ class DataFrame(NDFrame, OpsMixin, _GetItemHack):
14241435
**kwargs: Any,
14251436
) -> Series[S2]: ...
14261437
@overload
1438+
def apply(
1439+
self,
1440+
f: Callable[..., SeriesDType | NAType],
1441+
axis: Axis = ...,
1442+
raw: _bool = ...,
1443+
args: Any = ...,
1444+
*,
1445+
result_type: Literal["expand", "reduce"],
1446+
**kwargs: Any,
1447+
) -> Series: ...
1448+
@overload
14271449
def apply(
14281450
self,
14291451
f: Callable[..., ListLikeExceptSeriesAndStr | Series | Mapping[Any, Any]],
@@ -1473,10 +1495,11 @@ class DataFrame(NDFrame, OpsMixin, _GetItemHack):
14731495
) -> Series: ...
14741496

14751497
# apply() overloads with default result_type of None, and keyword axis=1 matters
1498+
# Use S2 (TypeVar without the `Any` default) to avoid MyPy issue https://github.com/python/mypy/issues/19182.
14761499
@overload
14771500
def apply(
14781501
self,
1479-
f: Callable[..., S2 | NAType],
1502+
f: Callable[..., S2],
14801503
raw: _bool = ...,
14811504
result_type: None = ...,
14821505
args: Any = ...,
@@ -1485,6 +1508,17 @@ class DataFrame(NDFrame, OpsMixin, _GetItemHack):
14851508
**kwargs: Any,
14861509
) -> Series[S2]: ...
14871510
@overload
1511+
def apply(
1512+
self,
1513+
f: Callable[..., SeriesDType | NAType],
1514+
raw: _bool = ...,
1515+
result_type: None = ...,
1516+
args: Any = ...,
1517+
*,
1518+
axis: AxisColumn,
1519+
**kwargs: Any,
1520+
) -> Series: ...
1521+
@overload
14881522
def apply(
14891523
self,
14901524
f: Callable[..., ListLikeExceptSeriesAndStr | Mapping[Any, Any]],

0 commit comments

Comments
 (0)