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: DataFrame.where for EA dtype mask #51574

Merged
merged 6 commits into from
Feb 25, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions asv_bench/benchmarks/frame_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -754,4 +754,19 @@ def time_memory_usage_object_dtype(self):
self.df2.memory_usage(deep=True)


class Where:
params = (
[True, False],
["float64", "Float64", "float64[pyarrow]"],
)
param_names = ["dtype"]

def setup(self, inplace, dtype):
self.df = DataFrame(np.random.randn(100_000, 10), dtype=dtype)
self.mask = self.df < 0

def time_where(self, inplace, dtype):
self.df.where(self.mask, other=0.0, inplace=inplace)


from .pandas_vb_common import setup # noqa: F401 isort:skip
1 change: 1 addition & 0 deletions doc/source/whatsnew/v2.1.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ Deprecations

Performance improvements
~~~~~~~~~~~~~~~~~~~~~~~~
- Performance improvement in :meth:`DataFrame.where` when ``cond`` is backed by an extension dtype (:issue:`51574`)
- Performance improvement in :meth:`DataFrame.clip` and :meth:`Series.clip` (:issue:`51472`)
-

Expand Down
16 changes: 16 additions & 0 deletions pandas/core/internals/managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,14 @@ def where(self: T, other, cond, align: bool) -> T:
align_keys = ["cond"]
other = extract_array(other, extract_numpy=True)

if isinstance(cond, ABCDataFrame) and cond._mgr.any_extension_types:
# GH51574: avoid object ndarray conversion later on
cond = cond._constructor(
cond.to_numpy(dtype=bool, na_value=False),
index=cond.index,
columns=cond.columns,
)

return self.apply(
"where",
align_keys=align_keys,
Expand Down Expand Up @@ -390,6 +398,14 @@ def putmask(self, mask, new, align: bool = True):
align_keys = ["mask"]
new = extract_array(new, extract_numpy=True)

if isinstance(mask, ABCDataFrame) and mask._mgr.any_extension_types:
Copy link
Member

Choose a reason for hiding this comment

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

is there a viable option to avoid getting here with DataFrames?

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 suspect something could be done in NDFrame._where to avoid most cases. And then maybe .clip could just call _where?

If you're already thinking about changes to ._where in #51547 I can close this if it makes sense to combine the two.

Copy link
Member Author

@lukemanley lukemanley Feb 24, 2023

Choose a reason for hiding this comment

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

I've moved this to NDFrame._where and updated the timings in the OP.

# GH51574: avoid object ndarray conversion later on
mask = mask._constructor(
mask.to_numpy(dtype=bool, na_value=False),
index=mask.index,
columns=mask.columns,
)

return self.apply(
"putmask",
align_keys=align_keys,
Expand Down