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
18 changes: 18 additions & 0 deletions asv_bench/benchmarks/frame_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,24 @@
from .pandas_vb_common import tm


class Clip:
params = [
["float64", "Float64", "float64[pyarrow]"],
[True, False],
]
param_names = ["dtype", "hasna"]

def setup(self, dtype, hasna):
data = np.random.randn(100_000, 10)
df = DataFrame(data, dtype=dtype)
if hasna:
df.iloc[2, :] = None
self.df = df

def time_clip(self, dtype, hasna):
self.df.clip(-1.0, 1.0)


class GetNumericData:
def setup(self):
self.df = DataFrame(np.random.randn(10000, 25))
Expand Down
1 change: 1 addition & 0 deletions doc/source/whatsnew/v2.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1115,6 +1115,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?

- Performance improvement for :meth:`Series.replace` with categorical dtype (:issue:`49404`)
- Performance improvement for :meth:`MultiIndex.unique` (:issue:`48335`)
- Performance improvement for indexing operations with nullable and arrow dtypes (:issue:`49420`, :issue:`51316`)
Expand Down
18 changes: 7 additions & 11 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -7989,20 +7989,16 @@ def _clip_with_scalar(self, lower, upper, inplace: bool_t = False):
):
raise ValueError("Cannot use an NA value as a clip threshold")

result = self
mask = isna(self._values)

with np.errstate(all="ignore"):
def blk_func(arr, lower=lower, upper=upper):
if upper is not None:
subset = self <= upper
result = result.where(subset, upper, axis=None, inplace=False)
arr[arr > upper] = upper
if lower is not None:
subset = self >= lower
result = result.where(subset, lower, axis=None, inplace=False)

if np.any(mask):
result[mask] = np.nan
arr[arr < lower] = lower
return arr

result = self if inplace else self.copy()
result_mgr = result._mgr.apply(blk_func)
result = self._constructor(result_mgr).__finalize__(self)
if inplace:
return self._update_inplace(result)
else:
Expand Down
6 changes: 3 additions & 3 deletions pandas/tests/copy_view/test_clip.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ def test_clip_inplace_reference(using_copy_on_write):
view = df[:]
df.clip(lower=2, inplace=True)

# Clip not actually inplace right now but could be
assert not np.shares_memory(get_array(df, "a"), arr_a)

if using_copy_on_write:
assert not np.shares_memory(get_array(df, "a"), arr_a)
assert df._mgr._has_no_reference(0)
assert view._mgr._has_no_reference(0)
tm.assert_frame_equal(df_copy, view)
else:
assert np.shares_memory(get_array(df, "a"), arr_a)