Skip to content

PERF: DataFrame.clip / Series.clip #51472

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 14 commits into from
Feb 22, 2023
Merged

Conversation

lukemanley
Copy link
Member

@lukemanley lukemanley commented Feb 18, 2023

Perf improvement in DataFrame.clip and Series.clip:

asv continuous -f 1.1 upstream/main perf-dataframe-clip -b frame_methods.Clip

       before           after         ratio
     [2070bb8d]       [39c44771]
     <main>           <perf-dataframe-clip>
-      14.1±0.4ms       11.2±0.4ms     0.79  frame_methods.Clip.time_clip('float64')
-         147±7ms         98.6±5ms     0.67  frame_methods.Clip.time_clip('Float64')
-       133±0.7ms         78.4±1ms     0.59  frame_methods.Clip.time_clip('float64[pyarrow]')
asv continuous -f 1.1 upstream/main perf-dataframe-clip -b series_methods.Clip

       before           after         ratio
     [2070bb8d]       [39c44771]
     <main>           <perf-dataframe-clip>
-        613±30μs         533±30μs     0.87  series_methods.Clip.time_clip(1000)
-        635±60μs          509±8μs     0.80  series_methods.Clip.time_clip(50)

@lukemanley lukemanley added the Performance Memory or execution speed performance label Feb 18, 2023
Copy link
Member

@phofl phofl left a comment

Choose a reason for hiding this comment

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

The copy on write failures are legit. I guess you’ll have to do self.copy(deep=None) instead of self.copy()

@lukemanley
Copy link
Member Author

The copy on write failures are legit. I guess you’ll have to do self.copy(deep=None) instead of self.copy()

I just tried that, still seems to fail. I made a slightly different change that seems to work, let me know if this looks ok.

Copy link
Member

@phofl phofl left a comment

Choose a reason for hiding this comment

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

This moves away from CoW syntax now:

Both objects should share memory, if clip is a no-op:

df = DataFrame({"a": [1.5, 2, 3]})
df_copy = df.copy()
arr_a = get_array(df, "a")
view = df[:]
df.clip(lower=1, inplace=True)
print(np.shares_memory(get_array(df, "a"), arr_a))

Also with inplace and without references, this can still modify the existing array inplace.

Your current pr avoids this (we don't have tests for this yet, because we did not get to it and we covered where extensively :))

Additionally, this introduces bugs when setting other dtypes:

df = DataFrame({"a": [1, 2, 3]})
df = df.clip(lower=1.5)

This is a no-op on this branch but works on main (should probably add a test)

In general, I'd prefer doing this on the block level. This keeps CoW handling in more or less on place.

@lukemanley
Copy link
Member Author

Thanks for the explanation. I've updated the PR to use BlockManager.where and added a test for the int/float case you highlighted. I've updated the timings the OP.

@phofl
Copy link
Member

phofl commented Feb 19, 2023

Opened #51492 that covers possible cases. Would like to merge this first to ensure that we don't miss anything. Do you know why where is so much slower? Meaning the block method?

@phofl
Copy link
Member

phofl commented Feb 19, 2023

You could look into putmask for inplace ops, that avoids copying if no reference is found

@lukemanley
Copy link
Member Author

You could look into putmask for inplace ops, that avoids copying if no reference is found

Added inplace via putmask.

Do you know why where is so much slower? Meaning the block method?

Yes, I think I'll open a separate PR for this. When we compute a boolean mask on an EA-backed frame, we get a "boolean" (EA) mask. This gets converted to an object dtype ndarray within BaseBlockManager.apply which is quite slow.

As an example:

import pandas as pd
import numpy as np

df = pd.DataFrame(np.random.randn(10**6, 2), dtype="Float64")

mask = df > 0

%timeit mask._values                                 # -> dtype object (SLOW...)
%timeit mask.to_numpy(dtype=bool, na_value=False)    # -> dtype bool

# 25.2 ms ± 408 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)
# 539 µs ± 9.9 µs per loop (mean ± std. dev. of 7 runs, 1,000 loops each)

@phofl
Copy link
Member

phofl commented Feb 20, 2023

Merged the clip tests, could you rebase?

Ah that makes sense, got it.

Copy link
Member

@phofl phofl left a comment

Choose a reason for hiding this comment

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

small comments, otherwise lgtm

@lukemanley
Copy link
Member Author

small comments, otherwise lgtm

@phofl - sorry, which small comments are you referring to?

cond = mask | (self <= upper)
mgr = mgr.where(other=upper, cond=cond, align=False)
if lower is not None:
cond = mask | (self >= lower)
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 use > or >= in both cases? Also why don’t we need a mask in the inplace case?

Copy link
Member Author

@lukemanley lukemanley Feb 22, 2023

Choose a reason for hiding this comment

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

The condition in putmask identifies the values to be updated so we look for values outside the threshold (e.g. ">")

The condition in where identifies the values to be left as is which are NA values (mask) and anything within the threshold (e.g. "<=")

So I think the difference is consistent with the meaning of those different methods. Using <= and >= in putmask would actually turn no-ops at the boundary into unnecessary ops I think.

Copy link
Member

Choose a reason for hiding this comment

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

Thx, makes sense. Did not consider that, could you add a really short comment to that effect?

@phofl
Copy link
Member

phofl commented Feb 21, 2023

Sorry looks like the did not get posted. Had some connection troubles today, reposted them

@@ -1122,6 +1122,7 @@ Performance improvements
- Performance improvement in :func:`merge` and :meth:`DataFrame.join` when joining on a sorted :class:`MultiIndex` (:issue:`48504`)
- Performance improvement in :func:`to_datetime` when parsing strings with timezone offsets (:issue:`50107`)
- Performance improvement in :meth:`DataFrame.loc` and :meth:`Series.loc` for tuple-based indexing of a :class:`MultiIndex` (:issue:`48384`)
- Performance improvement in :meth:`DataFrame.clip` and :meth:`Series.clip` (:issue:`51472`)
Copy link
Member

Choose a reason for hiding this comment

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

One last comment. Can you move to 2.1?

@phofl phofl added this to the 2.1 milestone Feb 22, 2023
@phofl phofl merged commit 14a315c into pandas-dev:main Feb 22, 2023
@phofl
Copy link
Member

phofl commented Feb 22, 2023

thx @lukemanley

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Performance Memory or execution speed performance
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants