Skip to content

BUG:Timezone lost when assigning Datetime via DataFrame.at #33544

@torfsen

Description

@torfsen
  • I have checked that this issue has not already been reported.

  • I have confirmed this bug exists on the latest version of pandas.

  • (optional) I have confirmed this bug exists on the master branch of pandas.


Note: Please read this guide detailing how to provide the necessary information for us to reproduce your bug.

Code Sample, a copy-pastable example

import datetime as dt
import pandas as pd

df = pd.DataFrame({'foo': [dt.datetime(2000, 1, 1)]})
df.at[0, 'foo'] = dt.datetime(2000, 1, 2, tzinfo=dt.timezone.utc)
print(df.at[0, 'foo'])  # Prints 2000-01-02 00:00:00
assert df.at[0, 'foo'].tzinfo is not None  # Fails

Problem description

When assigning a tz-aware datetime via at I expect that the tz-information is kept. If that is not possible due to a datatype problem then I expect a warning or error message. Instead it is lost without a warning or error message.

Expected Output

I would have expected that the tz-information is kept, i.e. that

assert df.at[0, 'foo'].tzinfo is not None  # Should not fail

Output of pd.show_versions()

Details

INSTALLED VERSIONS

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

pandas : 1.0.3
numpy : 1.18.1
pytz : 2019.3
dateutil : 2.8.1
pip : 20.0.2
setuptools : 46.1.3.post20200330
Cython : None
pytest : 5.4.1
hypothesis : None
sphinx : 2.4.4
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : 2.8.4 (dt dec pq3 ext lo64)
jinja2 : 2.11.1
IPython : 7.13.0
pandas_datareader: None
bs4 : 4.8.2
bottleneck : None
fastparquet : None
gcsfs : None
lxml.etree : None
matplotlib : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pytables : None
pytest : 5.4.1
pyxlsb : None
s3fs : None
scipy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
xlsxwriter : None
numba : None

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions