-
-
Notifications
You must be signed in to change notification settings - Fork 11k
TST: migrating from pytz to zoneinfo + tzdata (where needed) #29148
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
base: main
Are you sure you want to change the base?
Conversation
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
Looks good so far. In order for tests to actual exercise this code, you need to refactor the tests to use numpy/numpy/_core/tests/test_datetime.py Lines 19 to 22 in 75fd1ff
|
Ah missed the test's. |
Cool. There are still a few more references to pytz to clean up, from
|
Taken care. |
_has_pytz = True | ||
except ImportError: | ||
_has_pytz = False | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
On windows without installing tzdata
creating a ZoneInfo object will fail. Perhaps you could do
try:
ZoneInfo('US/Central')
_has_tz = True
except zoneinfo.ZoneInfoNotFoundError:
_has_tz = False
and then use it to skip the test below
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added
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.
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 #29064