Skip to content

DOC: DatetimeTZDtype astype behavior with Series.astype #33399

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
Loupiol opened this issue Apr 8, 2020 · 12 comments · Fixed by #33470
Closed

DOC: DatetimeTZDtype astype behavior with Series.astype #33399

Loupiol opened this issue Apr 8, 2020 · 12 comments · Fixed by #33470
Assignees
Labels

Comments

@Loupiol
Copy link

Loupiol commented Apr 8, 2020

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]

@Loupiol Loupiol added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Apr 8, 2020
@TomAugspurger
Copy link
Contributor

Thanks for the bug report. Your expected output matches the result for DatetimeIndex.astype() with the same values

In [10]: pd.Index([pd.Timestamp('2020-01-01 15:00')]).astype('datetime64[ns, EST]')
Out[10]: DatetimeIndex(['2020-01-01 15:00:00-05:00'], dtype='datetime64[ns, EST]', freq=None)

cc @mroeschke and @jbrockmendel if you have thoughts on which of these is correct.

@TomAugspurger TomAugspurger added Datetime Datetime data dtype Timezones Timezone data dtype and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Apr 8, 2020
@jbrockmendel
Copy link
Member

So first off, as a matter of heading off future headaches, you should be using "US/Eastern" instead of "EST".

I would expect the data stored in the database to be UTC times (or unix timestamps). is that correct? if not, how are they stored?

@mroeschke
Copy link
Member

This behavior is documented in https://pandas.pydata.org/docs/user_guide/timeseries.html#time-zone-series-operations

In [458]: s_naive = pd.Series(pd.date_range('20130101', periods=3))

In [459]: s_naive
Out[459]: 
0   2013-01-01
1   2013-01-02
2   2013-01-03
dtype: datetime64[ns]

# localize and convert a naive time zone
In [463]: s_naive.astype('datetime64[ns, US/Eastern]')
Out[463]: 
0   2012-12-31 19:00:00-05:00
1   2013-01-01 19:00:00-05:00
2   2013-01-02 19:00:00-05:00
dtype: datetime64[ns, US/Eastern]

I think the comment can be made more explicit as # localize to UTC and convert to US/Eastern. Think that small doc update would be worthwhile

@mroeschke
Copy link
Member

And the astype documentation could be updated to reflect this behavior too. https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.Series.astype.html

@mroeschke mroeschke added Docs good first issue and removed Bug Datetime Datetime data dtype labels Apr 8, 2020
@Loupiol
Copy link
Author

Loupiol commented Apr 8, 2020

If that's the expected behavior, I'll make it work with that then. A quick example in astype showing this would indeed avoid any future confusion (that's the first doc I checked).

However what's the rationale behind having a different behavior for Series and DatetimeIndex as @TomAugspurger showed?

@TomAugspurger
Copy link
Contributor

TomAugspurger commented Apr 8, 2020 via email

@jbrockmendel
Copy link
Member

However what's the rationale behind having a different behavior for Series and DatetimeIndex

That definitely seems undesirable

@mroeschke
Copy link
Member

Created #33401 for that discrepancy

@mroeschke mroeschke changed the title DatetimeTZDtype astype behavior DOC: DatetimeTZDtype astype behavior with Series.astype Apr 8, 2020
@venkateshdatta1993
Copy link
Contributor

Hey @mroeschke can I pick this up?

If I understand correctly, changes need to be made in two pages. 1. The astype page and 2. The timezone series operations page.

@mroeschke
Copy link
Member

Sure @venkateshdatta1993. Just this page needs an updated example with using a timezone data type with astype https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.Series.astype.html

@venkateshdatta1993
Copy link
Contributor

take

@venkateshdatta1993
Copy link
Contributor

@mroeschke Awesome, I’ll work on it. Thanks for the clarification.

venkateshdatta1993 pushed a commit to venkateshdatta1993/pandas that referenced this issue Apr 10, 2020
venkateshdatta1993 pushed a commit to venkateshdatta1993/pandas that referenced this issue Apr 11, 2020
mroeschke pushed a commit that referenced this issue Apr 12, 2020
…3399 (#33470)

* DOC: timezone conversion example added to pandas.Series.astype doc #33399

* DOC: fixed the whitespace issue flagged in pep8 checks #33399

* DOC: added text in comments to commentary in example #333999

* DOC: added a more generic behavior statement as suggested. #33399

* DOC: removed unwanted commentary statement as per suggestion #33399

Co-authored-by: Venkatesh Datta <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants