Skip to content

Commit

Permalink
PERF: Use fused types for map_infer_mask
Browse files Browse the repository at this point in the history
  • Loading branch information
rhshadrach committed Oct 27, 2023
1 parent b7ab856 commit 5921bf1
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 2 deletions.
7 changes: 7 additions & 0 deletions pandas/_libs/dtypes.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ from numpy cimport (
int16_t,
int32_t,
int64_t,
npy_bool,
uint8_t,
uint16_t,
uint32_t,
Expand All @@ -34,3 +35,9 @@ ctypedef fused numeric_t:
ctypedef fused numeric_object_t:
numeric_t
object

# bool + all numeric types + object, doesn't include complex
ctypedef fused bool_numeric_object_t:
npy_bool
numeric_t
object
27 changes: 25 additions & 2 deletions pandas/_libs/lib.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ cdef extern from "pandas/parser/pd_parser.h":
PandasParser_IMPORT

from pandas._libs cimport util
from pandas._libs.dtypes cimport bool_numeric_object_t
from pandas._libs.util cimport (
INT64_MAX,
INT64_MIN,
Expand Down Expand Up @@ -2855,12 +2856,34 @@ no_default = _NoDefault.no_default # Sentinel indicating the default value.
NoDefault = Literal[_NoDefault.no_default]


def map_infer_mask(
ndarray[object] arr,
object f,
const uint8_t[:] mask,
bint convert=True,
object na_value=no_default,
cnp.dtype dtype=np.dtype(object)
) -> np.ndarray:
dummy = np.empty(0, dtype=dtype)
result = _map_infer_mask(
arr,
f,
mask,
dummy,
convert,
na_value,
dtype,
)
return result


@cython.boundscheck(False)
@cython.wraparound(False)
def map_infer_mask(
def _map_infer_mask(
ndarray[object] arr,
object f,
const uint8_t[:] mask,
bool_numeric_object_t[:] dummy,
bint convert=True,
object na_value=no_default,
cnp.dtype dtype=np.dtype(object)
Expand Down Expand Up @@ -2888,7 +2911,7 @@ def map_infer_mask(
"""
cdef:
Py_ssize_t i, n
ndarray result
ndarray[bool_numeric_object_t] result
object val

n = len(arr)
Expand Down
2 changes: 2 additions & 0 deletions pandas/core/arrays/string_.py
Original file line number Diff line number Diff line change
Expand Up @@ -624,6 +624,8 @@ def _str_map(
na_value_is_na = isna(na_value)
if na_value_is_na:
na_value = 1
elif dtype == np.dtype("bool"):
na_value = bool(na_value)
result = lib.map_infer_mask(
arr,
f,
Expand Down

0 comments on commit 5921bf1

Please sign in to comment.