-
-
Notifications
You must be signed in to change notification settings - Fork 18.6k
PERF: Use fused types for map_infer_mask #55736
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
Changes from 6 commits
5921bf1
5c4d8d9
c4bdb1a
c58e682
8ff9047
4634a24
59c836d
da55ccc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -102,6 +102,7 @@ cdef extern from "pandas/parser/pd_parser.h": | |
PandasParser_IMPORT | ||
|
||
from pandas._libs cimport util | ||
from pandas._libs.dtypes cimport uint8_int64_object_t | ||
from pandas._libs.util cimport ( | ||
INT64_MAX, | ||
INT64_MIN, | ||
|
@@ -2856,8 +2857,6 @@ no_default = _NoDefault.no_default # Sentinel indicating the default value. | |
NoDefault = Literal[_NoDefault.no_default] | ||
|
||
|
||
@cython.boundscheck(False) | ||
@cython.wraparound(False) | ||
def map_infer_mask( | ||
ndarray[object] arr, | ||
object f, | ||
|
@@ -2876,24 +2875,54 @@ def map_infer_mask( | |
mask : ndarray | ||
uint8 dtype ndarray indicating values not to apply `f` to. | ||
convert : bool, default True | ||
Whether to call `maybe_convert_objects` on the resulting ndarray | ||
Whether to call `maybe_convert_objects` on the resulting ndarray. | ||
na_value : Any, optional | ||
The result value to use for masked values. By default, the | ||
input value is used | ||
input value is used. | ||
dtype : numpy.dtype | ||
The numpy dtype to use for the result ndarray. | ||
|
||
Returns | ||
------- | ||
np.ndarray | ||
""" | ||
cdef Py_ssize_t n = len(arr) | ||
result = np.empty(n, dtype=dtype) | ||
|
||
_map_infer_mask( | ||
result, | ||
arr, | ||
f, | ||
mask, | ||
convert, | ||
na_value, | ||
dtype, | ||
) | ||
if convert: | ||
return maybe_convert_objects(result) | ||
else: | ||
return result | ||
|
||
|
||
@cython.boundscheck(False) | ||
@cython.wraparound(False) | ||
def _map_infer_mask( | ||
ndarray[uint8_int64_object_t] out, | ||
ndarray[object] arr, | ||
object f, | ||
const uint8_t[:] mask, | ||
bint convert=True, | ||
object na_value=no_default, | ||
cnp.dtype dtype=np.dtype(object) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i think dtype and convert are not needed here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Doh - this is why you don't refactor late at night 😆 Removed, thanks. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @lithomas1 were you once looking at adding Wextra or increasing the meson warning setting? I know we'd have to do a bit more work to get there but I think that would catch issues like this in the future (via -Wunused-argument, which could alternately be added directly) |
||
): | ||
""" | ||
jbrockmendel marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Helper for map_infer_mask, split off to use fused types based on the result. | ||
""" | ||
cdef: | ||
Py_ssize_t i, n | ||
ndarray result | ||
object val | ||
|
||
n = len(arr) | ||
result = np.empty(n, dtype=dtype) | ||
for i in range(n): | ||
if mask[i]: | ||
if na_value is no_default: | ||
|
@@ -2907,12 +2936,7 @@ def map_infer_mask( | |
# unbox 0-dim arrays, GH#690 | ||
val = val.item() | ||
|
||
result[i] = val | ||
|
||
if convert: | ||
return maybe_convert_objects(result) | ||
else: | ||
return result | ||
out[i] = val | ||
|
||
|
||
@cython.boundscheck(False) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
make this a cdef function?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't believe so. When I try, I get:
I believe this is cython/cython#2462
I'm not certain, but I believe it happens when calling a cdef function from a def function where the def function does not use the fused types.