Skip to content

Commit ee64bd6

Browse files
committed
PERF: improve conversion to BooleanArray from int/float array
1 parent f1117bd commit ee64bd6

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

pandas/core/arrays/boolean.py

+11-2
Original file line numberDiff line numberDiff line change
@@ -130,9 +130,18 @@ def coerce_to_array(values, mask=None, copy: bool = False):
130130
if isinstance(values, np.ndarray) and values.dtype == np.bool_:
131131
if copy:
132132
values = values.copy()
133+
elif isinstance(values, np.ndarray) and values.dtype in (np.int_, np.float_):
134+
values_copy = values.copy()
135+
136+
mask_values = isna(values)
137+
values = np.zeros(len(values), dtype=bool)
138+
values[~mask_values] = values_copy[~mask_values].astype(bool)
139+
140+
if not np.all(
141+
values[~mask_values].astype(values.dtype) == values_copy[~mask_values]
142+
):
143+
raise TypeError("Need to pass bool-like values")
133144
else:
134-
# TODO conversion from integer/float ndarray can be done more efficiently
135-
# (avoid roundtrip through object)
136145
values_object = np.asarray(values, dtype=object)
137146

138147
inferred_dtype = lib.infer_dtype(values_object, skipna=True)

0 commit comments

Comments
 (0)