Should SparseArray.astype be dense or Sparse
See original GitHub issueRight now SparseArray.astype(numpy_dtype)
is sparse:
In [6]: a = pd.SparseArray([0, 1, 0, 1])
In [7]: a.astype(np.dtype('float'))
Out[7]:
[0, 1.0, 0, 1.0]
Fill: 0
IntIndex
Indices: array([1, 3], dtype=int32)
This is potentially confusing. I did it to match the behavior of SparseSeries, but we may not want that.
Issue Analytics
- State:
- Created 5 years ago
- Reactions:1
- Comments:16 (16 by maintainers)
Top Results From Across the Web
pandas.SparseArray.astype — pandas 0.24.2 documentation
Change the dtype of a SparseArray. The output will always be a SparseArray. To convert to a dense ndarray with a certain dtype,...
Read more >Python SparseArray Dtype to Float - pandas - Stack Overflow
If you don't need sparsity anymore, use SparseArray.values.to_dense() to convert the series into a dense numpy array. The .astype() function ...
Read more >Sparse data structures — pandas 1.0.0rc0+111.ge72cd7c52 ...
A sparse array can be converted to a regular (dense) ndarray with numpy.asarray() ... From dense to sparse, use DataFrame.astype() with a SparseDtype...
Read more >Dataframe from sparse array - Dask Forum - Discourse
I want to create a sparse array with dask without ever creating a dense matrix (at least not in memory).
Read more >scipy.sparse.csr_matrix — SciPy v1.9.3 Manual
with a dense matrix or rank-2 ndarray D ... Sparse matrices can be used in arithmetic operations: they support addition, ... astype (dtype[,...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I think this is a regression compared to released version. Compare the last example:
When
astype
-ing with a numpy dtype, I think we should simply “astype” the scalar fill_value as well.Long term it might be nice to be able to make the distinction between
astype('int')
andastype(np.int64)
, where the first could be interpreted by pandas and keep the sparsity, while the second could ensure the output actually has dtype ofnp.int64
. But that might also be confusing …