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 3 commits 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

@V-R-S
Copy link
Author

V-R-S commented Jun 10, 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

Ah missed the test's.
Added them now.

@V-R-S V-R-S force-pushed the refactor-zoneinfo branch from 961957d to a18f690 Compare June 10, 2025 11:54
@V-R-S V-R-S force-pushed the refactor-zoneinfo branch from a18f690 to 81b2a67 Compare June 10, 2025 11:55
@mattip
Copy link
Member

mattip commented Jun 10, 2025

Cool. There are still a few more references to pytz to clean up, from git grep pytz

  • .github/workflows/linux.yml:
  • numpy/_core/multiarray.py in the documentation of datetime_as_string (I wonder how doctests are passing)
  • requirements/doc_requirements.txt in a comment

@V-R-S
Copy link
Author

V-R-S commented Jun 10, 2025

Taken care.

_has_pytz = True
except ImportError:
_has_pytz = False

Copy link
Member

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

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added

@V-R-S V-R-S force-pushed the refactor-zoneinfo branch from fe84835 to 39777b9 Compare June 10, 2025 17:36
@V-R-S V-R-S force-pushed the refactor-zoneinfo branch from 6f540c8 to 0f1a6f8 Compare June 10, 2025 17:38
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