pandas 1.4.2

ParametersReturns
astype(self, dtype: 'AstypeArg | None' = None, copy: 'bool' = True)

The output will always be a SparseArray. To convert to a dense ndarray with a certain dtype, use numpy.asarray .

Parameters

dtype : np.dtype or ExtensionDtype

For SparseDtype, this changes the dtype of self.sp_values and the self.fill_value .

For other dtypes, this only changes the dtype of self.sp_values .

copy : bool, default True

Whether to ensure a copy is made, even if not necessary.

Returns

SparseArray

Change the dtype of a SparseArray.

Examples

This example is valid syntax, but we were not able to check execution
>>> arr = pd.arrays.SparseArray([0, 0, 1, 2])
... arr [0, 0, 1, 2] Fill: 0 IntIndex Indices: array([2, 3], dtype=int32)
This example is valid syntax, but we were not able to check execution
>>> arr.astype(np.dtype('int32'))
[0, 0, 1, 2]
Fill: 0
IntIndex
Indices: array([2, 3], dtype=int32)

Using a NumPy dtype with a different kind (e.g. float) will coerce just self.sp_values .

This example is valid syntax, but we were not able to check execution
>>> arr.astype(np.dtype('float64'))
... # doctest: +NORMALIZE_WHITESPACE [0.0, 0.0, 1.0, 2.0] Fill: 0.0 IntIndex Indices: array([2, 3], dtype=int32)

Use a SparseDtype if you wish to be change the fill value as well.

This example is valid syntax, but we were not able to check execution
>>> arr.astype(SparseDtype("float64", fill_value=np.nan))
... # doctest: +NORMALIZE_WHITESPACE [nan, nan, 1.0, 2.0] Fill: nan IntIndex Indices: array([2, 3], dtype=int32)
See :

Local connectivity graph

Hover to see nodes names; edges to Self not shown, Caped at 50 nodes.

Using a canvas is more power efficient and can get hundred of nodes ; but does not allow hyperlinks; , arrows or text (beyond on hover)

SVG is more flexible but power hungry; and does not scale well to 50 + nodes.

All aboves nodes referred to, (or are referred from) current nodes; Edges from Self to other have been omitted (or all nodes would be connected to the central node "self" which is not useful). Nodes are colored by the library they belong to, and scaled with the number of references pointing them


File: /pandas/core/arrays/sparse/array.py#1205
type: <class 'function'>
Commit: