Skip to content

CLN: simplify _shallow_copy #29474

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 2 commits into from
Nov 8, 2019
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
9 changes: 4 additions & 5 deletions pandas/core/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -634,20 +634,19 @@ def _is_builtin_func(self, arg):
class ShallowMixin:
_attributes = [] # type: List[str]

def _shallow_copy(self, obj=None, obj_type=None, **kwargs):
def _shallow_copy(self, obj=None, **kwargs):
"""
return a new object with the replacement attributes
"""
if obj is None:
obj = self._selected_obj.copy()
if obj_type is None:
obj_type = self._constructor
if isinstance(obj, obj_type):

if isinstance(obj, self._constructor):
obj = obj.obj
for attr in self._attributes:
if attr not in kwargs:
kwargs[attr] = getattr(self, attr)
return obj_type(obj, **kwargs)
return self._constructor(obj, **kwargs)


class IndexOpsMixin:
Expand Down
16 changes: 0 additions & 16 deletions pandas/core/groupby/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,6 @@ class GroupByMixin:
Provide the groupby facilities to the mixed object.
"""

@staticmethod
def _dispatch(name, *args, **kwargs):
"""
Dispatch to apply.
"""

def outer(self, *args, **kwargs):
def f(x):
x = self._shallow_copy(x, groupby=self._groupby)
return getattr(x, name)(*args, **kwargs)

return self._groupby.apply(f)

outer.__name__ = name
return outer

def _gotitem(self, key, ndim, subset=None):
"""
Sub-classes to define. Return a sliced object.
Expand Down
25 changes: 21 additions & 4 deletions pandas/core/window/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,23 @@
"""


class _GroupByMixin(GroupByMixin):
def _dispatch(name: str, *args, **kwargs):
Copy link
Contributor

Choose a reason for hiding this comment

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

eventually add a full doc-string if we end up using this

"""
Dispatch to apply.
"""

def outer(self, *args, **kwargs):
def f(x):
x = self._shallow_copy(x, groupby=self._groupby)
return getattr(x, name)(*args, **kwargs)

return self._groupby.apply(f)

outer.__name__ = name
return outer


class WindowGroupByMixin(GroupByMixin):
"""
Provide the groupby facilities.
"""
Expand All @@ -41,9 +57,9 @@ def __init__(self, obj, *args, **kwargs):
self._groupby.grouper.mutated = True
super().__init__(obj, *args, **kwargs)

count = GroupByMixin._dispatch("count")
corr = GroupByMixin._dispatch("corr", other=None, pairwise=None)
cov = GroupByMixin._dispatch("cov", other=None, pairwise=None)
count = _dispatch("count")
corr = _dispatch("corr", other=None, pairwise=None)
cov = _dispatch("cov", other=None, pairwise=None)

def _apply(
self, func, name=None, window=None, center=None, check_minp=None, **kwargs
Expand All @@ -53,6 +69,7 @@ def _apply(
performing the original function call on the grouped object.
"""

# TODO: can we de-duplicate with _dispatch?
def f(x, name=name, *args):
x = self._shallow_copy(x)

Expand Down
4 changes: 2 additions & 2 deletions pandas/core/window/expanding.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from pandas.compat.numpy import function as nv
from pandas.util._decorators import Appender, Substitution

from pandas.core.window.common import _doc_template, _GroupByMixin, _shared_docs
from pandas.core.window.common import WindowGroupByMixin, _doc_template, _shared_docs
from pandas.core.window.rolling import _Rolling_and_Expanding


Expand Down Expand Up @@ -250,7 +250,7 @@ def corr(self, other=None, pairwise=None, **kwargs):
return super().corr(other=other, pairwise=pairwise, **kwargs)


class ExpandingGroupby(_GroupByMixin, Expanding):
class ExpandingGroupby(WindowGroupByMixin, Expanding):
"""
Provide a expanding groupby implementation.
"""
Expand Down
4 changes: 2 additions & 2 deletions pandas/core/window/rolling.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@
import pandas.core.common as com
from pandas.core.index import Index, ensure_index
from pandas.core.window.common import (
WindowGroupByMixin,
_doc_template,
_flex_binary_moment,
_GroupByMixin,
_offset,
_require_min_periods,
_shared_docs,
Expand Down Expand Up @@ -1917,7 +1917,7 @@ def corr(self, other=None, pairwise=None, **kwargs):
Rolling.__doc__ = Window.__doc__


class RollingGroupby(_GroupByMixin, Rolling):
class RollingGroupby(WindowGroupByMixin, Rolling):
"""
Provide a rolling groupby implementation.
"""
Expand Down