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

CLN: Simplify gathering of results in aggregate #37227

Merged
merged 3 commits into from
Oct 22, 2020

Conversation

rhshadrach
Copy link
Member

@rhshadrach rhshadrach commented Oct 18, 2020

  • closes #xxxx
  • tests added / passed
  • passes black pandas
  • passes git diff upstream/master -u -- "*.py" | flake8 --diff
  • whatsnew entry

Reduces the number of paths when collecting results in aggregation.aggregate; one where we are gathering NDFrames using concat and the other where we are gathering scalars using Series.

The reason for the one test change is as follows. In one case, we previously gathered results using DataFrame. When this occurs and the indexes are not all equal, DataFrame will sort the index whereas concat will have the index in order of appearance. For example with

df = DataFrame(
    {
        'A': pd.Series([1, 2], index=['b', 'a']),
        'B': pd.Series([3, 4], index=['c', 'a'])
    }
)

gives

     A    B
a  2.0  4.0
b  1.0  NaN
c  NaN  3.0

whereas using concat instead of DataFrame on the first line with axis=1 gives:

     A    B
b  1.0  NaN
a  2.0  4.0
c  NaN  3.0

If in this example you replace the 2nd index with ['b', 'a'] (so that they are equal), then both Dataframe and concat will produce the same result with index ['b', 'a']. If on the other hand you replace the 2nd index with ['a', 'b'], then DataFrame will result in index ['a', 'b'] whereas concat will result in index ['b', 'a'].

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.

wow a lot simpler. pls merge master and ping on green.


# we have a dict of Series
# return a MI Series
if any(isinstance(r, NDFrame) for r in results.values()):
Copy link
Contributor

Choose a reason for hiding this comment

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

can you add a ABCNDFrame in pandas.core.dtypes.generic

keys_to_use = keys_to_use if keys_to_use != [] else keys
axis = 0 if isinstance(obj, ABCSeries) else 1
result = concat({k: results[k] for k in keys_to_use}, axis=axis)
# Raised if some value of results is not a NDFrame
Copy link
Contributor

Choose a reason for hiding this comment

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

can you move this comment below the AttributeError (as confusing where it is now)

try:
result = concat(results)
except TypeError as err:
keys_to_use = [k for k in keys if not results[k].empty]
Copy link
Contributor

Choose a reason for hiding this comment

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

where does the AttributeError come from?

can you move things outside the try/except (or use an else)?

@jreback jreback added Refactor Internal refactoring of code Reshaping Concat, Merge/Join, Stack/Unstack, Explode labels Oct 20, 2020
@jreback jreback added this to the 1.2 milestone Oct 22, 2020
@jreback
Copy link
Contributor

jreback commented Oct 22, 2020

lgtm merge on green

@jreback jreback merged commit affc4d5 into pandas-dev:master Oct 22, 2020
@jreback
Copy link
Contributor

jreback commented Oct 22, 2020

thanks @rhshadrach

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Refactor Internal refactoring of code Reshaping Concat, Merge/Join, Stack/Unstack, Explode
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants