MCPcopy
hub / github.com/pandas-dev/pandas / __init__

Method __init__

pandas/core/arrays/sparse/array.py:383–506  ·  view source on GitHub ↗
(
        self,
        data,
        sparse_index=None,
        fill_value=None,
        kind: SparseIndexKind = "integer",
        dtype: Dtype | None = None,
        copy: bool = False,
    )

Source from the content-addressed store, hash-verified

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:

Callers

nothing calls this directly

Calls 11

pandas_dtypeFunction · 0.90
sanitize_arrayFunction · 0.90
na_value_for_dtypeFunction · 0.90
extract_arrayFunction · 0.90
find_stack_levelFunction · 0.90
SparseDtypeClass · 0.90
_make_sparseFunction · 0.85
construct_from_stringMethod · 0.45
arrayMethod · 0.45
dtypeMethod · 0.45
copyMethod · 0.45

Tested by

no test coverage detected