|
24 | 24 | pandas_dtype, |
25 | 25 | ) |
26 | 26 |
|
27 | | -import pandas as pd |
28 | 27 | from pandas.core.arrays.masked import ( |
29 | 28 | BaseMaskedArray, |
30 | 29 | BaseMaskedDtype, |
31 | 30 | ) |
| 31 | +from pandas.core.construction import extract_array |
32 | 32 |
|
33 | 33 | if TYPE_CHECKING: |
34 | 34 | from collections.abc import ( |
@@ -143,6 +143,7 @@ def _safe_cast(cls, values: np.ndarray, dtype: np.dtype, copy: bool) -> np.ndarr |
143 | 143 | def _coerce_to_data_and_mask( |
144 | 144 | values, dtype, copy: bool, dtype_cls: type[NumericDtype], default_dtype: np.dtype |
145 | 145 | ): |
| 146 | + values = extract_array(values, extract_numpy=True) |
146 | 147 | checker = dtype_cls._checker |
147 | 148 |
|
148 | 149 | mask = None |
@@ -232,17 +233,16 @@ def _coerce_to_data_and_mask( |
232 | 233 | values = np.ones(values.shape, dtype=dtype) |
233 | 234 | else: |
234 | 235 | idx = np.nanargmax(values) |
235 | | - if not (pd.isna(values[idx]) or pd.isna(original[idx])): |
236 | | - if int(values[idx]) != original[idx]: |
237 | | - # We have ints that lost precision during the cast. |
238 | | - inferred_type = lib.infer_dtype(original, skipna=True) |
239 | | - if ( |
240 | | - inferred_type not in ["floating", "mixed-integer-float"] |
241 | | - and not mask.any() |
242 | | - ): |
243 | | - values = np.asarray(original, dtype=dtype) |
244 | | - else: |
245 | | - values = np.asarray(original, dtype="object") |
| 236 | + if int(values[idx]) != original[idx]: |
| 237 | + # We have ints that lost precision during the cast. |
| 238 | + inferred_type = lib.infer_dtype(original, skipna=True) |
| 239 | + if ( |
| 240 | + inferred_type not in ["floating", "mixed-integer-float"] |
| 241 | + and not mask.any() |
| 242 | + ): |
| 243 | + values = np.asarray(original, dtype=dtype) |
| 244 | + else: |
| 245 | + values = np.asarray(original, dtype="object") |
246 | 246 |
|
247 | 247 | # we copy as need to coerce here |
248 | 248 | if mask.any(): |
|
0 commit comments