Skip to content

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

@mborsetti

Description

@mborsetti

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)

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions