BUG/ENH: Bad columns dtype when creating empty DataFrame · Issue #22858 · pandas-dev/pandas (original) (raw)

Code Sample

df = pd.DataFrame(columns=list('ABC'), dtype='int64') df Empty DataFrame Columns: [A, B, C] Index: [] df.dtypes A float64 B float64 C float64 dtype: object

Problem description

When creating a DataFrame with no rows, the presence of a dtype argument may convert the columns into float64. The problem does not happen if the DataFrame has one or more rows:

df = pd.DataFrame([[1, 2, 3]], columns=list('ABC'), dtype='int64') df A B C 0 1 2 3 df.dtypes A int64 B int64 C int64 dtype: object

Expected Output

df = pd.DataFrame(columns=list('ABC'), dtype='int64') df.dtypes A int64 B int64 C int64 dtype: object

Output of pd.show_versions()

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

pandas: 0.23.4
pytest: 3.8.0
pip: 10.0.1
setuptools: 40.2.0
Cython: 0.28.5
numpy: 1.15.1
scipy: 1.1.0
pyarrow: 0.9.0
xarray: 0.10.8
IPython: 6.5.0
sphinx: 1.7.9
patsy: 0.5.0
dateutil: 2.7.3
pytz: 2018.5
blosc: None
bottleneck: 1.2.1
tables: 3.4.4
numexpr: 2.6.8
feather: 0.4.0
matplotlib: 2.2.3
openpyxl: 2.5.5
xlrd: 1.1.0
xlwt: 1.3.0
xlsxwriter: 1.1.0
lxml: 4.2.5
bs4: 4.6.3
html5lib: 1.0.1
sqlalchemy: 1.2.11
pymysql: 0.9.2
psycopg2: None
jinja2: 2.10
s3fs: 0.1.6
fastparquet: 0.1.6
pandas_gbq: None
pandas_datareader: None