-
-
Notifications
You must be signed in to change notification settings - Fork 10.9k
TST: migrate from pytz to zoneinfo + tzdata (where needed) #29064
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Here I see the task would not be just to replace the pytz with ZoneInfo
This particular snippet will work but
This won't as it will result in an exception It seems that function get_tzoffset_from_pytzinfo in numpy/_core/src/multiarray/datetime.c might need to be modified to handle the new behaviour of a different object type. But I also see that this particular function will impact other places as well. |
What other places would be impacted? |
Well not in this codebase. As I first thought it would, but definitely it might affect the codebases that use this function datetime_as_string That would depend if it is required to support pytz along with ZoneInfo.
I think a quick change like this would make ZoneInfo object work. with the above change at least the tests doesn't fail.(I have modified the tests to use ZoneInfo) |
Why? |
For migration from pytz to zoneinfo function get_tzoffset_from_pytzinfo from numpy/_core/src/multiarray/datetime.c is modified to use astimezone instead of fromutc. As the object ZoneInfo is not directly compatible to be used with datetime object. Hence, something like this would result in an exception. from datetime import datetime from zoneinfo import ZoneInfo a = datetime(2025, 6, 7, 10, 0, 0) zoneInfo = ZoneInfo("US/Central") b = zoneInfo.fromutc(a) ValueError: fromutc: dt.tzinfo is not self The function astimezone can be used with both pytz.timezone object and zoneinfo.ZoneInfo object But, if we want to use the datetime object consistently we cannot let it be a naive type i.e. without a timezone. As the default behaviour of astimezone would take the system timezone if the datetime object is not timezone aware. Hence, I had to also change the call to create datetime object to take UTC timezone. See numpy#29064
The problem would be the naive datetime object which is not aware of the timezone. But I have taken an approach inmy PR please have a look. |
We use pytz in testing, notably in
numpy/_core/tests/test_datetime.py
andnumpy/_core/tests/test_deprecations.py
. Since Python3.9, the recommended way to access timezone information is to use the standard libraryzoneinfo
, and installtzdata
where needed (like windows). We should refactor our tests to use zoneinfo.The text was updated successfully, but these errors were encountered: