Skip to content
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

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

Merged
merged 6 commits into from
Jan 15, 2021
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -49,6 +49,7 @@
NpDtype,
Renamer,
StorageOptions,
T,
TimedeltaConvertibleTypes,
TimestampConvertibleTypes,
ValueKeyFunc,
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
17 changes: 12 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 Down Expand Up @@ -45,6 +46,7 @@ class providing the base-class of operations.
IndexLabel,
Label,
Scalar,
T,
final,
)
from pandas.compat.numpy import function as nv
Expand Down Expand Up @@ -476,7 +478,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 +726,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 +792,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 +3065,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
13 changes: 10 additions & 3 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 Callable, Dict, Optional, Tuple, Union, no_type_check

import numpy as np

Expand All @@ -14,7 +16,7 @@
Timestamp,
to_offset,
)
from pandas._typing import TimedeltaConvertibleTypes, TimestampConvertibleTypes
from pandas._typing import T, TimedeltaConvertibleTypes, TimestampConvertibleTypes
from pandas.compat.numpy import function as nv
from pandas.errors import AbstractMethodError
from pandas.util._decorators import Appender, Substitution, doc
Expand Down Expand Up @@ -231,7 +233,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