Skip to content

BUG: DataFrame[SparseArray] coerces to dense with .T #28477

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
scottgigante opened this issue Sep 17, 2019 · 1 comment
Closed

BUG: DataFrame[SparseArray] coerces to dense with .T #28477

scottgigante opened this issue Sep 17, 2019 · 1 comment

Comments

@scottgigante
Copy link
Contributor

Code Sample, a copy-pastable example if possible

import numpy as np
import pandas as pd
X = np.array([[0, 1, 0], [4, 0, 0]])
X = pd.DataFrame(X).astype(pd.SparseDtype(float, fill_value=0.0))
X.dtypes
X.T.dtypes

Problem description

Given the entire dataframe is sparse, it should probably remain sparse on transpose?

Current output:

>>> import numpy as np
>>> import pandas as pd
>>> X = np.array([[0, 1, 0], [4, 0, 0]])
>>> X = pd.DataFrame(X).astype(pd.SparseDtype(float, fill_value=0.0))
>>> X.dtypes
0    Sparse[float64, 0.0]
1    Sparse[float64, 0.0]
2    Sparse[float64, 0.0]
dtype: object
>>> X.T.dtypes
0    float64
1    float64
dtype: object

Expected Output

>>> import numpy as np
>>> import pandas as pd
>>> X = np.array([[0, 1, 0], [4, 0, 0]])
>>> X = pd.DataFrame(X).astype(pd.SparseDtype(float, fill_value=0.0))
>>> X.dtypes
0    Sparse[float64, 0.0]
1    Sparse[float64, 0.0]
2    Sparse[float64, 0.0]
dtype: object
>>> X.T.dtypes
0    Sparse[float64, 0.0]
1    Sparse[float64, 0.0]
dtype: object

Output of pd.show_versions()

INSTALLED VERSIONS
------------------
commit           : None
python           : 3.7.3.final.0
python-bits      : 64
OS               : Linux
OS-release       : 5.0.10-arch1-1-ARCH
machine          : x86_64
processor        : 
byteorder        : little
LC_ALL           : None
LANG             : en_US.UTF-8
LOCALE           : en_US.UTF-8

pandas           : 0.25.1
numpy            : 1.17.0
pytz             : 2019.2
dateutil         : 2.8.0
pip              : 19.2.3
setuptools       : 41.0.1
Cython           : None
pytest           : 5.1.2
hypothesis       : None
sphinx           : None
blosc            : None
feather          : None
xlsxwriter       : None
lxml.etree       : None
html5lib         : None
pymysql          : None
psycopg2         : None
jinja2           : 2.10.1
IPython          : None
pandas_datareader: None
bs4              : None
bottleneck       : None
fastparquet      : None
gcsfs            : None
lxml.etree       : None
matplotlib       : 3.1.1
numexpr          : 2.7.0
odfpy            : None
openpyxl         : None
pandas_gbq       : None
pyarrow          : None
pytables         : None
s3fs             : None
scipy            : 1.3.1
sqlalchemy       : None
tables           : 3.5.2
xarray           : None
xlrd             : None
xlwt             : None
xlsxwriter       : None
@TomAugspurger
Copy link
Contributor

Given the entire dataframe is sparse, it should probably remain sparse on transpose?

Yep. This is a duplicate of #22120.

Note however, that there isn't necessarily an efficient way to do this for Sparse. The sparsity is encoded column-wise, so I suspect you'll need to have an intermediate dense representation before returning to sparse.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants