Skip to content

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

V-R-S
Copy link

@V-R-S V-R-S commented Jun 8, 2025

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 #29064

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
@mattip
Copy link
Member

mattip commented Jun 9, 2025

Looks good so far. In order for tests to actual exercise this code, you need to refactor the tests to use tzinfo zoneinfo here

# Use pytz to test out various time zones if available
try:
from pytz import timezone as tz
_has_pytz = True

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants