Skip to content
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

Incorrect conversion from Python datetime to np.datetime64 when timezone specified. #5761

Closed
aisthesis opened this issue Apr 10, 2015 · 1 comment
Labels

Comments

@aisthesis
Copy link
Contributor

Here's the behavior I'm seeing, both on Ubuntu 14.04 and on OS X Mavericks using Python 2.7.8:

>>> from pytz import timezone
>>> import numpy as np
>>> np.datetime64(dt.datetime(2015, 4, 3, 16, tzinfo=timezone('US/Eastern')))
numpy.datetime64('2015-04-03T13:56:00.000000-0700')

It's getting the offset right but for some reason off by 56 minutes. This could also be an issue in pytz but numpy seems more plausible. When I use naive timezone (tzinfo=None implicitly), I get:

>>> np.datetime64(dt.datetime(2015, 4, 3, 16))
numpy.datetime64('2015-04-03T09:00:00.000000-0700')

So, correct conversion if we interpret tzinfo=None to be UTC, and my machine is on PDT, which is presumably why in both cases we are seeing -0700 for timezone.

@mattip mattip added the component: numpy.datetime64 (and timedelta64) label Sep 4, 2018
@bsipocz
Copy link
Member

bsipocz commented Oct 15, 2019

I suggest to close this issue, as parsing timezone aware datetimes has been deprecated a while now (1.11), and should be subject to removal.

>>> from pytz import timezone 
>>> import datetime as dt
>>> import numpy as np 
>>> np.datetime64(dt.datetime(2015, 4, 3, 16, tzinfo=timezone('US/Eastern')))                                   
/usr/local/bin/ipython:3: DeprecationWarning: parsing timezone aware datetimes is deprecated; this will raise an error in the future
  import re
numpy.datetime64('2015-04-03T20:56:00.000000')

(I'm not sure why the import re is part of the warning though.)

Also, for the sake of the exercise, there is weird 4 minutes difference when using datetime with ptx, so even the original issue seems to be theirs rather than numpy's. And the pytz docs state that there are exactly these kind of issues when using it for datetime's tzinfo (http://pytz.sourceforge.net/#localized-times-and-date-arithmetic).

In [6]: dt.datetime(2015, 4, 3, 16, tzinfo=timezone('US/Eastern'))                                                      
Out[6]: datetime.datetime(2015, 4, 3, 16, 0, tzinfo=<DstTzInfo 'US/Eastern' LMT-1 day, 19:04:00 STD>)

@bsipocz bsipocz closed this as completed Oct 15, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants