-
-
Notifications
You must be signed in to change notification settings - Fork 18.2k
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
REF: move Block construction in groupby aggregation to internals #39997
Merged
jreback
merged 3 commits into
pandas-dev:master
from
jorisvandenbossche:refactor-groupby-internals
Feb 24, 2021
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,7 +15,6 @@ | |
from functools import partial | ||
from textwrap import dedent | ||
from typing import ( | ||
TYPE_CHECKING, | ||
Any, | ||
Callable, | ||
Dict, | ||
|
@@ -25,7 +24,6 @@ | |
List, | ||
Mapping, | ||
Optional, | ||
Sequence, | ||
Type, | ||
TypeVar, | ||
Union, | ||
|
@@ -115,10 +113,6 @@ | |
|
||
from pandas.plotting import boxplot_frame_groupby | ||
|
||
if TYPE_CHECKING: | ||
from pandas.core.internals import Block | ||
|
||
|
||
NamedAgg = namedtuple("NamedAgg", ["column", "aggfunc"]) | ||
# TODO(typing) the return value on this callable should be any *scalar*. | ||
AggScalar = Union[str, Callable[..., Any]] | ||
|
@@ -1074,7 +1068,7 @@ def _cython_agg_general( | |
agg_mgr = self._cython_agg_blocks( | ||
how, alt=alt, numeric_only=numeric_only, min_count=min_count | ||
) | ||
return self._wrap_agged_blocks(agg_mgr.blocks, items=agg_mgr.items) | ||
return self._wrap_agged_manager(agg_mgr) | ||
|
||
def _cython_agg_blocks( | ||
self, how: str, alt=None, numeric_only: bool = True, min_count: int = -1 | ||
|
@@ -1174,7 +1168,7 @@ def blk_func(bvalues: ArrayLike) -> ArrayLike: | |
# TypeError -> we may have an exception in trying to aggregate | ||
# continue and exclude the block | ||
# NotImplementedError -> "ohlc" with wrong dtype | ||
new_mgr = data.apply(blk_func, ignore_failures=True) | ||
new_mgr = data.grouped_reduce(blk_func, ignore_failures=True) | ||
|
||
if not len(new_mgr): | ||
raise DataError("No numeric types to aggregate") | ||
|
@@ -1748,17 +1742,17 @@ def _wrap_transformed_output( | |
|
||
return result | ||
|
||
def _wrap_agged_blocks(self, blocks: Sequence[Block], items: Index) -> DataFrame: | ||
def _wrap_agged_manager(self, mgr: BlockManager) -> DataFrame: | ||
if not self.as_index: | ||
index = np.arange(blocks[0].values.shape[-1]) | ||
mgr = BlockManager(blocks, axes=[items, index]) | ||
index = np.arange(mgr.shape[1]) | ||
mgr.axes[1] = ibase.Index(index) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is super janky, maybe can push this down somehow |
||
result = self.obj._constructor(mgr) | ||
|
||
self._insert_inaxis_grouper_inplace(result) | ||
result = result._consolidate() | ||
else: | ||
index = self.grouper.result_index | ||
mgr = BlockManager(blocks, axes=[items, index]) | ||
mgr.axes[1] = index | ||
result = self.obj._constructor(mgr) | ||
|
||
if self.axis == 1: | ||
|
@@ -1808,13 +1802,13 @@ def hfunc(bvalues: ArrayLike) -> ArrayLike: | |
counted = lib.count_level_2d(masked, labels=ids, max_bin=ngroups, axis=1) | ||
return counted | ||
|
||
new_mgr = data.apply(hfunc) | ||
new_mgr = data.grouped_reduce(hfunc) | ||
|
||
# If we are grouping on categoricals we want unobserved categories to | ||
# return zero, rather than the default of NaN which the reindexing in | ||
# _wrap_agged_blocks() returns. GH 35028 | ||
# _wrap_agged_manager() returns. GH 35028 | ||
with com.temp_setattr(self, "observed", True): | ||
result = self._wrap_agged_blocks(new_mgr.blocks, items=data.items) | ||
result = self._wrap_agged_manager(new_mgr) | ||
|
||
return self._reindex_output(result, fill_value=0) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
agreed this is an improvement