Skip to content

TYP: NDFrame.pipe, GroupBy.pipe etc. #39093

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jan 15, 2021
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 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, Optional, Union, cast
from typing import (
Any,
Callable,
Collection,
Iterable,
Iterator,
List,
Optional,
Tuple,
Union,
cast,
)
import warnings

import numpy as np
Expand Down Expand Up @@ -405,7 +416,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 @@ -123,6 +123,7 @@

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

imports from _typing are already guarded.


from pandas.core.frame import DataFrame
from pandas.core.resample import Resampler
Expand Down Expand Up @@ -5356,7 +5357,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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same



_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
14 changes: 12 additions & 2 deletions pandas/core/resample.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from __future__ import annotations

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 +45,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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same


_shared_docs_kwargs: Dict[str, str] = {}


Expand Down Expand Up @@ -231,7 +236,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