Skip to content

CLN: Exception*2 in groupby wrapper #28771

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

Merged
merged 8 commits into from
Oct 8, 2019
Merged

Conversation

jbrockmendel
Copy link
Member

Gets rid of two except Exceptions in the same function, which is also one of the more heavily-nested functions in there.

except AttributeError:
# e.g. SparseArray has no flags attr
raise ValueError
import inspect
Copy link
Contributor

Choose a reason for hiding this comment

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

import at the top of the module

Copy link
Member Author

Choose a reason for hiding this comment

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

its an expensive import im trying to get rid of in the one other place we use it

Copy link
Contributor

Choose a reason for hiding this comment

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

really? interesting

Copy link
Member

Choose a reason for hiding this comment

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

What do we consider an "expensive" import? It seems natural to me to import the stdlib at the top of a module so would prefer to do that as well unless there really is a lot to be gained here

Copy link
Member Author

Choose a reason for hiding this comment

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

About 2.6 ms (of about 400 ms total). So its not massive, but at this point it is among the lower-hanging fruit. Fine to move I guess.

except AttributeError:
# e.g. SparseArray has no flags attr
raise ValueError
import inspect
Copy link
Member

Choose a reason for hiding this comment

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

What do we consider an "expensive" import? It seems natural to me to import the stdlib at the top of a module so would prefer to do that as well unless there really is a lot to be gained here

except AttributeError:
# e.g. SparseArray has no flags attr
raise ValueError
sig = inspect.signature(f)
Copy link
Member

Choose a reason for hiding this comment

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

Could this be done before the currying? IIUC correctly we are mutating the kwargs after the function has been defined but before it gets executed. I think would be more intuitive if we set up the appropriate kwargs before the currying even happens

Copy link
Member Author

Choose a reason for hiding this comment

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

i wasnt sure what the semantics are of editing kwargs. is it necessarily a new dict, or could we be editing a dict that exists in the calling namespace?

Copy link
Member

Choose a reason for hiding this comment

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

I don't know definitely but I think the same rules would apply to kwargs as to default arguments, i.e. the parameter values get evaluated on function execution and not definition

https://docs.python.org/3/reference/compound_stmts.html#function-definitions

Copy link
Member Author

Choose a reason for hiding this comment

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

If there wasn't the if name in base.plotting_methods case just above this I would think the answer is "yes" we can do this earlier, but I'm not confident as-is. Good candidate for follow-up to see if we can get that case out of this wrapper.

# in tests.groupby.test_function.test_non_cython_api
raise ValueError

wrapper.__name__ = name
Copy link
Member

Choose a reason for hiding this comment

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

Any reason we don't just use functools.wraps? Maybe a general question in our code base but unclear to me why we choose to assign to the dunders directly instead of using functools

Copy link
Member Author

Choose a reason for hiding this comment

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

no idea

@jreback jreback added the Clean label Oct 8, 2019
@jreback jreback added this to the 1.0 milestone Oct 8, 2019
@jreback
Copy link
Contributor

jreback commented Oct 8, 2019

not a big deal but can you merge master after @WillAyd refactor.

Copy link
Member

@WillAyd WillAyd left a comment

Choose a reason for hiding this comment

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

lgtm - these Exception cleanups are great thanks @jbrockmendel

@jbrockmendel
Copy link
Member Author

rebased+green

@WillAyd WillAyd merged commit 46d88c1 into pandas-dev:master Oct 8, 2019
@WillAyd
Copy link
Member

WillAyd commented Oct 8, 2019

Thanks @jbrockmendel

@jbrockmendel jbrockmendel deleted the faster34 branch October 8, 2019 19:00
proost pushed a commit to proost/pandas that referenced this pull request Dec 19, 2019
proost pushed a commit to proost/pandas that referenced this pull request Dec 19, 2019
bongolegend pushed a commit to bongolegend/pandas that referenced this pull request Jan 1, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants