Skip to content

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

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

Merged
merged 8 commits into from
Dec 9, 2019
13 changes: 11 additions & 2 deletions pandas/core/arrays/boolean.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,18 @@ def coerce_to_array(values, mask=None, copy: bool = False):
if isinstance(values, np.ndarray) and values.dtype == np.bool_:
if copy:
values = values.copy()
elif isinstance(values, np.ndarray) and values.dtype in (np.int_, np.float_):
values_copy = values.copy()

mask_values = isna(values)
values = np.zeros(len(values), dtype=bool)
values[~mask_values] = values_copy[~mask_values].astype(bool)

if not np.all(
values[~mask_values].astype(values.dtype) == values_copy[~mask_values]
):
raise TypeError("Need to pass bool-like values")
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)
Expand Down