Skip to content

Bug: loc assignment results in wrong dtype #31340

Closed
@krasnikov138

Description

@krasnikov138

Let's see output of this code snippet

df = pd.DataFrame({'id': ['A'], 'a': [1.2], 'b': [0.0], 'c': [-2.5]})
cols = ['a', 'b', 'c']
df.loc[:, cols] = df.loc[:, cols].astype('float32')
print(df.dtypes)

Problem description

In this code we expect columns 'a', 'b', 'c' will have types float32. But in fact the result is:

>>> print(df.dtypes)
id     object
a     float32
b       int64
c     float32
dtype: object

Bug occurs when the value in column is close to integer. In other words, for df = pd.DataFrame({'id': ['A'], 'a': [1.2], 'b': [1.0], 'c': [-2.5]}) behavior will be the same. However, if df has 2 rows or more the result is right:

>>> df = pd.DataFrame({'id': ['A', 'B'], 'a': [1.2, 0.55], 'b': [0.0, 0.0], 'c': [-2.5, 12.56]})
>>> df.loc[:, cols] = df.loc[:, cols].astype('float32')
>>> df.dtypes
id     object
a     float32
b     float32
c     float32
dtype: object

Output of pd.show_versions()

INSTALLED VERSIONS ------------------ commit : None python : 3.6.5.final.0 python-bits : 64 OS : Linux OS-release : 4.19.6-200.fc28.x86_64 machine : x86_64 processor : x86_64 byteorder : little LC_ALL : None LANG : en_US.UTF-8 LOCALE : en_US.UTF-8

pandas : 0.25.3
numpy : 1.17.4
pytz : 2018.4
dateutil : 2.7.5
pip : 19.2.3
setuptools : 41.6.0
Cython : 0.28.2
pytest : 3.5.1
hypothesis : None
sphinx : 1.7.4
blosc : None
feather : None
xlsxwriter : 1.0.4
lxml.etree : 4.2.1
html5lib : 1.0.1
pymysql : None
psycopg2 : 2.7.5 (dt dec pq3 ext lo64)
jinja2 : 2.10
IPython : 6.4.0
pandas_datareader: None
bs4 : 4.6.3
bottleneck : 1.2.1
fastparquet : None
gcsfs : None
lxml.etree : 4.2.1
matplotlib : 3.0.1
numexpr : 2.6.5
odfpy : None
openpyxl : 2.5.3
pandas_gbq : None
pyarrow : None
pytables : None
s3fs : None
scipy : 1.1.0
sqlalchemy : 1.2.14
tables : 3.4.3
xarray : None
xlrd : 1.1.0
xlwt : 1.3.0
xlsxwriter : 1.0.4

Metadata

Metadata

Assignees

No one assigned

    Labels

    Dtype ConversionsUnexpected or buggy dtype conversionsIndexingRelated to indexing on series/frames, not to indexes themselves

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions