You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
importpandasaspdimportscipyasspimportscipy.sparsespmatrix=sp.sparse.csr_matrix((2, 2))
spmatrix[0, 0] =1df=pd.DataFrame.sparse.from_spmatrix(spmatrix)
# work as expected, cooldf.loc[1]
# 0 0.0# 1 0.0# Name: 0, dtype: Sparse[float64, 0.0]# Indexing by index list. I expect 1,0, 0.0, not cooldf.loc[[1]]
# 0 1# 1 0.0 0# whenever indexing an all-zero series, it returns int type.df.loc[[1]].loc[[1]]
# 0 1# 1 0 0# checking the dtype, it has been turned into int64 instead of float64df.loc[[1]].dtypes# 0 Sparse[float64, 0]# 1 Sparse[int64, 0]# dtype: object# indexing by bool list also not cooldf.loc[[True, True]]
# 0 1# 0 1.0 0# 1 0.0 0# Slicing is cooldf.loc[0:2]
# 0 1# 0 1.0 0.0# 1 0.0 0.0# If we explicitly specify its dtype, the type still not preserved.importnumpyasnpdf.astype(np.float32).dtypes# 0 Sparse[float32, 0.0]# 1 Sparse[float32, 0.0]df.astype(np.float32).loc[[1]].dtypes# 0 Sparse[float64, 0.0]# 1 Sparse[float64, 0.0]
Problem description
Index slice an all-zero sparse series by indexer (whether by bool array or index array) results in type int64. It should preserve type.
Code Sample, a copy-pastable example
Problem description
Index slice an all-zero sparse series by indexer (whether by bool array or index array) results in type int64. It should preserve type.
Expected Output
Output of
pd.show_versions()
The text was updated successfully, but these errors were encountered: