Skip to content

Commit a7085c0

Browse files
authored
TYP: NDFrame.pipe, GroupBy.pipe etc. (#39093)
1 parent ee92c2f commit a7085c0

File tree

4 files changed

+44
-11
lines changed

4 files changed

+44
-11
lines changed

pandas/core/common.py

+15-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,18 @@
88
import contextlib
99
from functools import partial
1010
import inspect
11-
from typing import Any, Collection, Iterable, Iterator, List, Optional, Union, cast
11+
from typing import (
12+
Any,
13+
Callable,
14+
Collection,
15+
Iterable,
16+
Iterator,
17+
List,
18+
Optional,
19+
Tuple,
20+
Union,
21+
cast,
22+
)
1223
import warnings
1324

1425
import numpy as np
@@ -405,7 +416,9 @@ def random_state(state=None):
405416
)
406417

407418

408-
def pipe(obj, func, *args, **kwargs):
419+
def pipe(
420+
obj, func: Union[Callable[..., T], Tuple[Callable[..., T], str]], *args, **kwargs
421+
) -> T:
409422
"""
410423
Apply a function ``func`` to object ``obj`` either by passing obj as the
411424
first argument to the function or, in the case that the func is a tuple,

pandas/core/generic.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
NpDtype,
5050
Renamer,
5151
StorageOptions,
52+
T,
5253
TimedeltaConvertibleTypes,
5354
TimestampConvertibleTypes,
5455
ValueKeyFunc,
@@ -5359,7 +5360,12 @@ def sample(
53595360

53605361
@final
53615362
@doc(klass=_shared_doc_kwargs["klass"])
5362-
def pipe(self, func, *args, **kwargs):
5363+
def pipe(
5364+
self,
5365+
func: Union[Callable[..., T], Tuple[Callable[..., T], str]],
5366+
*args,
5367+
**kwargs,
5368+
) -> T:
53635369
r"""
53645370
Apply func(self, \*args, \*\*kwargs).
53655371

pandas/core/groupby/groupby.py

+12-5
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ class providing the base-class of operations.
66
(defined in pandas.core.groupby.generic)
77
expose these user-facing objects to provide specific functionality.
88
"""
9+
from __future__ import annotations
910

1011
from contextlib import contextmanager
1112
import datetime
@@ -44,6 +45,7 @@ class providing the base-class of operations.
4445
FrameOrSeriesUnion,
4546
IndexLabel,
4647
Scalar,
48+
T,
4749
final,
4850
)
4951
from pandas.compat.numpy import function as nv
@@ -475,7 +477,7 @@ def f(self):
475477

476478

477479
@contextmanager
478-
def group_selection_context(groupby: "BaseGroupBy") -> Iterator["BaseGroupBy"]:
480+
def group_selection_context(groupby: BaseGroupBy) -> Iterator[BaseGroupBy]:
479481
"""
480482
Set / reset the group_selection_context.
481483
"""
@@ -723,8 +725,8 @@ def _set_group_selection(self) -> None:
723725

724726
@final
725727
def _set_result_index_ordered(
726-
self, result: "OutputFrameOrSeries"
727-
) -> "OutputFrameOrSeries":
728+
self, result: OutputFrameOrSeries
729+
) -> OutputFrameOrSeries:
728730
# set the result index on the passed values object and
729731
# return the new object, xref 8046
730732

@@ -789,7 +791,12 @@ def __getattr__(self, attr: str):
789791
),
790792
)
791793
@Appender(_pipe_template)
792-
def pipe(self, func, *args, **kwargs):
794+
def pipe(
795+
self,
796+
func: Union[Callable[..., T], Tuple[Callable[..., T], str]],
797+
*args,
798+
**kwargs,
799+
) -> T:
793800
return com.pipe(self, func, *args, **kwargs)
794801

795802
plot = property(GroupByPlot)
@@ -3057,7 +3064,7 @@ def get_groupby(
30573064
by: Optional[_KeysArgType] = None,
30583065
axis: int = 0,
30593066
level=None,
3060-
grouper: "Optional[ops.BaseGrouper]" = None,
3067+
grouper: Optional[ops.BaseGrouper] = None,
30613068
exclusions=None,
30623069
selection=None,
30633070
as_index: bool = True,

pandas/core/resample.py

+10-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
from __future__ import annotations
2+
13
import copy
24
from datetime import timedelta
35
from textwrap import dedent
4-
from typing import Dict, Optional, Union, no_type_check
6+
from typing import Callable, Dict, Optional, Tuple, Union, no_type_check
57

68
import numpy as np
79

@@ -14,7 +16,7 @@
1416
Timestamp,
1517
to_offset,
1618
)
17-
from pandas._typing import TimedeltaConvertibleTypes, TimestampConvertibleTypes
19+
from pandas._typing import T, TimedeltaConvertibleTypes, TimestampConvertibleTypes
1820
from pandas.compat.numpy import function as nv
1921
from pandas.errors import AbstractMethodError
2022
from pandas.util._decorators import Appender, Substitution, doc
@@ -231,7 +233,12 @@ def _assure_grouper(self):
231233
2012-08-04 1""",
232234
)
233235
@Appender(_pipe_template)
234-
def pipe(self, func, *args, **kwargs):
236+
def pipe(
237+
self,
238+
func: Union[Callable[..., T], Tuple[Callable[..., T], str]],
239+
*args,
240+
**kwargs,
241+
) -> T:
235242
return super().pipe(func, *args, **kwargs)
236243

237244
_agg_see_also_doc = dedent(

0 commit comments

Comments
 (0)