Skip to content

Commit 63c4567

Browse files
Add explicit type parameters to arguments of df.apply (#953)
* Add explicit Any type parameters for arguments used in df.apply * Add any to args, kwargs and Mapping overloads
1 parent 3db439d commit 63c4567

File tree

2 files changed

+41
-39
lines changed

2 files changed

+41
-39
lines changed

pandas-stubs/_typing.pyi

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -490,8 +490,8 @@ Axis: TypeAlias = AxisIndex | AxisColumn
490490
DtypeNp = TypeVar("DtypeNp", bound=np.dtype[np.generic])
491491
KeysArgType: TypeAlias = Any
492492
ListLike = TypeVar("ListLike", Sequence, np.ndarray, Series, Index)
493-
ListLikeExceptSeriesAndStr = TypeVar(
494-
"ListLikeExceptSeriesAndStr", MutableSequence, np.ndarray, tuple, Index
493+
ListLikeExceptSeriesAndStr: TypeAlias = (
494+
MutableSequence[Any] | np.ndarray[Any, Any] | tuple[Any, ...] | Index[Any]
495495
)
496496
ListLikeU: TypeAlias = Sequence | np.ndarray | Series | Index
497497
ListLikeHashable: TypeAlias = (

pandas-stubs/core/frame.pyi

Lines changed: 39 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1250,12 +1250,12 @@ class DataFrame(NDFrame, OpsMixin):
12501250
@overload
12511251
def apply(
12521252
self,
1253-
f: Callable[..., ListLikeExceptSeriesAndStr | Series],
1253+
f: Callable[..., ListLikeExceptSeriesAndStr | Series[Any]],
12541254
axis: AxisIndex = ...,
12551255
raw: _bool = ...,
12561256
result_type: None = ...,
1257-
args=...,
1258-
**kwargs,
1257+
args: Any = ...,
1258+
**kwargs: Any,
12591259
) -> DataFrame: ...
12601260
@overload
12611261
def apply(
@@ -1264,21 +1264,21 @@ class DataFrame(NDFrame, OpsMixin):
12641264
axis: AxisIndex = ...,
12651265
raw: _bool = ...,
12661266
result_type: None = ...,
1267-
args=...,
1268-
**kwargs,
1267+
args: Any = ...,
1268+
**kwargs: Any,
12691269
) -> Series[S1]: ...
12701270
# Since non-scalar type T is not supported in Series[T],
12711271
# we separate this overload from the above one
12721272
@overload
12731273
def apply(
12741274
self,
1275-
f: Callable[..., Mapping],
1275+
f: Callable[..., Mapping[Any, Any]],
12761276
axis: AxisIndex = ...,
12771277
raw: _bool = ...,
12781278
result_type: None = ...,
1279-
args=...,
1280-
**kwargs,
1281-
) -> Series: ...
1279+
args: Any = ...,
1280+
**kwargs: Any,
1281+
) -> Series[Any]: ...
12821282

12831283
# apply() overloads with keyword result_type, and axis does not matter
12841284
@overload
@@ -1287,57 +1287,59 @@ class DataFrame(NDFrame, OpsMixin):
12871287
f: Callable[..., S1],
12881288
axis: Axis = ...,
12891289
raw: _bool = ...,
1290-
args=...,
1290+
args: Any = ...,
12911291
*,
12921292
result_type: Literal["expand", "reduce"],
1293-
**kwargs,
1293+
**kwargs: Any,
12941294
) -> Series[S1]: ...
12951295
@overload
12961296
def apply(
12971297
self,
1298-
f: Callable[..., ListLikeExceptSeriesAndStr | Series | Mapping],
1298+
f: Callable[..., ListLikeExceptSeriesAndStr | Series[Any] | Mapping[Any, Any]],
12991299
axis: Axis = ...,
13001300
raw: _bool = ...,
1301-
args=...,
1301+
args: Any = ...,
13021302
*,
13031303
result_type: Literal["expand"],
1304-
**kwargs,
1304+
**kwargs: Any,
13051305
) -> DataFrame: ...
13061306
@overload
13071307
def apply(
13081308
self,
1309-
f: Callable[..., ListLikeExceptSeriesAndStr | Mapping],
1309+
f: Callable[..., ListLikeExceptSeriesAndStr | Mapping[Any, Any]],
13101310
axis: Axis = ...,
13111311
raw: _bool = ...,
1312-
args=...,
1312+
args: Any = ...,
13131313
*,
13141314
result_type: Literal["reduce"],
1315-
**kwargs,
1316-
) -> Series: ...
1315+
**kwargs: Any,
1316+
) -> Series[Any]: ...
13171317
@overload
13181318
def apply(
13191319
self,
1320-
f: Callable[..., ListLikeExceptSeriesAndStr | Series | Scalar | Mapping],
1320+
f: Callable[
1321+
..., ListLikeExceptSeriesAndStr | Series[Any] | Scalar | Mapping[Any, Any]
1322+
],
13211323
axis: Axis = ...,
13221324
raw: _bool = ...,
1323-
args=...,
1325+
args: Any = ...,
13241326
*,
13251327
result_type: Literal["broadcast"],
1326-
**kwargs,
1328+
**kwargs: Any,
13271329
) -> DataFrame: ...
13281330

13291331
# apply() overloads with keyword result_type, and axis does matter
13301332
@overload
13311333
def apply(
13321334
self,
1333-
f: Callable[..., Series],
1335+
f: Callable[..., Series[Any]],
13341336
axis: AxisIndex = ...,
13351337
raw: _bool = ...,
1336-
args=...,
1338+
args: Any = ...,
13371339
*,
13381340
result_type: Literal["reduce"],
1339-
**kwargs,
1340-
) -> Series: ...
1341+
**kwargs: Any,
1342+
) -> Series[Any]: ...
13411343

13421344
# apply() overloads with default result_type of None, and keyword axis=1 matters
13431345
@overload
@@ -1346,45 +1348,45 @@ class DataFrame(NDFrame, OpsMixin):
13461348
f: Callable[..., S1],
13471349
raw: _bool = ...,
13481350
result_type: None = ...,
1349-
args=...,
1351+
args: Any = ...,
13501352
*,
13511353
axis: AxisColumn,
1352-
**kwargs,
1354+
**kwargs: Any,
13531355
) -> Series[S1]: ...
13541356
@overload
13551357
def apply(
13561358
self,
1357-
f: Callable[..., ListLikeExceptSeriesAndStr | Mapping],
1359+
f: Callable[..., ListLikeExceptSeriesAndStr | Mapping[Any, Any]],
13581360
raw: _bool = ...,
13591361
result_type: None = ...,
1360-
args=...,
1362+
args: Any = ...,
13611363
*,
13621364
axis: AxisColumn,
1363-
**kwargs,
1364-
) -> Series: ...
1365+
**kwargs: Any,
1366+
) -> Series[Any]: ...
13651367
@overload
13661368
def apply(
13671369
self,
1368-
f: Callable[..., Series],
1370+
f: Callable[..., Series[Any]],
13691371
raw: _bool = ...,
13701372
result_type: None = ...,
1371-
args=...,
1373+
args: Any = ...,
13721374
*,
13731375
axis: AxisColumn,
1374-
**kwargs,
1376+
**kwargs: Any,
13751377
) -> DataFrame: ...
13761378

13771379
# apply() overloads with keyword axis=1 and keyword result_type
13781380
@overload
13791381
def apply(
13801382
self,
1381-
f: Callable[..., Series],
1383+
f: Callable[..., Series[Any]],
13821384
raw: _bool = ...,
1383-
args=...,
1385+
args: Any = ...,
13841386
*,
13851387
axis: AxisColumn,
13861388
result_type: Literal["reduce"],
1387-
**kwargs,
1389+
**kwargs: Any,
13881390
) -> DataFrame: ...
13891391

13901392
# Add spacing between apply() overloads and remaining annotations

0 commit comments

Comments
 (0)