|
9 | 9 | import numpy as np
|
10 | 10 | import numpy.ma as ma
|
11 | 11 |
|
12 |
| -from pandas._libs import lib, tslibs |
| 12 | +from pandas._libs import lib |
13 | 13 | from pandas._libs.tslibs import IncompatibleFrequency, OutOfBoundsDatetime
|
14 | 14 |
|
15 | 15 | from pandas.core.dtypes.cast import (
|
|
36 | 36 | is_timedelta64_ns_dtype,
|
37 | 37 | pandas_dtype,
|
38 | 38 | )
|
39 |
| -from pandas.core.dtypes.dtypes import ExtensionDtype, registry |
| 39 | +from pandas.core.dtypes.dtypes import CategoricalDtype, ExtensionDtype, registry |
40 | 40 | from pandas.core.dtypes.generic import (
|
41 | 41 | ABCExtensionArray,
|
42 | 42 | ABCIndexClass,
|
@@ -275,7 +275,7 @@ def array(
|
275 | 275 | if inferred_dtype == "period":
|
276 | 276 | try:
|
277 | 277 | return period_array(data, copy=copy)
|
278 |
| - except tslibs.IncompatibleFrequency: |
| 278 | + except IncompatibleFrequency: |
279 | 279 | # We may have a mixture of frequencies.
|
280 | 280 | # We choose to return an ndarray, rather than raising.
|
281 | 281 | pass
|
@@ -365,7 +365,9 @@ def extract_array(obj, extract_numpy=False):
|
365 | 365 | return obj
|
366 | 366 |
|
367 | 367 |
|
368 |
| -def sanitize_array(data, index, dtype=None, copy=False, raise_cast_failure=False): |
| 368 | +def sanitize_array( |
| 369 | + data, index, dtype=None, copy: bool = False, raise_cast_failure: bool = False |
| 370 | +): |
369 | 371 | """
|
370 | 372 | Sanitize input data to an ndarray, copy if specified, coerce to the
|
371 | 373 | dtype if specified.
|
@@ -486,13 +488,19 @@ def sanitize_array(data, index, dtype=None, copy=False, raise_cast_failure=False
|
486 | 488 | return subarr
|
487 | 489 |
|
488 | 490 |
|
489 |
| -def _try_cast(arr, dtype, copy, raise_cast_failure): |
| 491 | +def _try_cast( |
| 492 | + arr, |
| 493 | + dtype: Optional[Union[np.dtype, "ExtensionDtype"]], |
| 494 | + copy: bool, |
| 495 | + raise_cast_failure: bool, |
| 496 | +): |
490 | 497 | """
|
491 | 498 | Convert input to numpy ndarray and optionally cast to a given dtype.
|
492 | 499 |
|
493 | 500 | Parameters
|
494 | 501 | ----------
|
495 |
| - arr : array-like |
| 502 | + arr : ndarray, list, tuple, iterator (catchall) |
| 503 | + Excludes: ExtensionArray, Series, Index. |
496 | 504 | dtype : np.dtype, ExtensionDtype or None
|
497 | 505 | copy : bool
|
498 | 506 | If False, don't copy the data if not needed.
|
@@ -528,11 +536,13 @@ def _try_cast(arr, dtype, copy, raise_cast_failure):
|
528 | 536 | if is_categorical_dtype(dtype):
|
529 | 537 | # We *do* allow casting to categorical, since we know
|
530 | 538 | # that Categorical is the only array type for 'category'.
|
| 539 | + dtype = cast(CategoricalDtype, dtype) |
531 | 540 | subarr = dtype.construct_array_type()(
|
532 | 541 | arr, dtype.categories, ordered=dtype._ordered
|
533 | 542 | )
|
534 | 543 | elif is_extension_array_dtype(dtype):
|
535 | 544 | # create an extension array from its dtype
|
| 545 | + dtype = cast(ExtensionDtype, dtype) |
536 | 546 | array_type = dtype.construct_array_type()._from_sequence
|
537 | 547 | subarr = array_type(arr, dtype=dtype, copy=copy)
|
538 | 548 | elif dtype is not None and raise_cast_failure:
|
|
0 commit comments