pysolar icon indicating copy to clipboard operation
pysolar copied to clipboard

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

Open mborsetti opened this issue 4 years ago • 3 comments

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)

mborsetti avatar Oct 18 '20 10:10 mborsetti

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

pingswept avatar Oct 18 '20 14:10 pingswept

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!

adRn-s avatar Aug 24 '22 09:08 adRn-s

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: https://github.com/numpy/numpy/issues/23904

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

anoduck avatar Aug 02 '24 05:08 anoduck