BUG: concat with empty dataframe with columns passed and nonempty dataframe coerces dtype to object · Issue #45637 · pandas-dev/pandas (original) (raw)
Pandas version checks
- I have checked that this issue has not already been reported.
- I have confirmed this bug exists on the latest version of pandas.
- I have confirmed this bug exists on the main branch of pandas.
Reproducible Example
import pandas as pd
pd.concat([pd.DataFrame(columns=['foo', 'bar']), pd.DataFrame({'foo': [1.0, 2.0], 'bar': [1.0, 2.0]})]).dtypes
Out[3]: foo object bar object dtype: object
Issue Description
When using concat
with empty dataframe with columns
argument passed, and a nonempty dataframe, the dtypes of the resulting dataframe are coerced to object.
Expected Behavior
I would expect the dtypes to be taken from the nonempty dataframe, as was the behavior in previous versions of pandas.
This issue can be avoided if dtypes are explicitly passed, which maybe is intentional, but still it is unexpected.
Pandas 1.3.5 behavior:
In [120]: pd.concat([pd.DataFrame(columns=['foo', 'bar']), pd.DataFrame({'foo': [1.0, 2.0], 'bar': [1.0, 2.0]})]).dtypes
...:
Out[120]:
foo float64
bar float64
dtype: object
Installed Versions
INSTALLED VERSIONS
------------------
commit : bb1f651536508cdfef8550f93ace7849b00046ee
python : 3.8.12.final.0
python-bits : 64
OS : Linux
OS-release : 3.10.0-957.27.2.el7.x86_64
Version : #1 SMP Mon Jul 29 17:46:05 UTC 2019
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8
pandas : 1.4.0
numpy : 1.21.5
pytz : 2021.3
dateutil : 2.8.2
pip : 21.3.1
setuptools : 60.5.0
Cython : None
pytest : 5.4.3
hypothesis : None
sphinx : 4.4.0
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : 2.9.3
jinja2 : 2.11.3
IPython : 8.0.1
pandas_datareader: None
bs4 : None
bottleneck : None
fastparquet : None
fsspec : None
gcsfs : None
matplotlib : 3.4.2
numba : None
numexpr : 2.8.1
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : 1.7.3
sqlalchemy : 1.4.31
tables : 3.6.1
tabulate : None
xarray : 0.15.1
xlrd : None
xlwt : None
zstandard : None