Skip to content

REF: move only-used-once mixins to resample #41058

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 1 commit into from
Apr 20, 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
75 changes: 0 additions & 75 deletions pandas/core/groupby/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,83 +7,8 @@

import collections

from pandas._typing import final

from pandas.core.dtypes.common import (
is_list_like,
is_scalar,
)

from pandas.core.base import PandasObject

OutputKey = collections.namedtuple("OutputKey", ["label", "position"])


class ShallowMixin(PandasObject):
_attributes: list[str] = []

@final
def _shallow_copy(self, obj, **kwargs):
"""
return a new object with the replacement attributes
"""
if isinstance(obj, self._constructor):
obj = obj.obj
for attr in self._attributes:
if attr not in kwargs:
kwargs[attr] = getattr(self, attr)
return self._constructor(obj, **kwargs)


class GotItemMixin(PandasObject):
"""
Provide the groupby facilities to the mixed object.
"""

_attributes: list[str]

@final
def _gotitem(self, key, ndim, subset=None):
"""
Sub-classes to define. Return a sliced object.

Parameters
----------
key : string / list of selections
ndim : {1, 2}
requested ndim of result
subset : object, default None
subset to act on
"""
# create a new object to prevent aliasing
if subset is None:
# error: "GotItemMixin" has no attribute "obj"
subset = self.obj # type: ignore[attr-defined]

# we need to make a shallow copy of ourselves
# with the same groupby
kwargs = {attr: getattr(self, attr) for attr in self._attributes}

# Try to select from a DataFrame, falling back to a Series
try:
# error: "GotItemMixin" has no attribute "_groupby"
groupby = self._groupby[key] # type: ignore[attr-defined]
except IndexError:
# error: "GotItemMixin" has no attribute "_groupby"
groupby = self._groupby # type: ignore[attr-defined]

# error: Too many arguments for "GotItemMixin"
# error: Unexpected keyword argument "groupby" for "GotItemMixin"
# error: Unexpected keyword argument "parent" for "GotItemMixin"
self = type(self)(
subset, groupby=groupby, parent=self, **kwargs # type: ignore[call-arg]
)
self._reset_cache()
if subset.ndim == 2 and (is_scalar(key) and key in subset or is_list_like(key)):
self._selection = key
return self


# special case to prevent duplicate plots when catching exceptions when
# forwarding methods from NDFrames
plotting_methods = frozenset(["plot", "hist"])
Expand Down
64 changes: 57 additions & 7 deletions pandas/core/resample.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
T,
TimedeltaConvertibleTypes,
TimestampConvertibleTypes,
final,
)
from pandas.compat.numpy import function as nv
from pandas.errors import AbstractMethodError
Expand All @@ -39,16 +40,15 @@

import pandas.core.algorithms as algos
from pandas.core.apply import ResamplerWindowApply
from pandas.core.base import DataError
from pandas.core.base import (
DataError,
PandasObject,
)
import pandas.core.common as com
from pandas.core.generic import (
NDFrame,
_shared_docs,
)
from pandas.core.groupby.base import (
GotItemMixin,
ShallowMixin,
)
from pandas.core.groupby.generic import SeriesGroupBy
from pandas.core.groupby.groupby import (
BaseGroupBy,
Expand Down Expand Up @@ -86,7 +86,7 @@
_shared_docs_kwargs: dict[str, str] = {}


class Resampler(BaseGroupBy, ShallowMixin):
class Resampler(BaseGroupBy, PandasObject):
"""
Class for resampling datetimelike data, a groupby-like operation.
See aggregate, transform, and apply functions on this object.
Expand Down Expand Up @@ -141,6 +141,18 @@ def __init__(self, obj, groupby=None, axis=0, kind=None, **kwargs):
if self.groupby is not None:
self.groupby._set_grouper(self._convert_obj(obj), sort=True)

@final
def _shallow_copy(self, obj, **kwargs):
"""
return a new object with the replacement attributes
"""
if isinstance(obj, self._constructor):
obj = obj.obj
for attr in self._attributes:
if attr not in kwargs:
kwargs[attr] = getattr(self, attr)
return self._constructor(obj, **kwargs)

def __str__(self) -> str:
"""
Provide a nice str repr of our rolling object.
Expand Down Expand Up @@ -1018,11 +1030,13 @@ def h(self, _method=method):
setattr(Resampler, method, h)


class _GroupByMixin(GotItemMixin):
class _GroupByMixin(PandasObject):
"""
Provide the groupby facilities.
"""

_attributes: list[str]

def __init__(self, obj, *args, **kwargs):

parent = kwargs.pop("parent", None)
Expand Down Expand Up @@ -1064,6 +1078,42 @@ def func(x):
_downsample = _apply
_groupby_and_aggregate = _apply

@final
def _gotitem(self, key, ndim, subset=None):
"""
Sub-classes to define. Return a sliced object.

Parameters
----------
key : string / list of selections
ndim : {1, 2}
requested ndim of result
subset : object, default None
subset to act on
"""
# create a new object to prevent aliasing
if subset is None:
# error: "GotItemMixin" has no attribute "obj"
subset = self.obj # type: ignore[attr-defined]

# we need to make a shallow copy of ourselves
# with the same groupby
kwargs = {attr: getattr(self, attr) for attr in self._attributes}

# Try to select from a DataFrame, falling back to a Series
try:
groupby = self._groupby[key]
except IndexError:
groupby = self._groupby

self = type(self)(subset, groupby=groupby, parent=self, **kwargs)
self._reset_cache()
if subset.ndim == 2 and (
lib.is_scalar(key) and key in subset or lib.is_list_like(key)
):
self._selection = key
return self


class DatetimeIndexResampler(Resampler):
@property
Expand Down