API: SparseArray.astype behaviour to always preserve sparseness #34457
Labels
Astype
Enhancement
Needs Discussion
Requires discussion from core team before further action
Sparse
Sparse Data Type
Milestone
Currently, the
SparseArray.astype
function will always convert the specified target dtype to a sparse dtype, if it is not one. For example, this gives:This ensures that a simple
astype
doesn't densify the sparse array (and you don't need to doastype(pd.SparseDtype(float, fill_value))
).And note this also gives this behaviour to
Series.astype(..)
But, this also gives the inconsistency that
arr.astype(target_dtype).dtype != target_dtype
, so you can rely on the fact that you get back an array of the actual dtype that you specified.See eg the workaround I need to add for this in #34338
The text was updated successfully, but these errors were encountered: