-
-
Notifications
You must be signed in to change notification settings - Fork 18.7k
Closed
Labels
ExtensionArrayExtending pandas with custom dtypes or arrays.Extending pandas with custom dtypes or arrays.good first issue
Milestone
Description
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):
pandas/pandas/core/arrays/boolean.py
Lines 133 to 145 in 7d7f885
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
ExtensionArrayExtending pandas with custom dtypes or arrays.Extending pandas with custom dtypes or arrays.good first issue