A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://github.com/pandas-dev/pandas/issues/34457 below:

SparseArray.astype behaviour to always preserve sparseness · Issue #34457 · pandas-dev/pandas · GitHub

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:

In [64]: arr = pd.arrays.SparseArray([1, 0, 0, 2])  

In [65]: arr   
Out[65]: 
[1, 0, 0, 2]
Fill: 0
IntIndex
Indices: array([0, 3], dtype=int32)

In [66]: arr.astype(float)  
Out[66]: 
[1.0, 0.0, 0.0, 2.0]
Fill: 0.0
IntIndex
Indices: array([0, 3], dtype=int32)

This ensures that a simple astype doesn't densify the sparse array (and you don't need to do astype(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


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4