Skip to content

PERF: improve conversion to BooleanArray from int/float array #29838

Closed
@jorisvandenbossche

Description

@jorisvandenbossche

Currently, the creation of a BooleanArray from an int/float array goes through a conversion to object dtype (to do it together with the generic conversion from any list-like):

else:
# TODO conversion from integer/float ndarray can be done more efficiently
# (avoid roundtrip through object)
values_object = np.asarray(values, dtype=object)
inferred_dtype = lib.infer_dtype(values_object, skipna=True)
integer_like = ("floating", "integer", "mixed-integer-float")
if inferred_dtype not in ("boolean", "empty") + integer_like:
raise TypeError("Need to pass bool-like values")
mask_values = isna(values_object)
values = np.zeros(len(values), dtype=bool)
values[~mask_values] = values_object[~mask_values].astype(bool)

For the specific case of int/float ndarray, this could be optimized with a specific path for those cases without the casting to object array (probably just skipping the np.asarray(values, dtype=object) if values is a float/int ndarray will be enough).

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions