-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
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
Comments
Thanks for the bug report. Your expected output matches the result for 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. |
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? |
This behavior is documented in https://pandas.pydata.org/docs/user_guide/timeseries.html#time-zone-series-operations
I think the comment can be made more explicit as |
And the |
If that's the expected behavior, I'll make it work with that then. A quick example in However what's the rationale behind having a different behavior for |
I don’t think that’s deliberate.
… On Apr 8, 2020, at 11:54, Loupiol ***@***.***> wrote:
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?
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub, or unsubscribe.
|
That definitely seems undesirable |
Created #33401 for that discrepancy |
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. |
Sure @venkateshdatta1993. Just this page needs an updated example with using a timezone data type with |
take |
@mroeschke Awesome, I’ll work on it. Thanks for the clarification. |
…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]>
Code Sample
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 usingastype
, it will first localize the timestamps toUTC
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
andastype
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
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]The text was updated successfully, but these errors were encountered: