pingswept / pysolar

Pysolar is a collection of Python libraries for simulating the irradiation of any point on earth by the sun. It includes code for extremely precise ephemeris calculations.
http://pysolar.org
GNU General Public License v3.0
372 stars 125 forks source link

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

Open mborsetti opened 3 years ago

mborsetti commented 3 years ago

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 commented 3 years ago

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

adRn-s commented 2 years ago

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 commented 1 month ago

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.