Skip to content

Inconsistent dtype changes between multi-column assignment and single-column assignment #27583

Closed
@howsiwei

Description

@howsiwei

Code Sample, a copy-pastable example if possible

import pandas as pd

df = pd.DataFrame({'a': [0.0], 'b': 0.0})

df[['a', 'b']] = 0
print(df)
print()

df['b'] = 0
print(df)

Output:

     a    b
0  0.0  0.0

     a  b
0  0.0  0

Problem description

As you can see above, the column type doesn't change in multi-column assignment but changes from float64 to int64 in single-column assignment. This behavior seems quite inconsistent.

Expected Output

   a  b
0  0  0

   a  b
0  0  0

Both multi-column assignment and single-column assignment should result in the correct dtype.

Output of pd.show_versions()

INSTALLED VERSIONS

commit : 3b96ada
python : 3.6.8.final.0
python-bits : 64
OS : Linux
OS-release : 4.15.0-52-generic
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : en_US.utf8
LOCALE : en_US.UTF-8

pandas : 0.25.0rc0+131.g3b96ada3a
numpy : 1.16.3
pytz : 2019.1
dateutil : 2.8.0
pip : 19.1.1
setuptools : 41.0.1
Cython : 0.29.7
pytest : 4.4.2
hypothesis : 4.17.2
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : None
IPython : None
pandas_datareader: None
bs4 : None
bottleneck : None
fastparquet : None
gcsfs : None
lxml.etree : None
matplotlib : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pytables : None
s3fs : None
scipy : None
sqlalchemy : None
tables : None
xarray : None
xlrd : None
xlwt : None
xlsxwriter : None

Metadata

Metadata

Labels

Dtype ConversionsUnexpected or buggy dtype conversionsIndexingRelated to indexing on series/frames, not to indexes themselvesNeeds TestsUnit test(s) needed to prevent regressionsgood first issue

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions