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

PERF: fix some of .clip() performance regression by using numpy arrays where possible #24735

Merged
merged 1 commit into from
Jan 20, 2019

Conversation

qwhelan
Copy link
Contributor

@qwhelan qwhelan commented Jan 11, 2019

A recent change to respect dtypes in .clip() (#24458) introduced a decent overhead of ~2ms to the call:

$ asv compare v0.23.4 v0.24.0rc1 --only-changed
       before           after         ratio
     [04095216]       [fdc4db25]
     <v0.23.4^0>       <v0.24.0rc1^0>
+         120±1μs      2.04±0.04ms    17.01  series_methods.Clip.time_clip(1000)
+        936±40μs      3.09±0.02ms     3.31  series_methods.Clip.time_clip(100000)
+         111±3μs      2.05±0.08ms    18.55  series_methods.Clip.time_clip(50)

This PR cuts the overhead from ~2ms to ~0.6ms by keeping subset as a numpy array; it's entirely boolean regardless of underlying dtype, so a DataFrame only adds overhead here:

$ asv compare v0.24.0rc1 HEAD --only-changed -s
       before           after         ratio
     [fdc4db25]       [63c47c58]
     <v0.24.0rc1^0>       <clip>
-     2.04±0.04ms         759±20μs     0.37  series_methods.Clip.time_clip(1000)
-     3.09±0.02ms      1.46±0.04ms     0.47  series_methods.Clip.time_clip(100000)
-     2.05±0.08ms         724±20μs     0.35  series_methods.Clip.time_clip(50)
  • closes #xxxx
  • tests added / passed
  • passes git diff upstream/master -u -- "*.py" | flake8 --diff
  • whatsnew entry

@codecov
Copy link

codecov bot commented Jan 11, 2019

Codecov Report

Merging #24735 into master will increase coverage by <.01%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master   #24735      +/-   ##
==========================================
+ Coverage   92.39%   92.39%   +<.01%     
==========================================
  Files         166      166              
  Lines       52358    52362       +4     
==========================================
+ Hits        48374    48378       +4     
  Misses       3984     3984
Flag Coverage Δ
#multiple 90.81% <100%> (ø) ⬆️
#single 43.06% <0%> (-0.03%) ⬇️
Impacted Files Coverage Δ
pandas/core/generic.py 96.63% <100%> (ø) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update fdc4db2...63c47c5. Read the comment docs.

@codecov
Copy link

codecov bot commented Jan 11, 2019

Codecov Report

Merging #24735 into master will increase coverage by <.01%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master   #24735      +/-   ##
==========================================
+ Coverage   92.39%   92.39%   +<.01%     
==========================================
  Files         166      166              
  Lines       52378    52382       +4     
==========================================
+ Hits        48393    48398       +5     
+ Misses       3985     3984       -1
Flag Coverage Δ
#multiple 90.81% <100%> (ø) ⬆️
#single 42.9% <0%> (-0.01%) ⬇️
Impacted Files Coverage Δ
pandas/core/generic.py 96.63% <100%> (ø) ⬆️
pandas/util/testing.py 88.13% <0%> (+0.09%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update f4458c1...f372c9e. Read the comment docs.

@jschendel jschendel added Performance Memory or execution speed performance Dtype Conversions Unexpected or buggy dtype conversions labels Jan 12, 2019

with np.errstate(all='ignore'):
if upper is not None:
subset = self.values <= upper
Copy link
Member

Choose a reason for hiding this comment

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

Curious, does this place nice when self is the new Int64 dtype?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It appears to - let me know if there's a more extensive test you have in mind:

In [2]: s = pd.Series(range(5)).astype('Int64')
In [3]: s.clip(1, 3)
Out[3]: 0    1
        1    1
        2    2
        3    3
        4    3
        dtype: Int64

result = result.where(subset, lower, axis=None, inplace=False)
mask = isna(self.values)

with np.errstate(all='ignore'):
Copy link
Member

Choose a reason for hiding this comment

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

What's the point of this?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is simply reverting back to what this block used to do; it's needed in the event values <= upper would otherwise raise a type error.


with np.errstate(all='ignore'):
if upper is not None:
subset = self.values <= upper
Copy link
Member

Choose a reason for hiding this comment

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

Idiomatic approach here would now be to_numpy instead of .values

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated

@jreback
Copy link
Contributor

jreback commented Jan 16, 2019

@qwhelan can you update

@jreback
Copy link
Contributor

jreback commented Jan 19, 2019

@qwhelan can you merge master and ping when passing

@jreback jreback added this to the 0.24.0 milestone Jan 20, 2019
@jreback jreback merged commit 2d47349 into pandas-dev:master Jan 20, 2019
@jreback
Copy link
Contributor

jreback commented Jan 20, 2019

thanks @qwhelan

Pingviinituutti pushed a commit to Pingviinituutti/pandas that referenced this pull request Feb 28, 2019
Pingviinituutti pushed a commit to Pingviinituutti/pandas that referenced this pull request Feb 28, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Dtype Conversions Unexpected or buggy dtype conversions Performance Memory or execution speed performance
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants