Open kgoebber opened 11 months ago
So the whole situation is kind of a mess. Pandas apparent does actually support timezones but that gets stored under its own Dtype; numpy datetime64
is defined to be naive; and xarray is caught in the middle as well. There is probably a role we could potentially play trying to contribute, but I personally do not have a great handle on all the potential pitfalls that are at play--I only know enough to know that dates & times & timezones are a huge pain.
I'm not even sure how you could open a netCDF file and make the time axis set the timezone to UTC, but it seems to be possible?
Given that, for MetPy I'm coming around to the idea that while we should address the warnings and avoid deprecated methods (#3255), we should keep using tz-naive datetime
instances internally. So e.g.:
datetime.utcnow()
becomes
datetime.now(timezone.utc).replace(tzinfo=None)
That feels ugly to drop timezone information (which really is nice to have for robust labelling, etc.), but would preserve the existing behavior and avoid a bunch of headaches for our users until the rest of the ecosystem is more ready to deal with them.
So for completeness, #3255 implements this using a sensible mix:
datetime
(e.g. directly passed to string formatting) stay tz-aware when generateddatetime
instances have the timezone information dropped to avoid changing behavior and creating the problems outlined above.Is there anything more we need to do in MetPy itself about this?
What went wrong?
In Python 3.12 the
datetime.utcnow()
functionality is removed. The solution is to move to timezone aware datetime objects withThe issue that comes up is that our data that we bring in, whether surface, upper air, or gridded have traditionally not been timezone aware. There are many instances of setting a datetime, whether with
utcnow()
or with a manual setting (e.g.,datetime(2017, 3, 8, 12)
that are/were timezone naive. The issue comes in when comparing timezone aware to timezone naive values as that comparison cannot be done. This is most likely to come up when subsetting data for a certain time or time window.This issue captures our need to address this with the challenge being in determining how to address this change holistically. Some changes may need to be in MetPy, while others lie upstream with other packages or might require code examples to highlight how to get the proper object type for users.
Operating System
MacOS
Version
1.5.1
Python Version
3.12
Code to Reproduce
Errors, Traceback, and Logs