pandas.SparseArray

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

An ExtensionArray for storing sparse data.

Changed in version 0.24.0: Implements the ExtensionArray interface.

Parameters
dataarray-like

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

sparse_indexSparseIndex, optional
indexIndex
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:

data.dtype

na_value

float

np.nan

int

0

bool

False

datetime64

pd.NaT

timedelta64

pd.NaT

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 dtype is not a SparseDtype and data is a SparseArray.

kind{‘integer’, ‘block’}, default ‘integer’

The type of storage for sparse locations.

  • ‘block’: Stores a block and block_length for each contiguous span of sparse values. This is best when sparse data tends to be clumped together, with large regsions of fill-value values between sparse values.

  • ‘integer’: uses an integer to store the location of each sparse value.

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

T

Returns the SparseArray.

density

The percent of non- fill_value points, as decimal.

dtype

An instance of ‘ExtensionDtype’.

fill_value

Elements in data that are fill_value are not stored.

kind

The kind of sparse index for this array.

nbytes

The number of bytes needed to store this object in memory.

ndim

Extension Arrays are only allowed to be 1-dimensional.

npoints

The number of non- fill_value points.

shape

Return a tuple of the array dimensions.

sp_index

The SparseIndex containing the location of non- fill_value points.

sp_values

An ndarray containing the non- fill_value values.

values

(DEPRECATED) Dense values

Methods

all(self[, axis])

Tests whether all elements evaluate True

any(self[, axis])

Tests whether at least one of elements evaluate True

argsort(self[, ascending, kind])

Return the indices that would sort this array.

astype(self[, dtype, copy])

Change the dtype of a SparseArray.

copy(self[, deep])

Return a copy of the array.

cumsum(self[, axis])

Cumulative sum of non-NA/null values.

dropna(self)

Return ExtensionArray without NA values

factorize(self[, na_sentinel])

Encode the extension array as an enumerated type.

fillna(self[, value, method, limit])

Fill missing values with value.

from_spmatrix(data)

Create a SparseArray from a scipy.sparse matrix.

get_values(self)

Convert SparseArray to a NumPy array.

isna(self)

A 1-D array indicating if each value is missing.

map(self, mapper)

Map categories using input correspondence (dict, Series, or function).

mean(self[, axis])

Mean of non-NA/null values

repeat(self, repeats[, axis])

Repeat elements of a ExtensionArray.

searchsorted(self, v[, side, sorter])

Find indices where elements should be inserted to maintain order.

shift(self[, periods, fill_value])

Shift values by desired number.

sum(self[, axis])

Sum of non-NA/null values

take(self, indices[, allow_fill, fill_value])

Take elements from an array.

to_dense(self)

Convert SparseArray to a NumPy array.

transpose(self, \*axes)

Returns the SparseArray.

unique(self)

Compute the ExtensionArray of unique values.

value_counts(self[, dropna])

Returns a Series containing counts of unique values.

nonzero

Scroll To Top