|
48 | 48 | from pandas.core.dtypes.cast import (
|
49 | 49 | coerce_indexer_dtype,
|
50 | 50 | maybe_cast_to_extension_array,
|
51 |
| - maybe_infer_to_datetimelike, |
52 |
| - sanitize_to_nanoseconds, |
53 | 51 | )
|
54 | 52 | from pandas.core.dtypes.common import (
|
55 | 53 | ensure_int64,
|
@@ -396,24 +394,27 @@ def __init__(
|
396 | 394 | if dtype.categories is None:
|
397 | 395 | dtype = CategoricalDtype(values.categories, dtype.ordered)
|
398 | 396 | elif not isinstance(values, (ABCIndex, ABCSeries, ExtensionArray)):
|
399 |
| - # sanitize_array coerces np.nan to a string under certain versions |
400 |
| - # of numpy |
401 |
| - if not isinstance(values, (np.ndarray, list)): |
402 |
| - # convert e.g. range, tuple to allow for stronger typing |
403 |
| - # of maybe_infer_to_datetimelike |
404 |
| - values = list(values) |
405 |
| - values = maybe_infer_to_datetimelike(values) |
406 |
| - if isinstance(values, np.ndarray): |
407 |
| - values = sanitize_to_nanoseconds(values) |
408 |
| - elif not isinstance(values, ExtensionArray): |
409 |
| - values = com.convert_to_list_like(values) |
410 |
| - |
| 397 | + values = com.convert_to_list_like(values) |
| 398 | + if isinstance(values, list) and len(values) == 0: |
411 | 399 | # By convention, empty lists result in object dtype:
|
412 |
| - sanitize_dtype = np.dtype("O") if len(values) == 0 else None |
413 |
| - null_mask = isna(values) |
| 400 | + values = np.array([], dtype=object) |
| 401 | + elif isinstance(values, np.ndarray): |
| 402 | + if values.ndim > 1: |
| 403 | + # preempt sanitize_array from raising ValueError |
| 404 | + raise NotImplementedError( |
| 405 | + "> 1 ndim Categorical are not supported at this time" |
| 406 | + ) |
| 407 | + values = sanitize_array(values, None) |
| 408 | + else: |
| 409 | + # i.e. must be a list |
| 410 | + arr = sanitize_array(values, None) |
| 411 | + null_mask = isna(arr) |
414 | 412 | if null_mask.any():
|
415 |
| - values = [values[idx] for idx in np.where(~null_mask)[0]] |
416 |
| - values = sanitize_array(values, None, dtype=sanitize_dtype) |
| 413 | + # We remove null values here, then below will re-insert |
| 414 | + # them, grep "full_codes" |
| 415 | + arr = [values[idx] for idx in np.where(~null_mask)[0]] |
| 416 | + arr = sanitize_array(arr, None) |
| 417 | + values = arr |
417 | 418 |
|
418 | 419 | if dtype.categories is None:
|
419 | 420 | try:
|
|
0 commit comments