Skip to content
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

Conversation

jorisvandenbossche
Copy link
Member

Possible precursor for #39885

@jorisvandenbossche jorisvandenbossche added Refactor Internal refactoring of code Groupby Internals Related to non-user accessible pandas implementation labels Feb 23, 2021
@jorisvandenbossche jorisvandenbossche requested review from jreback and jbrockmendel and removed request for jreback February 23, 2021 15:47
@jorisvandenbossche
Copy link
Member Author

For the current BlockManager code, this doesn't actually simplify that much in the groupby code (it does remove accessing the blocks and an explicit BlockManager(..) call, so removing BlockManager internals).

In principle I could re-use BlockManager.apply inside BlockManager.grouped_reduce, but I don't find it very clean to use apply for something for something that doesn't generate a same-shape BlockManager (it basically now creates an invalid BlockManager object because the length of the index is not correct).

But it will probably make it a bit cleaner to add ArrayManager support.

if len(result_blocks) == 0:
raise DataError("No numeric types to aggregate")

index = Index(range(result_blocks[0].values.shape[-1]))
Copy link
Member

Choose a reason for hiding this comment

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

can we figure out this length before calling grouped_reduce? if so, i think that we can turn it into an argument, then refactor reduce, apply and possibly quantile to dispatch to this

Copy link
Member Author

Choose a reason for hiding this comment

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

The length can certainly be known before hand for the groupby case (self.grouper.ngroups), I am only not fully sure this is a nice API for the Manager

@@ -1748,17 +1735,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:
Copy link
Member

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

@jreback jreback added this to the 1.3 milestone Feb 24, 2021
@jreback
Copy link
Contributor

jreback commented Feb 24, 2021

lgtm. some failing tests :->

Copy link
Contributor

@jreback jreback left a comment

Choose a reason for hiding this comment

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

lgtm. comment for the future

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)
Copy link
Contributor

Choose a reason for hiding this comment

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

this is super janky, maybe can push this down somehow

@jreback jreback merged commit 408216c into pandas-dev:master Feb 24, 2021
@jorisvandenbossche jorisvandenbossche deleted the refactor-groupby-internals branch February 24, 2021 18:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Groupby Internals Related to non-user accessible pandas implementation Refactor Internal refactoring of code
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants