Skip to content

different type conversion results in assignments using DataFrame.loc and DataFrame.at #26395

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
henryeck opened this issue May 14, 2019 · 1 comment · Fixed by #45154
Closed
Labels
Bug Dtype Conversions Unexpected or buggy dtype conversions Indexing Related to indexing on series/frames, not to indexes themselves
Milestone

Comments

@henryeck
Copy link

henryeck commented May 14, 2019

Code Sample, a copy-pastable example if possible

# Python 3.5.6 |Anaconda custom (64-bit)| (default, Aug 26 2018, 21:41:56)
# assignment behavior with DataFrame.loc and DataFrame.at
# df.loc[] = x will promote dataframe column type to x's type
# df.at[] = x will cast x value to column's type
# apparently undocumented.

import pandas as pd

df = pd.DataFrame(index=['A','B','C'])
df['D'] = 0
df.at['C','D'] = 2
df
#       D
#    A  0
#    B  0
#    C  2

df.dtypes
#    D    int64
#    dtype: object

df.at['C','D'] = 44.5
df
#        D
#    A   0
#    B   0
#    C  44

df.dtypes
#    D    int64
#    dtype: object

df.loc['C','D'] = 44.5
df
#          D
#    A   0.0
#    B   0.0
#    C  44.5

df.dtypes
#    D    float64
#    dtype: object

df.loc['C','D'] = 'hello'
df
#           D
#    A      0
#    B      0
#    C  hello

df.dtypes
#    D    object
#    dtype: object

Problem description

the type-conversion/cast behavior is not documented for either .loc or .at.

Expected Output

expect same behavior. also expect a warning/error if casting from float to int

Output of pd.show_versions()

[paste the output of pd.show_versions() here below this line]
pd.show_versions()

INSTALLED VERSIONS

commit: None
python: 3.5.6.final.0
python-bits: 64
OS: Linux
OS-release: 4.15.0-48-generic
machine: x86_64
processor: x86_64
byteorder: little
LC_ALL: None
LANG: en_US.UTF-8
LOCALE: en_US.UTF-8

pandas: 0.22.0
pytest: 3.7.3
pip: 18.0
setuptools: 40.2.0
Cython: 0.28.5
numpy: 1.14.5
scipy: 1.1.0
pyarrow: None
xarray: None
IPython: 6.5.0
sphinx: 1.7.7
patsy: 0.5.0
dateutil: 2.7.3
pytz: 2018.5
blosc: None
bottleneck: 1.2.1
tables: 3.4.4
numexpr: 2.6.1
feather: None
matplotlib: 2.2.3
openpyxl: 2.5.5
xlrd: 1.1.0
xlwt: 1.3.0
xlsxwriter: 1.0.8
lxml: 4.2.4
bs4: 4.6.3
html5lib: 1.0.1
sqlalchemy: 1.2.11
pymysql: None
psycopg2: None
jinja2: 2.10
s3fs: None
fastparquet: None
pandas_gbq: None
pandas_datareader: 0.6.0

@TomAugspurger
Copy link
Contributor

It looks like IndexEngine.set_value, or before we get there, may need to cast to the result type (float) before setting. Further investigation / a fix would be welcome.

expect same behavior. also expect a warning/error if casting from float to int

This would be inconsistent with the rest of pandas. I don't think we would want that.

@TomAugspurger TomAugspurger added Dtype Conversions Unexpected or buggy dtype conversions Indexing Related to indexing on series/frames, not to indexes themselves labels May 14, 2019
@mroeschke mroeschke added the Bug label Jul 2, 2021
@jreback jreback added this to the 1.4 milestone Jan 3, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Dtype Conversions Unexpected or buggy dtype conversions Indexing Related to indexing on series/frames, not to indexes themselves
Projects
None yet
4 participants