- API reference
- pandas arrays, scalars, and data types
- pandas.array...
pandas.arrays.SparseArray#
- classpandas.arrays.SparseArray(data,sparse_index=None,fill_value=None,kind='integer',dtype=None,copy=False)[source]#
An ExtensionArray for storing sparse data.
- Parameters:
- dataarray-like or scalar
A dense array of values to store in the SparseArray. This may containfill_value.
- sparse_indexSparseIndex, optional
- fill_valuescalar, optional
Elements in data that are
fill_value
are not stored in theSparseArray. For memory savings, this should be the most common valueindata. 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 ofprecedence, these are
Thefill_value argument
dtype.fill_value
iffill_value is None anddtype isaSparseDtype
data.dtype.fill_value
iffill_value is None anddtypeis not aSparseDtype
anddata is aSparseArray
.
- kindstr
Can be ‘integer’ or ‘block’, default is ‘integer’.The type of storage for sparse locations.
‘block’: Stores ablock andblock_length for eachcontiguousspan of sparse values. This is best whensparse data tends to be clumped together, with largeregions of
fill-value
values between sparse values.‘integer’: uses an integer to store the location ofeach sparse value.
- dtypenp.dtype or SparseDtype, optional
The dtype to use for the SparseArray. For numpy dtypes, thisdetermines the dtype of
self.sp_values
. For SparseDtype,this determinesself.sp_values
andself.fill_value
.- copybool, default False
Whether to explicitly copy the incomingdata array.
Attributes
None
Methods
None
Examples
>>>frompandas.arraysimportSparseArray>>>arr=SparseArray([0,0,1,2])>>>arr[0, 0, 1, 2]Fill: 0IntIndexIndices: array([2, 3], dtype=int32)