Skip to content

DataFrame.at indexing with date string promotes column type, while indexing with datetime casts value #29709

Closed
@Jokab

Description

@Jokab

Code Sample, a copy-pastable example if possible

index = pd.date_range("2019-01-01", "2019-01-02", freq="D")
df = pd.DataFrame(index=index, columns=["A"]).fillna(1)

>>> df.at[pd.to_datetime('2019-01-01'), 'A'] = 0.98
>>> df
            A
2019-01-01  0  # cast to column dtype
2019-01-02  1
>>> df['A'].dtype
dtype('int64')

>>> df.at['2019-01-01', 'A'] = 0.98
>>> df
               A
2019-01-01  0.98  # column cast to value dtype
2019-01-02  1.00
>>> df['A'].dtype
dtype('float64')

Problem description

This issue arises when dealing with a Dataframe with a datetime index. When using DataFrame.at[] and indexing using a date string, the column dtype is promoted to the type of the provided value. However when using DataFrame.at[] and indexing using a datetime, the provided value is instead cast to the column's dtype. In the example, 0.98 is in this case indeed cast to 0.

Expected Output

That indexing DataFrame.at[] with either a date string or a datetime works the same.

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.6.8.final.0
python-bits : 64
OS : Darwin
OS-release : 18.2.0
machine : x86_64
processor : i386
byteorder : little
LC_ALL : en_US.UTF-8
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8

pandas : 0.25.3
numpy : 1.16.1
pytz : 2019.3
dateutil : 2.8.0
pip : 19.3.1
setuptools : 41.6.0
Cython : None
pytest : 4.3.0
hypothesis : 4.7.11
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : 2.7.7 (dt dec pq3 ext lo64)
jinja2 : 2.10.3
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 : 1.3.1
sqlalchemy : None
tables : None
xarray : None
xlrd : None
xlwt : None
xlsxwriter : None

Metadata

Metadata

Assignees

No one assigned

    Labels

    DatetimeDatetime data dtypeDtype 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