Skip to content
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

.astype(SparseDtype(float)) on empty dataframe leads to "ValueError: No objects to concatenate" #33113

Closed
tgy opened this issue Mar 29, 2020 · 5 comments · Fixed by #33118
Closed
Labels
ExtensionArray Extending pandas with custom dtypes or arrays. Reshaping Concat, Merge/Join, Stack/Unstack, Explode Sparse Sparse Data Type
Milestone

Comments

@tgy
Copy link
Contributor

tgy commented Mar 29, 2020

Code Sample, a copy-pastable example if possible

import pandas as pd
pd.DataFrame().astype(pd.SparseDtype(float))

Problem description

Converting an empty dataframe to a sparse representation leads to a ValueError.

ValueError: No objects to concatenate

Expected Output

I expected this to work even though the dataframe is empty.

Output of pd.show_versions()

>>> pandas.show_versions()

INSTALLED VERSIONS
------------------
commit           : None
python           : 3.7.4.final.0
python-bits      : 64
OS               : Darwin
OS-release       : 19.0.0
machine          : x86_64
processor        : i386
byteorder        : little
LC_ALL           : en_US.UTF-8
LANG             : en_US.UTF-8
LOCALE           : en_US.UTF-8

pandas           : 1.0.3
numpy            : 1.18.2
pytz             : 2019.3
dateutil         : 2.8.1
pip              : 19.3.1
setuptools       : 45.0.0
Cython           : None
pytest           : 5.2.2
hypothesis       : None
sphinx           : None
blosc            : None
feather          : None
xlsxwriter       : None
lxml.etree       : 4.4.2
html5lib         : None
pymysql          : None
psycopg2         : None
jinja2           : 2.10.3
IPython          : 7.11.1
pandas_datareader: None
bs4              : None
bottleneck       : None
fastparquet      : None
gcsfs            : None
lxml.etree       : 4.4.2
matplotlib       : 3.1.2
numexpr          : 2.7.0
odfpy            : None
openpyxl         : None
pandas_gbq       : None
pyarrow          : None
pytables         : None
pytest           : 5.2.2
pyxlsb           : None
s3fs             : None
scipy            : 1.4.1
sqlalchemy       : None
tables           : 3.6.0
tabulate         : None
xarray           : None
xlrd             : 1.2.0
xlwt             : None
xlsxwriter       : None
numba            : 0.46.0
@jreback jreback added Reshaping Concat, Merge/Join, Stack/Unstack, Explode Sparse Sparse Data Type labels Mar 29, 2020
@simonjayhawkins
Copy link
Member

Thanks @tgy for the report. The same exception is raised for Int64Dtype, StringDtype and BooleanDtype and maybe more.

@simonjayhawkins simonjayhawkins added ExtensionArray Extending pandas with custom dtypes or arrays. and removed Sparse Sparse Data Type labels Mar 29, 2020
@tgy
Copy link
Contributor Author

tgy commented Mar 29, 2020

@simonjayhawkins yep, i used float as an example but i think it fails for all the types because the code clearly does the same thing for all these types. see #33118

@KardoPaska
Copy link

KardoPaska commented Jun 19, 2020

My motivation is to make one line of code robust to "blank" inputs. And I encountered the same kind of error for slightly different input...

summarys = [{'testid': 'abc', 'avg': 1.2, 'count': 5, 'ok': True}, # Pass
            {'testid': 'xyz',             'count': 0, 'ok': False},# Pass
                                                           dict()] # Fail (empty)
summary_dtypes = {'testid': str, 'avg': float, 'count': pd.Int64Dtype(), 'ok': bool}
actual_cols_set = {kk for x in summarys for kk in x.keys()}
actual_dtypes = {k: v for k,v in summary_dtypes.items() if k in actual_cols_set}

This is all good (aside from "missing" bool being True)...

>>> pd.DataFrame(summarys).astype(dtype=actual_dtypes)

  testid  avg  count     ok
0    abc  1.2      5   True
1    xyz  NaN      0  False
2    NaN  NaN   <NA>   True

this is also fine...

>>> pd.DataFrame([dict(), dict()])

Empty DataFrame
Columns: []
Index: [0, 1]

... but... for a list of empty dict(), my actual_dtypes will also be an empty dict()...

>>> pd.DataFrame([dict(), dict()]).astype(dtype=dict())

ValueError: No objects to concatenate

So perhaps a short circuit for empty dataframes? (instead of raising error?)

@tgy
Copy link
Contributor Author

tgy commented Jun 21, 2020

@KardoPaska My small change on #33118 seems to fix your issue as well:

>>> import pandas as pd
>>> pd.DataFrame([dict(), dict()]).astype(dtype=dict())
Empty DataFrame
Columns: []
Index: [0, 1]

Waiting for @jreback to approve the change on the PR

@tgy
Copy link
Contributor Author

tgy commented Jun 21, 2020

Shouldn't the code be pd.DataFrame([dict(), dict()]).astype(dtype=dict) instead? (dict instead of dict() in the dtype)? That works on master.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ExtensionArray Extending pandas with custom dtypes or arrays. Reshaping Concat, Merge/Join, Stack/Unstack, Explode Sparse Sparse Data Type
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants