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: -
data : array-like -
A dense array of values to store in the SparseArray. This may contain
fill_value. -
sparse_index : SparseIndex, optional -
index : Index -
fill_value : scalar, optional -
Elements in
datathat arefill_valueare not stored in the SparseArray. For memory savings, this should be the most common value indata. By default,fill_valuedepends on the dtype ofdata:data.dtype na_value float np.nanint 0bool False datetime64 pd.NaTtimedelta64 pd.NaTThe fill value is potentiall specified in three ways. In order of precedence, these are
- The
fill_valueargument -
dtype.fill_valueiffill_valueis None anddtypeis aSparseDtype -
data.dtype.fill_valueiffill_valueis None anddtypeis not aSparseDtypeanddatais aSparseArray.
- The
-
kind : {‘integer’, ‘block’}, default ‘integer’ -
The type of storage for sparse locations.
- ‘block’: Stores a
blockandblock_lengthfor each contiguous span of sparse values. This is best when sparse data tends to be clumped together, with large regsions offill-valuevalues between sparse values. - ‘integer’: uses an integer to store the location of each sparse value.
- ‘block’: Stores a
-
dtype : np.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 determinesself.sp_valuesandself.fill_value. -
copy : bool, default False -
Whether to explicitly copy the incoming
dataarray.
Attributes
TReturns the SparseArray. densityThe percent of non- fill_valuepoints, as decimal.dtypeAn instance of ‘ExtensionDtype’. fill_valueElements in datathat arefill_valueare not stored.kindThe kind of sparse index for this array. nbytesThe number of bytes needed to store this object in memory. ndimExtension Arrays are only allowed to be 1-dimensional. npointsThe number of non- fill_valuepoints.shapeReturn a tuple of the array dimensions. sp_indexThe SparseIndex containing the location of non- fill_valuepoints.sp_valuesAn ndarray containing the non- fill_valuevalues.valuesDense values Methods
all([axis])Tests whether all elements evaluate True any([axis])Tests whether at least one of elements evaluate True argsort([ascending, kind])Return the indices that would sort this array. astype([dtype, copy])Change the dtype of a SparseArray. copy([deep])Return a copy of the array. cumsum([axis])Cumulative sum of non-NA/null values. dropna()Return ExtensionArray without NA values factorize([na_sentinel])Encode the extension array as an enumerated type. fillna([value, method, limit])Fill missing values with value.get_values()Convert SparseArray to a NumPy array. isna()A 1-D array indicating if each value is missing. map(mapper)Map categories using input correspondence (dict, Series, or function). mean([axis])Mean of non-NA/null values repeat(repeats[, axis])Repeat elements of a ExtensionArray. searchsorted(v[, side, sorter])Find indices where elements should be inserted to maintain order. shift([periods, fill_value])Shift values by desired number. sum([axis])Sum of non-NA/null values take(indices[, allow_fill, fill_value])Take elements from an array. to_dense()Convert SparseArray to a NumPy array. transpose(*axes)Returns the SparseArray. unique()Compute the ExtensionArray of unique values. value_counts([dropna])Returns a Series containing counts of unique values. nonzero -
© 2008–2012, AQR Capital Management, LLC, Lambda Foundry, Inc. and PyData Development Team
Licensed under the 3-clause BSD License.
https://pandas.pydata.org/pandas-docs/version/0.24.2/reference/api/pandas.SparseArray.html