(
self,
data,
sparse_index=None,
fill_value=None,
kind: SparseIndexKind = "integer",
dtype: Dtype | None = None,
copy: bool = False,
)
| 381 | _dtype: SparseDtype |
| 382 | |
| 383 | def __init__( |
| 384 | self, |
| 385 | data, |
| 386 | sparse_index=None, |
| 387 | fill_value=None, |
| 388 | kind: SparseIndexKind = "integer", |
| 389 | dtype: Dtype | None = None, |
| 390 | copy: bool = False, |
| 391 | ) -> None: |
| 392 | if fill_value is None and isinstance(dtype, SparseDtype): |
| 393 | fill_value = dtype.fill_value |
| 394 | |
| 395 | if isinstance(data, type(self)): |
| 396 | # disable normal inference on dtype, sparse_index, & fill_value |
| 397 | if sparse_index is None: |
| 398 | sparse_index = data.sp_index |
| 399 | if fill_value is None: |
| 400 | fill_value = data.fill_value |
| 401 | if dtype is None: |
| 402 | dtype = data.dtype |
| 403 | # TODO: make kind=None, and use data.kind? |
| 404 | data = data.sp_values |
| 405 | |
| 406 | # Handle use-provided dtype |
| 407 | if isinstance(dtype, str): |
| 408 | # Two options: dtype='int', regular numpy dtype |
| 409 | # or dtype='Sparse[int]', a sparse dtype |
| 410 | try: |
| 411 | dtype = SparseDtype.construct_from_string(dtype) |
| 412 | except TypeError: |
| 413 | dtype = pandas_dtype(dtype) |
| 414 | |
| 415 | if isinstance(dtype, SparseDtype): |
| 416 | if fill_value is None: |
| 417 | fill_value = dtype.fill_value |
| 418 | dtype = dtype.subtype |
| 419 | |
| 420 | if is_scalar(data): |
| 421 | raise TypeError( |
| 422 | f"Cannot construct {type(self).__name__} from scalar data. " |
| 423 | "Pass a sequence instead." |
| 424 | ) |
| 425 | |
| 426 | if dtype is not None: |
| 427 | dtype = pandas_dtype(dtype) |
| 428 | |
| 429 | # TODO: disentangle the fill_value dtype inference from |
| 430 | # dtype inference |
| 431 | if data is None: |
| 432 | # TODO: What should the empty dtype be? Object or float? |
| 433 | |
| 434 | # error: Argument "dtype" to "array" has incompatible type |
| 435 | # "Union[ExtensionDtype, dtype[Any], None]"; expected "Union[dtype[Any], |
| 436 | # None, type, _SupportsDType, str, Union[Tuple[Any, int], Tuple[Any, |
| 437 | # Union[int, Sequence[int]]], List[Any], _DTypeDict, Tuple[Any, Any]]]" |
| 438 | data = np.array([], dtype=dtype) # type: ignore[arg-type] |
| 439 | |
| 440 | try: |
nothing calls this directly
no test coverage detected