pandas.SparseDtype — pandas 3.0.0.dev0+2103.g41968a550a documentation (original) (raw)

class pandas.SparseDtype(dtype=<class 'numpy.float64'>, fill_value=None)[source]#

Dtype for data stored in SparseArray.

SparseDtype is used as the data type for SparseArray, enabling more efficient storage of data that contains a significant number of repetitive values typically represented by a fill value. It supports any scalar dtype as the underlying data type of the non-fill values.

Parameters:

dtypestr, ExtensionDtype, numpy.dtype, type, default numpy.float64

The dtype of the underlying array storing the non-fill value values.

fill_valuescalar, optional

The scalar value not stored in the SparseArray. By default, this depends on dtype.

The default value may be overridden by specifying a fill_value.

Attributes

Methods

See also

arrays.SparseArray

The array structure that uses SparseDtype for data representation.

Examples

ser = pd.Series([1, 0, 0], dtype=pd.SparseDtype(dtype=int, fill_value=0)) ser 0 1 1 0 2 0 dtype: Sparse[int64, 0] ser.sparse.density 0.3333333333333333