pandas.arrays.SparseArray — pandas 3.0.0.dev0+2098.g9c5b9ee823 documentation (original) (raw)

class pandas.arrays.SparseArray(data, sparse_index=None, fill_value=None, kind='integer', dtype=None, copy=False)[source]#

An ExtensionArray for storing sparse data.

SparseArray efficiently stores data with a high frequency of a specific fill value (e.g., zeros), saving memory by only retaining non-fill elements and their indices. This class is particularly useful for large datasets where most values are redundant.

Parameters:

dataarray-like or scalar

A dense array of values to store in the SparseArray. This may containfill_value.

sparse_indexSparseIndex, optional

Index indicating the locations of sparse elements.

fill_valuescalar, optional

Elements in data that are fill_value are not stored in the SparseArray. For memory savings, this should be the most common value in data. By default, fill_value depends on the dtype of data:

The fill value is potentially specified in three ways. In order of precedence, these are

  1. The fill_value argument
  2. dtype.fill_value if fill_value is None and dtype is a SparseDtype
  3. data.dtype.fill_value if fill_value is None and dtypeis not a SparseDtype and data is a SparseArray.

kindstr

Can be ‘integer’ or ‘block’, default is ‘integer’. The type of storage for sparse locations.

dtypenp.dtype or SparseDtype, optional

The dtype to use for the SparseArray. For numpy dtypes, this determines the dtype of self.sp_values. For SparseDtype, this determines self.sp_values and self.fill_value.

copybool, default False

Whether to explicitly copy the incoming data array.

Attributes

Methods

See also

SparseDtype

Dtype for sparse data.

Examples

from pandas.arrays import SparseArray arr = SparseArray([0, 0, 1, 2]) arr [0, 0, 1, 2] Fill: 0 IntIndex Indices: array([2, 3], dtype=int32)