Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 6 additions & 9 deletions pandas/core/arrays/numeric.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,10 +139,9 @@ def _safe_cast(cls, values: np.ndarray, dtype: np.dtype, copy: bool) -> np.ndarr
raise AbstractMethodError(cls)


def _coerce_to_data_and_mask(
values, dtype, copy: bool, dtype_cls: type[NumericDtype], default_dtype: np.dtype
):
def _coerce_to_data_and_mask(values, dtype, copy: bool, dtype_cls: type[NumericDtype]):
checker = dtype_cls._checker
default_dtype = dtype_cls._default_np_dtype

mask = None
inferred_type = None
Expand All @@ -163,7 +162,7 @@ def _coerce_to_data_and_mask(
if copy:
values = values.copy()
mask = mask.copy()
return values, mask, dtype, inferred_type
return values, mask

original = values
if not copy:
Expand All @@ -174,6 +173,7 @@ def _coerce_to_data_and_mask(
if values.dtype == object or is_string_dtype(values.dtype):
inferred_type = lib.infer_dtype(values, skipna=True)
if inferred_type == "boolean" and dtype is None:
# object dtype array of bools
name = dtype_cls.__name__.strip("_")
raise TypeError(f"{values.dtype} cannot be converted to {name}")

Expand Down Expand Up @@ -252,7 +252,7 @@ def _coerce_to_data_and_mask(
values = values.astype(dtype, copy=copy)
else:
values = dtype_cls._safe_cast(values, dtype, copy=False)
return values, mask, dtype, inferred_type
return values, mask


class NumericArray(BaseMaskedArray):
Expand Down Expand Up @@ -296,10 +296,7 @@ def _coerce_to_array(
cls, value, *, dtype: DtypeObj, copy: bool = False
) -> tuple[np.ndarray, np.ndarray]:
dtype_cls = cls._dtype_cls
default_dtype = dtype_cls._default_np_dtype
values, mask, _, _ = _coerce_to_data_and_mask(
value, dtype, copy, dtype_cls, default_dtype
)
values, mask = _coerce_to_data_and_mask(value, dtype, copy, dtype_cls)
return values, mask

@classmethod
Expand Down
Loading