Skip to content

SparseDataFrame empty slice coerces to loses dtype and fill_value #21993

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
TomAugspurger opened this issue Jul 20, 2018 · 2 comments · Fixed by #28425
Closed

SparseDataFrame empty slice coerces to loses dtype and fill_value #21993

TomAugspurger opened this issue Jul 20, 2018 · 2 comments · Fixed by #28425
Labels
Dtype Conversions Unexpected or buggy dtype conversions Indexing Related to indexing on series/frames, not to indexes themselves Sparse Sparse Data Type

Comments

@TomAugspurger
Copy link
Contributor

import pandas as pd
import numpy as np

In [59]: df = pd.SparseDataFrame({"A": pd.SparseSeries(np.array([1, 1, 0], dtype='uint8'), fill_value=0), "B": pd.SparseSeries(np.array([1, 1, 0], dtype='uint8'), fill_value=0)})

In [60]: df.dtypes
Out[60]:
A    uint8
B    uint8
dtype: object

In [61]: df.A.fill_value
Out[61]: 0

In [62]: df.iloc[0:0].dtypes
Out[62]:
A    float64
B    float64
dtype: object

In [63]: df.iloc[0:0].A.fill_value
Out[63]: nan

non-empty slices are fine.

@TomAugspurger TomAugspurger added Indexing Related to indexing on series/frames, not to indexes themselves Dtype Conversions Unexpected or buggy dtype conversions Sparse Sparse Data Type labels Jul 20, 2018
@TomAugspurger
Copy link
Contributor Author

Although... a DataFrame of sparse series handles this fine, so maybe we just ignore this?

@jorisvandenbossche
Copy link
Member

This is no longer a problem with the new DataFrame[sparse]:

In [39]: df = pd.DataFrame({"A": pd.SparseArray(np.array([1, 1, 0], dtype='uint8'), fill_value=0), "B": pd.SparseArray(np.array([1, 1, 0], dtype='uint8'), fill_value=0)})                                         

In [40]: df                                                                                                                                                                                                        
Out[40]: 
   A  B
0  1  1
1  1  1
2  0  0

In [41]: df.dtypes                                                                                                                                                                                                 
Out[41]: 
A    Sparse[uint8, 0]
B    Sparse[uint8, 0]
dtype: object

In [42]: df.iloc[0:0].dtypes                                                                                                                                                                                       
Out[42]: 
A    Sparse[uint8, 0]
B    Sparse[uint8, 0]
dtype: object

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Dtype Conversions Unexpected or buggy dtype conversions Indexing Related to indexing on series/frames, not to indexes themselves Sparse Sparse Data Type
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants