GeezerVison is showing github.com — TST: migrate from pytz to zoneinfo + tzdata (where needed) · Issue #29064 · numpy/numpy
TST: migrate from pytz to zoneinfo + tzdata (where needed) #29064Labels sprintableIssue fits the time-frame and setting of a sprint DescriptionWe 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. Reacted by Joren Hammudoglu ActivityV-R-S commented on 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 commented on Jun 7, 2025 What other places would be impacted? V-R-S commented on 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 commented on Jun 8, 2025
It can work with pytz but the result might be inconsistent.
Why? V-R-S commented on 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. mattip commented on Jun 11, 2025
MetadataMetadatasprintableIssue fits the time-frame and setting of a sprint DevelopmentNo branches or pull requestsIssue actions
|