ENH: sparse astype now supports int64 and bool by sinhrks · Pull Request #13900 · pandas-dev/pandas (original) (raw)
SparseXXX now support astype other than float64 dtype. It raises ValueError when its sp_values or fill_value cannot be converted to specified dtype.
s = pd.SparseSeries([1., 0., 2., 0.], fill_value=0)
s.astype(np.int64)
#0 1
#1 0
#2 2
#3 0
# dtype: int64
# BlockIndex
# Block locations: array([0, 2], dtype=int32)
# Block lengths: array([1, 1], dtype=int32)
pd.SparseSeries([1, np.nan, 2, np.nan]).astype(np.int64)
# ValueError: unable to coerce current fill_value nan to int64 dtype
Also, fill_value setter now checks input value dtype and raises ValueError if value cannot be stored to current dtype.
s = pd.SparseSeries([1, 0, 2, 0], fill_value=0, dtype=np.int64)
s.fill_value = 2
s
#0 1
#1 2
#2 2
#3 2
# dtype: int64
# BlockIndex
# Block locations: array([0, 2], dtype=int32)
# Block lengths: array([1, 1], dtype=int32)
s.fill_value = np.nan
# ValueError: unable to set fill_value nan to int64 dtype