|
66 | 66 | can_hold_element,
|
67 | 67 | find_common_type,
|
68 | 68 | infer_dtype_from,
|
69 |
| - maybe_cast_to_integer_array, |
70 | 69 | validate_numeric_casting,
|
71 | 70 | )
|
72 | 71 | from pandas.core.dtypes.common import (
|
|
144 | 143 | from pandas.core.construction import (
|
145 | 144 | ensure_wrapped_if_datetimelike,
|
146 | 145 | extract_array,
|
| 146 | + sanitize_array, |
147 | 147 | )
|
148 | 148 | from pandas.core.indexers import deprecate_ndim_indexing
|
149 | 149 | from pandas.core.indexes.frozen import FrozenList
|
@@ -398,18 +398,17 @@ def __new__(
|
398 | 398 | # index-like
|
399 | 399 | elif isinstance(data, (np.ndarray, Index, ABCSeries)):
|
400 | 400 |
|
| 401 | + if isinstance(data, ABCMultiIndex): |
| 402 | + data = data._values |
| 403 | + |
401 | 404 | if dtype is not None:
|
402 | 405 | # we need to avoid having numpy coerce
|
403 | 406 | # things that look like ints/floats to ints unless
|
404 | 407 | # they are actually ints, e.g. '0' and 0.0
|
405 | 408 | # should not be coerced
|
406 | 409 | # GH 11836
|
| 410 | + data = sanitize_array(data, None, dtype=dtype, copy=copy) |
407 | 411 |
|
408 |
| - # error: Argument 1 to "_maybe_cast_with_dtype" has incompatible type |
409 |
| - # "Union[ndarray, Index, Series]"; expected "ndarray" |
410 |
| - data = _maybe_cast_with_dtype( |
411 |
| - data, dtype, copy # type: ignore[arg-type] |
412 |
| - ) |
413 | 412 | dtype = data.dtype
|
414 | 413 |
|
415 | 414 | if data.dtype.kind in ["i", "u", "f"]:
|
@@ -6314,56 +6313,6 @@ def maybe_extract_name(name, obj, cls) -> Hashable:
|
6314 | 6313 | return name
|
6315 | 6314 |
|
6316 | 6315 |
|
6317 |
| -def _maybe_cast_with_dtype(data: np.ndarray, dtype: np.dtype, copy: bool) -> np.ndarray: |
6318 |
| - """ |
6319 |
| - If a dtype is passed, cast to the closest matching dtype that is supported |
6320 |
| - by Index. |
6321 |
| -
|
6322 |
| - Parameters |
6323 |
| - ---------- |
6324 |
| - data : np.ndarray |
6325 |
| - dtype : np.dtype |
6326 |
| - copy : bool |
6327 |
| -
|
6328 |
| - Returns |
6329 |
| - ------- |
6330 |
| - np.ndarray |
6331 |
| - """ |
6332 |
| - # we need to avoid having numpy coerce |
6333 |
| - # things that look like ints/floats to ints unless |
6334 |
| - # they are actually ints, e.g. '0' and 0.0 |
6335 |
| - # should not be coerced |
6336 |
| - # GH 11836 |
6337 |
| - if is_integer_dtype(dtype): |
6338 |
| - inferred = lib.infer_dtype(data, skipna=False) |
6339 |
| - if inferred == "integer": |
6340 |
| - data = maybe_cast_to_integer_array(data, dtype, copy=copy) |
6341 |
| - elif inferred in ["floating", "mixed-integer-float"]: |
6342 |
| - if isna(data).any(): |
6343 |
| - raise ValueError("cannot convert float NaN to integer") |
6344 |
| - |
6345 |
| - if inferred == "mixed-integer-float": |
6346 |
| - data = maybe_cast_to_integer_array(data, dtype) |
6347 |
| - |
6348 |
| - # If we are actually all equal to integers, |
6349 |
| - # then coerce to integer. |
6350 |
| - try: |
6351 |
| - data = _try_convert_to_int_array(data, copy, dtype) |
6352 |
| - except ValueError: |
6353 |
| - data = np.array(data, dtype=np.float64, copy=copy) |
6354 |
| - |
6355 |
| - elif inferred != "string": |
6356 |
| - data = data.astype(dtype) |
6357 |
| - elif is_float_dtype(dtype): |
6358 |
| - inferred = lib.infer_dtype(data, skipna=False) |
6359 |
| - if inferred != "string": |
6360 |
| - data = data.astype(dtype) |
6361 |
| - else: |
6362 |
| - data = np.array(data, dtype=dtype, copy=copy) |
6363 |
| - |
6364 |
| - return data |
6365 |
| - |
6366 |
| - |
6367 | 6316 | def _maybe_cast_data_without_dtype(subarr):
|
6368 | 6317 | """
|
6369 | 6318 | If we have an arraylike input but no passed dtype, try to infer
|
|
0 commit comments