(
self,
values,
categories=None,
ordered=None,
dtype: Dtype | None = None,
copy: bool = True,
)
| 385 | return super()._simple_new(codes, dtype) |
| 386 | |
| 387 | def __init__( |
| 388 | self, |
| 389 | values, |
| 390 | categories=None, |
| 391 | ordered=None, |
| 392 | dtype: Dtype | None = None, |
| 393 | copy: bool = True, |
| 394 | ) -> None: |
| 395 | dtype = CategoricalDtype._from_values_or_dtype( |
| 396 | values, categories, ordered, dtype |
| 397 | ) |
| 398 | # At this point, dtype is always a CategoricalDtype, but |
| 399 | # we may have dtype.categories be None, and we need to |
| 400 | # infer categories in a factorization step further below |
| 401 | |
| 402 | if not is_list_like(values): |
| 403 | # GH#38433 |
| 404 | raise TypeError("Categorical input must be list-like") |
| 405 | |
| 406 | # null_mask indicates missing values we want to exclude from inference. |
| 407 | # This means: only missing values in list-likes (not arrays/ndframes). |
| 408 | null_mask = np.array(False) |
| 409 | |
| 410 | # sanitize input |
| 411 | vdtype = getattr(values, "dtype", None) |
| 412 | if isinstance(vdtype, CategoricalDtype): |
| 413 | if dtype.categories is None: |
| 414 | dtype = CategoricalDtype(values.categories, dtype.ordered) |
| 415 | elif isinstance(values, range): |
| 416 | from pandas.core.indexes.range import RangeIndex |
| 417 | |
| 418 | values = RangeIndex(values) |
| 419 | elif not isinstance(values, (ABCIndex, ABCSeries, ExtensionArray)): |
| 420 | values = com.convert_to_list_like(values) |
| 421 | if isinstance(values, list) and len(values) == 0: |
| 422 | # By convention, empty lists result in object dtype: |
| 423 | values = np.array([], dtype=object) |
| 424 | elif isinstance(values, np.ndarray): |
| 425 | if values.ndim > 1: |
| 426 | # preempt sanitize_array from raising ValueError |
| 427 | raise NotImplementedError( |
| 428 | "> 1 ndim Categorical are not supported at this time" |
| 429 | ) |
| 430 | values = sanitize_array(values, None) |
| 431 | else: |
| 432 | # i.e. must be a list |
| 433 | arr = sanitize_array(values, None) |
| 434 | null_mask = isna(arr) |
| 435 | if null_mask.any(): |
| 436 | # We remove null values here, then below will re-insert |
| 437 | # them, grep "full_codes" |
| 438 | arr_list = [values[idx] for idx in np.where(~null_mask)[0]] |
| 439 | |
| 440 | # GH#44900 Do not cast to float if we have only missing values |
| 441 | if arr_list or arr.dtype == "object": |
| 442 | sanitize_dtype = None |
| 443 | else: |
| 444 | sanitize_dtype = arr.dtype |
no test coverage detected