Skip to content

Commit 0134976

Browse files
committed
TYP: type up NDFrame.pipe GroupBy.pipe etc.
1 parent 2a60c56 commit 0134976

File tree

4 files changed

+51
-10
lines changed

4 files changed

+51
-10
lines changed

pandas/core/common.py

+18-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, Union, cast
11+
from typing import (
12+
TYPE_CHECKING,
13+
Any,
14+
Callable,
15+
Collection,
16+
Iterable,
17+
Iterator,
18+
List,
19+
Tuple,
20+
Union,
21+
cast,
22+
)
1223
import warnings
1324

1425
import numpy as np
@@ -28,6 +39,9 @@
2839
from pandas.core.dtypes.inference import iterable_not_string
2940
from pandas.core.dtypes.missing import isna, isnull, notnull # noqa
3041

42+
if TYPE_CHECKING:
43+
from pandas._typing import T
44+
3145

3246
class SettingWithCopyError(ValueError):
3347
pass
@@ -405,7 +419,9 @@ def random_state(state=None):
405419
)
406420

407421

408-
def pipe(obj, func, *args, **kwargs):
422+
def pipe(
423+
obj, func: Union[Callable[..., T], Tuple[Callable[..., T], str]], *args, **kwargs
424+
) -> T:
409425
"""
410426
Apply a function ``func`` to object ``obj`` either by passing obj as the
411427
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
@@ -120,6 +120,7 @@
120120

121121
if TYPE_CHECKING:
122122
from pandas._libs.tslibs import BaseOffset
123+
from pandas._typing import T
123124

124125
from pandas.core.frame import DataFrame
125126
from pandas.core.resample import Resampler
@@ -5351,7 +5352,12 @@ def sample(
53515352

53525353
@final
53535354
@doc(klass=_shared_doc_kwargs["klass"])
5354-
def pipe(self, func, *args, **kwargs):
5355+
def pipe(
5356+
self,
5357+
func: Union[Callable[..., T], Tuple[Callable[..., T], str]],
5358+
*args,
5359+
**kwargs,
5360+
) -> T:
53555361
r"""
53565362
Apply func(self, \*args, \*\*kwargs).
53575363

pandas/core/groupby/groupby.py

+16-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
@@ -14,6 +15,7 @@ class providing the base-class of operations.
1415
from textwrap import dedent
1516
import types
1617
from typing import (
18+
TYPE_CHECKING,
1719
Callable,
1820
Dict,
1921
FrozenSet,
@@ -78,6 +80,10 @@ class providing the base-class of operations.
7880
from pandas.core.sorting import get_group_index_sorter
7981
from pandas.core.util.numba_ import NUMBA_FUNC_CACHE
8082

83+
if TYPE_CHECKING:
84+
from pandas._typing import T
85+
86+
8187
_common_see_also = """
8288
See Also
8389
--------
@@ -476,7 +482,7 @@ def f(self):
476482

477483

478484
@contextmanager
479-
def group_selection_context(groupby: "BaseGroupBy") -> Iterator["BaseGroupBy"]:
485+
def group_selection_context(groupby: BaseGroupBy) -> Iterator[BaseGroupBy]:
480486
"""
481487
Set / reset the group_selection_context.
482488
"""
@@ -724,8 +730,8 @@ def _set_group_selection(self) -> None:
724730

725731
@final
726732
def _set_result_index_ordered(
727-
self, result: "OutputFrameOrSeries"
728-
) -> "OutputFrameOrSeries":
733+
self, result: OutputFrameOrSeries
734+
) -> OutputFrameOrSeries:
729735
# set the result index on the passed values object and
730736
# return the new object, xref 8046
731737

@@ -790,7 +796,12 @@ def __getattr__(self, attr: str):
790796
),
791797
)
792798
@Appender(_pipe_template)
793-
def pipe(self, func, *args, **kwargs):
799+
def pipe(
800+
self,
801+
func: Union[Callable[..., T], Tuple[Callable[..., T], str]],
802+
*args,
803+
**kwargs,
804+
) -> T:
794805
return com.pipe(self, func, *args, **kwargs)
795806

796807
plot = property(GroupByPlot)
@@ -3058,7 +3069,7 @@ def get_groupby(
30583069
by: Optional[_KeysArgType] = None,
30593070
axis: int = 0,
30603071
level=None,
3061-
grouper: "Optional[ops.BaseGrouper]" = None,
3072+
grouper: Optional[ops.BaseGrouper] = None,
30623073
exclusions=None,
30633074
selection=None,
30643075
as_index: bool = True,

pandas/core/resample.py

+10-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import copy
22
from datetime import timedelta
33
from textwrap import dedent
4-
from typing import Dict, Optional, Union, no_type_check
4+
from typing import TYPE_CHECKING, Callable, Dict, Optional, Tuple, Union, no_type_check
55

66
import numpy as np
77

@@ -43,6 +43,9 @@
4343
from pandas.tseries.frequencies import is_subperiod, is_superperiod
4444
from pandas.tseries.offsets import DateOffset, Day, Nano, Tick
4545

46+
if TYPE_CHECKING:
47+
from pandas._typing import T
48+
4649
_shared_docs_kwargs: Dict[str, str] = {}
4750

4851

@@ -231,7 +234,12 @@ def _assure_grouper(self):
231234
2012-08-04 1""",
232235
)
233236
@Appender(_pipe_template)
234-
def pipe(self, func, *args, **kwargs):
237+
def pipe(
238+
self,
239+
func: Union[Callable[..., T], Tuple[Callable[..., T], str]],
240+
*args,
241+
**kwargs,
242+
) -> T:
235243
return super().pipe(func, *args, **kwargs)
236244

237245
_agg_see_also_doc = dedent(

0 commit comments

Comments
 (0)