Description
Code Sample
import pandas as pd
typ = pd.DatetimeTZDtype(tz = 'EST')
s = pd.Series([pd.Timestamp('2020-01-01 15:00')])
print(s.astype(typ))
0 2020-01-01 10:00:00-05:00
dtype: datetime64[ns, EST]
Problem description
Hello,
I noticed a strange behavior using DatetimeTZDtype
, when specifying a timezone as argument. When the type is applied to a tz-naive datetime column using astype
, it will first localize the timestamps to UTC
and then convert the timezone, whereas I was expecting it would just localize my tz-naive timestamps.
My situation is I am getting data from a database and need to convert the columns to the desired types. Using DatetimeTZDtype
and astype
is a nice way to convert and localize in just one line, but I encountered this unexpected behavior (though it is easy to work around that).
I tracked the issue to these lines https://github.com/pandas-dev/pandas/blob/master/pandas/core/internals/blocks.py#L2090
Thanks
Expected Output
0 2020-01-01 15:00:00-05:00
dtype: datetime64[ns, EST]
Output of pd.show_versions()
INSTALLED VERSIONS
commit : None
python : 3.7.6.final.0
python-bits : 64
OS : Windows
OS-release : 10
machine : AMD64
processor : Intel64 Family 6 Model 158 Stepping 13, GenuineIntel
byteorder : little
LC_ALL : None
LANG : None
LOCALE : None.None
pandas : 1.0.3
numpy : 1.18.2
pytz : 2019.3
dateutil : 2.8.1
pip : 20.0.2
setuptools : 45.2.0
Cython : 0.29.16
pytest : 5.4.1
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : 0.9.3
psycopg2 : None
jinja2 : 2.11.1
IPython : 7.13.0
pandas_datareader: None
bs4 : None
bottleneck : None
fastparquet : None
gcsfs : None
lxml.etree : None
matplotlib : 3.2.1
numexpr : 2.7.1
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : 0.16.0
pytables : None
pytest : 5.4.1
pyxlsb : None
s3fs : None
scipy : 1.4.1
sqlalchemy : 1.3.15
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
xlsxwriter : None
numba : 0.48.0
[paste the output of pd.show_versions()
here leaving a blank line after the details tag]