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

Numpy DeprecationWarning: parsing timezone aware datetimes is deprecated; this will raise an error in the future #135

Open
mborsetti opened this issue Oct 18, 2020 · 3 comments

Comments

@mborsetti
Copy link
Contributor

When running tests, I am getting the following warning from numpy (current version):

[...]\pysolar\numeric.py:62: DeprecationWarning: parsing timezone aware datetimes is deprecated; this will raise an error in the future
dd = numpy.array(d, dtype='datetime64[D]')
[...]\pysolar\numeric.py:63: DeprecationWarning: parsing timezone aware datetimes is deprecated; this will raise an error in the future
dy = numpy.array(d, dtype='datetime64[Y]')
[...]\pysolar\numeric.py:74: DeprecationWarning: parsing timezone aware datetimes is deprecated; this will raise an error in the future
dh = numpy.array(d, dtype='datetime64[h]')
[...]\pysolar\numeric.py:75: DeprecationWarning: parsing timezone aware datetimes is deprecated; this will raise an error in the future
dd = numpy.array(d, dtype='datetime64[D]')
[...]\pysolar\numeric.py:86: DeprecationWarning: parsing timezone aware datetimes is deprecated; this will raise an error in the future
dm = numpy.array(d, dtype='datetime64[m]')
[...]\pysolar\numeric.py:87: DeprecationWarning: parsing timezone aware datetimes is deprecated; this will raise an error in the future
dh = numpy.array(d, dtype='datetime64[h]')

Per developer's manual at https://numpy.org/devdocs/reference/arrays.datetime.html#basic-datetimes

Deprecated since version 1.11.0: NumPy does not store timezone information. For backwards compatibility, datetime64 still parses timezone offsets, which it handles by converting to UTC. This behaviour is deprecated and will raise an error in the future.

The code below takes a timezone-aware datetime, converts it to UTC, and turns it into a timezone unaware one that can be passed to numpy. However, I am not sure the full functioning of the code and where it would need to be used in order not to introduce undesirable side effects (bugs).

d = d.astimezone(tz=datetime.timezone.utc).replace(tzinfo=None)

@pingswept
Copy link
Owner

Yes, this needs fixing, if anyone has the time to sort out the details. Patches welcome, as always.

w00kie added a commit to w00kie/pysolar that referenced this issue Jan 26, 2021
Make datetime input timezone unaware before feeding them to numpy.
Fixes pingswept#135
@adRn-s
Copy link

adRn-s commented Aug 24, 2022

Hi, I've just came across this issue because I encountered the same problem. I'd like to suggest you to try using pandas.to_datetime, this datetime object would then be tz aware. I'm about to do that for my app... Good luck!

@anoduck
Copy link

anoduck commented Aug 2, 2024

Confirming all of the above, as numerous lines of the above error message are generated, which is quite undesirable.

Referencing, an issue that reviewed numpy's decision to allow this error message to persist: numpy/numpy#23904

Lastly, confirming the comment made by @adRn-s as potentially the best approach towards resolution. Which is mentioned in several other sources.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants