BUG: convert_dtypes throws a ValueError when called on an empty DataFrame · Issue #40393 · pandas-dev/pandas (original) (raw)


Note: Please read this guide detailing how to provide the necessary information for us to reproduce your bug.

Code Sample, a copy-pastable example

Your code here

import pandas as pd

pd.DataFrame().convert_dtypes()

Problem description

convert_dtypes throws a "ValueError: No objects to concatenate" exception when called on an empty DataFrame. I find that a problem because I have use cases of chaining up DataFrame transform calls fluently that may result in an empty DataFrame before the convert_dtypes call. I don't want want to break up the transform calls to check for an empty DataFrame or handle the ValueError.

Expected Output

I expect calling convert_dtypes on an empty DataFrame should return an empty DataFrame without error.

import pandas as pd

df = pd.DataFrame().convert_dtypes() assert df.empty

Output of pd.show_versions()

INSTALLED VERSIONS

commit : f2c8480
python : 3.8.8.final.0
python-bits : 64
OS : Darwin
OS-release : 20.3.0
Version : Darwin Kernel Version 20.3.0: Thu Jan 21 00:07:06 PST 2021; root:xnu-7195.81.3~1/RELEASE_X86_64
machine : x86_64
processor : i386
byteorder : little
LC_ALL : None
LANG : en_AU.UTF-8
LOCALE : en_AU.UTF-8

pandas : 1.2.3
numpy : 1.20.1
pytz : 2020.1
dateutil : 2.8.1
pip : 21.0.1
setuptools : 49.6.0.post20200917
Cython : None
pytest : 6.0.2
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : 1.3.7
lxml.etree : 4.6.2
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 2.11.2
IPython : 7.21.0
pandas_datareader: None
bs4 : 4.9.3
bottleneck : 1.3.2
fsspec : 0.8.2
fastparquet : 0.5.0
gcsfs : None
matplotlib : 3.3.4
numexpr : 2.7.3
odfpy : None
openpyxl : 3.0.7
pandas_gbq : None
pyarrow : 3.0.0
pyxlsb : None
s3fs : 0.2.2
scipy : 1.5.3
sqlalchemy : 1.3.23
tables : 3.6.1
tabulate : 0.8.7
xarray : 0.16.1
xlrd : 2.0.1
xlwt : None
numba : 0.51.2