Skip to content

Commit

Permalink
TYP: type up NDFrame.pipe GroupBy.pipe etc.
Browse files Browse the repository at this point in the history
  • Loading branch information
topper-123 committed Jan 10, 2021
1 parent 2a60c56 commit 0134976
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 10 deletions.
20 changes: 18 additions & 2 deletions pandas/core/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,18 @@
import contextlib
from functools import partial
import inspect
from typing import Any, Collection, Iterable, Iterator, List, Union, cast
from typing import (
TYPE_CHECKING,
Any,
Callable,
Collection,
Iterable,
Iterator,
List,
Tuple,
Union,
cast,
)
import warnings

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

if TYPE_CHECKING:
from pandas._typing import T


class SettingWithCopyError(ValueError):
pass
Expand Down Expand Up @@ -405,7 +419,9 @@ def random_state(state=None):
)


def pipe(obj, func, *args, **kwargs):
def pipe(
obj, func: Union[Callable[..., T], Tuple[Callable[..., T], str]], *args, **kwargs
) -> T:
"""
Apply a function ``func`` to object ``obj`` either by passing obj as the
first argument to the function or, in the case that the func is a tuple,
Expand Down
8 changes: 7 additions & 1 deletion pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@

if TYPE_CHECKING:
from pandas._libs.tslibs import BaseOffset
from pandas._typing import T

from pandas.core.frame import DataFrame
from pandas.core.resample import Resampler
Expand Down Expand Up @@ -5351,7 +5352,12 @@ def sample(

@final
@doc(klass=_shared_doc_kwargs["klass"])
def pipe(self, func, *args, **kwargs):
def pipe(
self,
func: Union[Callable[..., T], Tuple[Callable[..., T], str]],
*args,
**kwargs,
) -> T:
r"""
Apply func(self, \*args, \*\*kwargs).
Expand Down
21 changes: 16 additions & 5 deletions pandas/core/groupby/groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ class providing the base-class of operations.
(defined in pandas.core.groupby.generic)
expose these user-facing objects to provide specific functionality.
"""
from __future__ import annotations

from contextlib import contextmanager
import datetime
Expand All @@ -14,6 +15,7 @@ class providing the base-class of operations.
from textwrap import dedent
import types
from typing import (
TYPE_CHECKING,
Callable,
Dict,
FrozenSet,
Expand Down Expand Up @@ -78,6 +80,10 @@ class providing the base-class of operations.
from pandas.core.sorting import get_group_index_sorter
from pandas.core.util.numba_ import NUMBA_FUNC_CACHE

if TYPE_CHECKING:
from pandas._typing import T


_common_see_also = """
See Also
--------
Expand Down Expand Up @@ -476,7 +482,7 @@ def f(self):


@contextmanager
def group_selection_context(groupby: "BaseGroupBy") -> Iterator["BaseGroupBy"]:
def group_selection_context(groupby: BaseGroupBy) -> Iterator[BaseGroupBy]:
"""
Set / reset the group_selection_context.
"""
Expand Down Expand Up @@ -724,8 +730,8 @@ def _set_group_selection(self) -> None:

@final
def _set_result_index_ordered(
self, result: "OutputFrameOrSeries"
) -> "OutputFrameOrSeries":
self, result: OutputFrameOrSeries
) -> OutputFrameOrSeries:
# set the result index on the passed values object and
# return the new object, xref 8046

Expand Down Expand Up @@ -790,7 +796,12 @@ def __getattr__(self, attr: str):
),
)
@Appender(_pipe_template)
def pipe(self, func, *args, **kwargs):
def pipe(
self,
func: Union[Callable[..., T], Tuple[Callable[..., T], str]],
*args,
**kwargs,
) -> T:
return com.pipe(self, func, *args, **kwargs)

plot = property(GroupByPlot)
Expand Down Expand Up @@ -3058,7 +3069,7 @@ def get_groupby(
by: Optional[_KeysArgType] = None,
axis: int = 0,
level=None,
grouper: "Optional[ops.BaseGrouper]" = None,
grouper: Optional[ops.BaseGrouper] = None,
exclusions=None,
selection=None,
as_index: bool = True,
Expand Down
12 changes: 10 additions & 2 deletions pandas/core/resample.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import copy
from datetime import timedelta
from textwrap import dedent
from typing import Dict, Optional, Union, no_type_check
from typing import TYPE_CHECKING, Callable, Dict, Optional, Tuple, Union, no_type_check

import numpy as np

Expand Down Expand Up @@ -43,6 +43,9 @@
from pandas.tseries.frequencies import is_subperiod, is_superperiod
from pandas.tseries.offsets import DateOffset, Day, Nano, Tick

if TYPE_CHECKING:
from pandas._typing import T

_shared_docs_kwargs: Dict[str, str] = {}


Expand Down Expand Up @@ -231,7 +234,12 @@ def _assure_grouper(self):
2012-08-04 1""",
)
@Appender(_pipe_template)
def pipe(self, func, *args, **kwargs):
def pipe(
self,
func: Union[Callable[..., T], Tuple[Callable[..., T], str]],
*args,
**kwargs,
) -> T:
return super().pipe(func, *args, **kwargs)

_agg_see_also_doc = dedent(
Expand Down

0 comments on commit 0134976

Please sign in to comment.