Skip to content

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

Open
mattip opened this issue May 27, 2025 · 5 comments
Open

TST: migrate from pytz to zoneinfo + tzdata (where needed) #29064

mattip opened this issue May 27, 2025 · 5 comments
Labels
sprintable Issue fits the time-frame and setting of a sprint

Comments

@mattip
Copy link
Member

mattip commented May 27, 2025

We use pytz in testing, notably in numpy/_core/tests/test_datetime.py and numpy/_core/tests/test_deprecations.py. Since Python3.9, the recommended way to access timezone information is to use the standard library zoneinfo, and install tzdata where needed (like windows). We should refactor our tests to use zoneinfo.

@mattip mattip added the sprintable Issue fits the time-frame and setting of a sprint label May 27, 2025
@V-R-S
Copy link

V-R-S commented Jun 7, 2025

Here I see the task would not be just to replace the pytz with ZoneInfo
As ZoneInfo is not compatible with datetime as pytz is compatible with it.

from datetime import datetime
from pytz import timezone as tz

a = datetime(2025, 6, 7, 10, 0, 0)
zonePytz = pytz("US/Central")
c = zonePytz.fromutc(a)

This particular snippet will work but

from datetime import datetime
from zoneinfo import ZoneInfo

a = datetime(2025, 6, 7, 10, 0, 0)
zoneInfo = ZoneInfo("US/Central")
b = zoneInfo.fromutc(a)

This won't as it will result in an exception
ValueError: fromutc: dt.tzinfo is not self

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.
Please guide me if there are gaps in my understanding.

@mattip
Copy link
Member Author

mattip commented Jun 7, 2025

What other places would be impacted?

@V-R-S
Copy link

V-R-S commented Jun 7, 2025

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.

diff --git a/numpy/_core/src/multiarray/datetime.c b/numpy/_core/src/multiarray/datetime.c
index 9c024dbcd9..88e7ec6cc7 100644
--- a/numpy/_core/src/multiarray/datetime.c
+++ b/numpy/_core/src/multiarray/datetime.c
@@ -2262,7 +2262,7 @@ get_tzoffset_from_pytzinfo(PyObject *timezone_obj, npy_datetimestruct *dts)
     }

     /* Convert the datetime from UTC to local time */
-    loc_dt = PyObject_CallMethod(timezone_obj, "fromutc", "O", dt);
+    loc_dt = PyObject_CallMethod(dt, "astimezone", "O", timezone_obj);
     Py_DECREF(dt);
     if (loc_dt == NULL) {
         return -1;

I think a quick change like this would make ZoneInfo object work.
it can work with pytz but the result might be inconsistent.

with the above change at least the tests doesn't fail.(I have modified the tests to use ZoneInfo)
================================= 47140 passed, 1738 skipped, 2818 deselected, 33 xfailed, 5 xpassed in 313.85s (0:05:13) ==================================

@mattip
Copy link
Member Author

mattip commented Jun 8, 2025

It can work with pytz but the result might be inconsistent.

Why?

V-R-S added a commit to V-R-S/numpy that referenced this issue 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 numpy#29064
@V-R-S
Copy link

V-R-S commented Jun 8, 2025

The problem would be the naive datetime object which is not aware of the timezone.
solution create a datetime object with timezone.
which is not provided in the
PyObject *PyDateTime_FromDateAndTime(int year, int month, int day, int hour, int minute, int second, int usecond)

But I have taken an approach inmy PR please have a look.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
sprintable Issue fits the time-frame and setting of a sprint
Projects
None yet
Development

No branches or pull requests

2 participants
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy