spedas / pyspedas

Python-based Space Physics Environment Data Analysis Software
https://pyspedas.readthedocs.io/
MIT License
143 stars 58 forks source link

Usage of deprecated datetime function utcfromtimestamp() #819

Closed jameswilburlewis closed 3 months ago

jameswilburlewis commented 3 months ago

We have about 22 usages of this routine between pyspedas, pytplot, and cdflib, which will be removed from the datetime library in a future release.

The recommended action is to replace all these calls with datetime.datetime.fromtimestamp(timestamp, datetime.UTC)

However, datetime.UTC was only introduced in Python 3.11. The backward compatible replacement is datetime.datetime.fromtimestamp(timestamp, datetime.timezone.utc)

jameswilburlewis commented 3 months ago

There's a difficulty with the suggested replacement: xarray doesn't support interpolation with timezone-aware datetime objects. This affects tinterpol and probably some of the other interpolation routines. utcfromtimestamp() returned timezone-naive datetimes, while from_timestamp() returns timezone-aware objects. xarray can work with np.datetime64 objects (as long as they have nanosecond precision), so that's what we should use in that context.