Skip to content

Commit

Permalink
CLN: move __finalize__ call into splitter (groupbt pandas-devGH-45363
Browse files Browse the repository at this point in the history
…follow-up)
  • Loading branch information
jorisvandenbossche committed Jan 17, 2022
1 parent 4e034ec commit 37c406b
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 26 deletions.
16 changes: 0 additions & 16 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -285,22 +285,6 @@ def _init_mgr(
mgr = mgr.astype(dtype=dtype)
return mgr

@classmethod
def _from_mgr(cls, mgr: Manager):
"""
Fastpath to create a new DataFrame/Series from just a BlockManager/ArrayManager.
Notes
-----
Skips setting `_flags` attribute; caller is responsible for doing so.
"""
obj = cls.__new__(cls)
object.__setattr__(obj, "_is_copy", None)
object.__setattr__(obj, "_mgr", mgr)
object.__setattr__(obj, "_item_cache", {})
object.__setattr__(obj, "_attrs", {})
return obj

def _as_manager(self: NDFrameT, typ: str, copy: bool_t = True) -> NDFrameT:
"""
Private helper function to create a DataFrame with specific manager.
Expand Down
15 changes: 5 additions & 10 deletions pandas/core/groupby/ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -705,17 +705,14 @@ def get_iterator(
"""
splitter = self._get_splitter(data, axis=axis)
keys = self.group_keys_seq
for key, group in zip(keys, splitter):
yield key, group.__finalize__(data, method="groupby")
yield from zip(keys, splitter)

@final
def _get_splitter(self, data: NDFrame, axis: int = 0) -> DataSplitter:
"""
Returns
-------
Generator yielding subsetted objects
__finalize__ has not been called for the subsetted objects returned.
"""
ids, _, ngroups = self.group_info
return get_splitter(data, ids, ngroups, axis=axis)
Expand Down Expand Up @@ -753,7 +750,6 @@ def apply(
zipped = zip(group_keys, splitter)

for key, group in zipped:
group = group.__finalize__(data, method="groupby")
object.__setattr__(group, "name", key)

# group might be modified
Expand Down Expand Up @@ -1001,7 +997,6 @@ def _aggregate_series_pure_python(
splitter = get_splitter(obj, ids, ngroups, axis=0)

for i, group in enumerate(splitter):
group = group.__finalize__(obj, method="groupby")
res = func(group)
res = libreduction.extract_result(res)

Expand Down Expand Up @@ -1244,8 +1239,9 @@ class SeriesSplitter(DataSplitter):
def _chop(self, sdata: Series, slice_obj: slice) -> Series:
# fastpath equivalent to `sdata.iloc[slice_obj]`
mgr = sdata._mgr.get_slice(slice_obj)
# __finalize__ not called here, must be applied by caller if applicable
return sdata._constructor(mgr, name=sdata.name, fastpath=True)
return sdata._constructor(mgr, name=sdata.name, fastpath=True).__finalize__(
sdata, method="groupby"
)


class FrameSplitter(DataSplitter):
Expand All @@ -1256,8 +1252,7 @@ def _chop(self, sdata: DataFrame, slice_obj: slice) -> DataFrame:
# else:
# return sdata.iloc[:, slice_obj]
mgr = sdata._mgr.get_slice(slice_obj, axis=1 - self.axis)
# __finalize__ not called here, must be applied by caller if applicable
return sdata._constructor(mgr)
return sdata._constructor(mgr).__finalize__(sdata, method="groupby")


def get_splitter(
Expand Down

0 comments on commit 37c406b

Please sign in to comment.