On this page
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
data
that arefill_value
are not stored in the SparseArray. For memory savings, this should be the most common value indata
. By default,fill_value
depends on the dtype ofdata
: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
- The
fill_value
argument dtype.fill_value
iffill_value
is None anddtype
is aSparseDtype
data.dtype.fill_value
iffill_value
is None anddtype
is not aSparseDtype
anddata
is aSparseArray
.
- The
-
kind : {‘integer’, ‘block’}, default ‘integer’
-
The type of storage for sparse locations.
- ‘block’: Stores a
block
andblock_length
for each contiguous span of sparse values. This is best when sparse data tends to be clumped together, with large regions offill-value
values 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_values
andself.fill_value
. -
copy : bool, default False
-
Whether to explicitly copy the incoming
data
array.
Attributes
None Methods
None -
© 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.25.0/reference/api/pandas.SparseArray.html