Skip to content

Commit 44d3cf1

Browse files
authored
Merge pull request #29148 from V-R-S/refactor-zoneinfo
TST: migrating from pytz to zoneinfo + tzdata (where needed)
2 parents 30a4883 + 0f1a6f8 commit 44d3cf1

File tree

10 files changed

+31
-39
lines changed

10 files changed

+31
-39
lines changed

.github/workflows/linux.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ jobs:
271271
# - name: Check docstests
272272
# shell: 'script -q -e -c "bash --noprofile --norc -eo pipefail {0}"'
273273
# run: |
274-
# pip install scipy-doctest>=1.8.0 hypothesis==6.104.1 matplotlib scipy pytz pandas
274+
# pip install scipy-doctest>=1.8.0 hypothesis==6.104.1 matplotlib scipy pandas
275275
# spin check-docs -v
276276
# spin check-tutorials -v
277277

environment.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,4 @@ dependencies:
4949
- gitpython
5050
# Used in some tests
5151
- cffi
52-
- pytz
52+
- tzdata

numpy/_core/multiarray.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1723,7 +1723,7 @@ def datetime_as_string(arr, unit=None, timezone=None, casting=None):
17231723
Examples
17241724
--------
17251725
>>> import numpy as np
1726-
>>> import pytz
1726+
>>> from zoneinfo import ZoneInfo
17271727
>>> d = np.arange('2002-10-27T04:30', 4*60, 60, dtype='M8[m]')
17281728
>>> d
17291729
array(['2002-10-27T04:30', '2002-10-27T05:30', '2002-10-27T06:30',
@@ -1736,9 +1736,9 @@ def datetime_as_string(arr, unit=None, timezone=None, casting=None):
17361736
'2002-10-27T07:30Z'], dtype='<U35')
17371737
17381738
Note that we picked datetimes that cross a DST boundary. Passing in a
1739-
``pytz`` timezone object will print the appropriate offset
1739+
``ZoneInfo`` object will print the appropriate offset
17401740
1741-
>>> np.datetime_as_string(d, timezone=pytz.timezone('US/Eastern'))
1741+
>>> np.datetime_as_string(d, timezone=ZoneInfo('US/Eastern'))
17421742
array(['2002-10-27T00:30-0400', '2002-10-27T01:30-0400',
17431743
'2002-10-27T01:30-0500', '2002-10-27T02:30-0500'], dtype='<U39')
17441744

numpy/_core/src/multiarray/_datetime.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,8 +174,8 @@ convert_datetime_metadata_tuple_to_datetime_metadata(PyObject *tuple,
174174
npy_bool from_pickle);
175175

176176
/*
177-
* Gets a tzoffset in minutes by calling the fromutc() function on
178-
* the Python datetime.tzinfo object.
177+
* Gets a tzoffset in minutes by calling the astimezone() function on
178+
* the Python datetime.datetime object.
179179
*/
180180
NPY_NO_EXPORT int
181181
get_tzoffset_from_pytzinfo(PyObject *timezone, npy_datetimestruct *dts);

numpy/_core/src/multiarray/datetime.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2245,8 +2245,8 @@ NpyDatetime_ConvertPyDateTimeToDatetimeStruct(
22452245
}
22462246

22472247
/*
2248-
* Gets a tzoffset in minutes by calling the fromutc() function on
2249-
* the Python datetime.tzinfo object.
2248+
* Gets a tzoffset in minutes by calling the astimezone() function on
2249+
* the Python datetime.datetime object.
22502250
*/
22512251
NPY_NO_EXPORT int
22522252
get_tzoffset_from_pytzinfo(PyObject *timezone_obj, npy_datetimestruct *dts)
@@ -2255,14 +2255,14 @@ get_tzoffset_from_pytzinfo(PyObject *timezone_obj, npy_datetimestruct *dts)
22552255
npy_datetimestruct loc_dts;
22562256

22572257
/* Create a Python datetime to give to the timezone object */
2258-
dt = PyDateTime_FromDateAndTime((int)dts->year, dts->month, dts->day,
2259-
dts->hour, dts->min, 0, 0);
2258+
dt = PyDateTimeAPI->DateTime_FromDateAndTime((int)dts->year, dts->month, dts->day,
2259+
dts->hour, dts->min, 0, 0, PyDateTime_TimeZone_UTC, PyDateTimeAPI->DateTimeType);
22602260
if (dt == NULL) {
22612261
return -1;
22622262
}
22632263

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

numpy/_core/tests/test_datetime.py

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import datetime
22
import pickle
3+
from zoneinfo import ZoneInfo, ZoneInfoNotFoundError
34

45
import pytest
56

@@ -16,18 +17,16 @@
1617
suppress_warnings,
1718
)
1819

19-
# Use pytz to test out various time zones if available
20-
try:
21-
from pytz import timezone as tz
22-
_has_pytz = True
23-
except ImportError:
24-
_has_pytz = False
25-
2620
try:
2721
RecursionError
2822
except NameError:
2923
RecursionError = RuntimeError # python < 3.5
3024

25+
try:
26+
ZoneInfo("US/Central")
27+
_has_tz = True
28+
except ZoneInfoNotFoundError:
29+
_has_tz = False
3130

3231
def _assert_equal_hash(v1, v2):
3332
assert v1 == v2
@@ -1886,7 +1885,7 @@ def test_datetime_as_string(self):
18861885
np.datetime64('2032-01-01T00:00:00', 'us'), unit='auto'),
18871886
'2032-01-01')
18881887

1889-
@pytest.mark.skipif(not _has_pytz, reason="The pytz module is not available.")
1888+
@pytest.mark.skipif(not _has_tz, reason="The tzdata module is not available.")
18901889
def test_datetime_as_string_timezone(self):
18911890
# timezone='local' vs 'UTC'
18921891
a = np.datetime64('2010-03-15T06:30', 'm')
@@ -1901,29 +1900,29 @@ def test_datetime_as_string_timezone(self):
19011900

19021901
b = np.datetime64('2010-02-15T06:30', 'm')
19031902

1904-
assert_equal(np.datetime_as_string(a, timezone=tz('US/Central')),
1903+
assert_equal(np.datetime_as_string(a, timezone=ZoneInfo('US/Central')),
19051904
'2010-03-15T01:30-0500')
1906-
assert_equal(np.datetime_as_string(a, timezone=tz('US/Eastern')),
1905+
assert_equal(np.datetime_as_string(a, timezone=ZoneInfo('US/Eastern')),
19071906
'2010-03-15T02:30-0400')
1908-
assert_equal(np.datetime_as_string(a, timezone=tz('US/Pacific')),
1907+
assert_equal(np.datetime_as_string(a, timezone=ZoneInfo('US/Pacific')),
19091908
'2010-03-14T23:30-0700')
19101909

1911-
assert_equal(np.datetime_as_string(b, timezone=tz('US/Central')),
1910+
assert_equal(np.datetime_as_string(b, timezone=ZoneInfo('US/Central')),
19121911
'2010-02-15T00:30-0600')
1913-
assert_equal(np.datetime_as_string(b, timezone=tz('US/Eastern')),
1912+
assert_equal(np.datetime_as_string(b, timezone=ZoneInfo('US/Eastern')),
19141913
'2010-02-15T01:30-0500')
1915-
assert_equal(np.datetime_as_string(b, timezone=tz('US/Pacific')),
1914+
assert_equal(np.datetime_as_string(b, timezone=ZoneInfo('US/Pacific')),
19161915
'2010-02-14T22:30-0800')
19171916

19181917
# Dates to strings with a timezone attached is disabled by default
19191918
assert_raises(TypeError, np.datetime_as_string, a, unit='D',
1920-
timezone=tz('US/Pacific'))
1919+
timezone=ZoneInfo('US/Pacific'))
19211920
# Check that we can print out the date in the specified time zone
19221921
assert_equal(np.datetime_as_string(a, unit='D',
1923-
timezone=tz('US/Pacific'), casting='unsafe'),
1922+
timezone=ZoneInfo('US/Pacific'), casting='unsafe'),
19241923
'2010-03-14')
19251924
assert_equal(np.datetime_as_string(b, unit='D',
1926-
timezone=tz('US/Central'), casting='unsafe'),
1925+
timezone=ZoneInfo('US/Central'), casting='unsafe'),
19271926
'2010-02-15')
19281927

19291928
def test_datetime_arange(self):

numpy/_core/tests/test_deprecations.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,6 @@
1313
import numpy as np
1414
from numpy.testing import assert_raises, temppath
1515

16-
try:
17-
import pytz # noqa: F401
18-
_has_pytz = True
19-
except ImportError:
20-
_has_pytz = False
21-
2216

2317
class _DeprecationTestCase:
2418
# Just as warning: warnings uses re.match, so the start of this message

requirements/doc_requirements.txt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@ pickleshare
1717
towncrier
1818
toml
1919

20-
21-
# for doctests, also needs pytz which is in test_requirements
2220
scipy-doctest>=1.8.0
2321

2422
# interactive documentation utilities
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
hypothesis==6.81.1
22
pytest==7.4.0
3-
pytz==2023.3.post1
3+
tzdata
44
pytest-xdist

requirements/test_requirements.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ setuptools==65.5.1 ; python_version < '3.12'
44
setuptools ; python_version >= '3.12'
55
hypothesis==6.104.1
66
pytest==7.4.0
7-
pytz==2023.3.post1
87
pytest-cov==4.1.0
98
meson
109
ninja; sys_platform != "emscripten"
@@ -17,3 +16,5 @@ mypy==1.16.0; platform_python_implementation != "PyPy"
1716
typing_extensions>=4.5.0
1817
# for optional f2py encoding detection
1918
charset-normalizer
19+
tzdata
20+

0 commit comments

Comments
 (0)
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