Skip to content

Commit 5206163

Browse files
jbrockmendelproost
authored andcommitted
CLN: simplify _shallow_copy (pandas-dev#29474)
1 parent ba5320b commit 5206163

File tree

5 files changed

+29
-29
lines changed

5 files changed

+29
-29
lines changed

pandas/core/base.py

+4-5
Original file line numberDiff line numberDiff line change
@@ -633,20 +633,19 @@ def _is_builtin_func(self, arg):
633633
class ShallowMixin:
634634
_attributes = [] # type: List[str]
635635

636-
def _shallow_copy(self, obj=None, obj_type=None, **kwargs):
636+
def _shallow_copy(self, obj=None, **kwargs):
637637
"""
638638
return a new object with the replacement attributes
639639
"""
640640
if obj is None:
641641
obj = self._selected_obj.copy()
642-
if obj_type is None:
643-
obj_type = self._constructor
644-
if isinstance(obj, obj_type):
642+
643+
if isinstance(obj, self._constructor):
645644
obj = obj.obj
646645
for attr in self._attributes:
647646
if attr not in kwargs:
648647
kwargs[attr] = getattr(self, attr)
649-
return obj_type(obj, **kwargs)
648+
return self._constructor(obj, **kwargs)
650649

651650

652651
class IndexOpsMixin:

pandas/core/groupby/base.py

-16
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,6 @@ class GroupByMixin:
1111
Provide the groupby facilities to the mixed object.
1212
"""
1313

14-
@staticmethod
15-
def _dispatch(name, *args, **kwargs):
16-
"""
17-
Dispatch to apply.
18-
"""
19-
20-
def outer(self, *args, **kwargs):
21-
def f(x):
22-
x = self._shallow_copy(x, groupby=self._groupby)
23-
return getattr(x, name)(*args, **kwargs)
24-
25-
return self._groupby.apply(f)
26-
27-
outer.__name__ = name
28-
return outer
29-
3014
def _gotitem(self, key, ndim, subset=None):
3115
"""
3216
Sub-classes to define. Return a sliced object.

pandas/core/window/common.py

+21-4
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,23 @@
2626
"""
2727

2828

29-
class _GroupByMixin(GroupByMixin):
29+
def _dispatch(name: str, *args, **kwargs):
30+
"""
31+
Dispatch to apply.
32+
"""
33+
34+
def outer(self, *args, **kwargs):
35+
def f(x):
36+
x = self._shallow_copy(x, groupby=self._groupby)
37+
return getattr(x, name)(*args, **kwargs)
38+
39+
return self._groupby.apply(f)
40+
41+
outer.__name__ = name
42+
return outer
43+
44+
45+
class WindowGroupByMixin(GroupByMixin):
3046
"""
3147
Provide the groupby facilities.
3248
"""
@@ -41,9 +57,9 @@ def __init__(self, obj, *args, **kwargs):
4157
self._groupby.grouper.mutated = True
4258
super().__init__(obj, *args, **kwargs)
4359

44-
count = GroupByMixin._dispatch("count")
45-
corr = GroupByMixin._dispatch("corr", other=None, pairwise=None)
46-
cov = GroupByMixin._dispatch("cov", other=None, pairwise=None)
60+
count = _dispatch("count")
61+
corr = _dispatch("corr", other=None, pairwise=None)
62+
cov = _dispatch("cov", other=None, pairwise=None)
4763

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

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

pandas/core/window/expanding.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from pandas.compat.numpy import function as nv
44
from pandas.util._decorators import Appender, Substitution
55

6-
from pandas.core.window.common import _doc_template, _GroupByMixin, _shared_docs
6+
from pandas.core.window.common import WindowGroupByMixin, _doc_template, _shared_docs
77
from pandas.core.window.rolling import _Rolling_and_Expanding
88

99

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

252252

253-
class ExpandingGroupby(_GroupByMixin, Expanding):
253+
class ExpandingGroupby(WindowGroupByMixin, Expanding):
254254
"""
255255
Provide a expanding groupby implementation.
256256
"""

pandas/core/window/rolling.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@
3939
import pandas.core.common as com
4040
from pandas.core.index import Index, ensure_index
4141
from pandas.core.window.common import (
42+
WindowGroupByMixin,
4243
_doc_template,
4344
_flex_binary_moment,
44-
_GroupByMixin,
4545
_offset,
4646
_require_min_periods,
4747
_shared_docs,
@@ -1917,7 +1917,7 @@ def corr(self, other=None, pairwise=None, **kwargs):
19171917
Rolling.__doc__ = Window.__doc__
19181918

19191919

1920-
class RollingGroupby(_GroupByMixin, Rolling):
1920+
class RollingGroupby(WindowGroupByMixin, Rolling):
19211921
"""
19221922
Provide a rolling groupby implementation.
19231923
"""

0 commit comments

Comments
 (0)