Skip to content

REF: call ensure_wrapped_if_datetimelike before the array arithmetic_op #39820

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

Conversation

jorisvandenbossche
Copy link
Member

@jorisvandenbossche jorisvandenbossche commented Feb 15, 2021

This is on top of #39772, so only the last commit needs to be looked at for now: bd50388

The actual goal here is to have a version of array_ops.py::arithmetic_op that does not call ensure_wrapped_if_datetimelike on its arrays. Reason: when performing ops column-wise, this becomes unnecessary overhead, and so the goal is to ensure this on a higher level, before calling the array_op.

So the experiment in this PR is to actually remove ensure_wrapped_if_datetimelike from arithmetic_op, and then add it in the places that will call arithmetic_op, to see how much changes this needs. This would ensure that we can keep a single shared arithmetic_op for use by both ArrayManager and BlockManager. The alternative would be to make a variant of arithmetic_op without the ensure_wrapped_if_datetimelike. That would require fewer code changes, but would give some duplication around arithmetic_op content (but it's not that it is a long function). Thoughts on which approach to prefer?

cc @jbrockmendel

@jorisvandenbossche jorisvandenbossche added Internals Related to non-user accessible pandas implementation Numeric Operations Arithmetic, Comparison, and Logical operations Refactor Internal refactoring of code labels Feb 15, 2021
@jbrockmendel
Copy link
Member

Any idea how big the perf impact is (both AM and non-AM)? needs to be worth the added complexity

Could trim down some of this by making DatetimeBlock/TimedeltaBlock backed directly by DTA/TDA

@jorisvandenbossche
Copy link
Member Author

I am currently profiling df + df for an all numeric dataframe of shape (1000, 1000) for ArrayManager. And for this case the ensure_wrapped_if_datetimelike takes around 1-2 %. So not much at all (but with a relatively wider dataframe, this percentage will increase. And as other parts get optimized, this will also increase).

So it's certainly not worth it on its own. But I think if we want to get the columnar frame ops closer to the blockwise ops (in performance), it will be thanks to multiple few-percentage improvements.
Short term, I am planning to do a few other possible changes (moving _maybe_upcast_for_op out of arithmetic_op, moving the np.seterr out of arithmetic_op, etc). Hopefully that will inform us a bit more about the best way forward.

The main complication here is that I needed to basically re-implement BlockManager.apply to be able to call ensure_wrapped_if_datetimelike there (so indeed as you mention the cause for that is DatetimeBlock/TimedeltaBlock not storing EAs directly, but still using the EA in operations -> any idea how far off we are from being able to store the actual EA?)

To also repeat from above: an alternative is to have a variant of arithmetic_op that does less checks and is only used in the AraryManager for now (arithmetic_op is not a long function, so IMO that would also be acceptable).

@jbrockmendel
Copy link
Member

any idea how far off we are from being able to store the actual EA?

ive got a branch just about ready. The sticking point ATM is arrow tests which I'll email you about shortly.

Short term, I am planning to do a few other possible changes (moving _maybe_upcast_for_op out of arithmetic_op, moving the np.seterr out of arithmetic_op, etc). Hopefully that will inform us a bit more about the best way forward.

sounds promising

@github-actions
Copy link
Contributor

This pull request is stale because it has been open for thirty days with no activity. Please update or respond to this comment if you're still interested in working on this.

@github-actions github-actions bot added the Stale label Mar 19, 2021
@jorisvandenbossche jorisvandenbossche force-pushed the ops-refactor-wrap-datetimelike branch from bd50388 to 89c375e Compare April 20, 2021 09:16
@jorisvandenbossche
Copy link
Member Author

Simplified this PR: it's no longer based on top of #39772 (since the datetimelike blocks now store their values as EAs, I don't need to override mgr.apply for "operate_scalar", as I did originally, and for which I needed the changes in #39772)

@jorisvandenbossche jorisvandenbossche marked this pull request as ready for review April 20, 2021 09:30
@jorisvandenbossche jorisvandenbossche added this to the 1.3 milestone Apr 22, 2021
@jorisvandenbossche
Copy link
Member Author

@jbrockmendel any further comments here?

Copy link
Member

@jbrockmendel jbrockmendel left a comment

Choose a reason for hiding this comment

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

LGTM

A lot has changed since the OP. Any updated estimates on perf impact?

@jorisvandenbossche
Copy link
Member Author

jorisvandenbossche commented Apr 22, 2021

A lot has changed since the OP. Any updated estimates on perf impact?

Eg in the FrameWithFrameWide benchmark (widest case, with operator.add), the ensure_wrapped_if_datetimelike calls take around 12% (without this PR).

@jreback
Copy link
Contributor

jreback commented Apr 22, 2021

thanks @jorisvandenbossche

@jorisvandenbossche jorisvandenbossche deleted the ops-refactor-wrap-datetimelike branch April 22, 2021 22:34
yeshsurya pushed a commit to yeshsurya/pandas that referenced this pull request May 6, 2021
JulianWgs pushed a commit to JulianWgs/pandas that referenced this pull request Jul 3, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Internals Related to non-user accessible pandas implementation Numeric Operations Arithmetic, Comparison, and Logical operations Refactor Internal refactoring of code
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants