Skip to content

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

Closed
@henryeck

Description

@henryeck

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugDtype 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