8000 Replace pytz with standard library's zoneinfo by markcampanelli · Pull Request #2343 · pvlib/pvlib-python · GitHub
[go: up one dir, main page]

Skip to content

Replace pytz with standard library's zoneinfo #2343

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

Draft
wants to merge 16 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Remove remaining direct dependency on pytz
  • Loading branch information
markcampanelli committed Dec 23, 2024
commit 9b104ec3ee269d0f91492ed7b65d2e6457a57a39
2 changes: 1 addition & 1 deletion ci/requirements-py3.10.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ dependencies:
- pytest-rerunfailures
- conda-forge::pytest-remotedata # version in default channel is old
- python=3.10
- pytz
# - pytz
- requests
- scipy >= 1.6.0
- statsmodels
Expand Down
2 changes: 1 addition & 1 deletion ci/requirements-py3.11.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ dependencies:
- pytest-rerunfailures
- conda-forge::pytest-remotedata # version in default channel is old
- python=3.11
- pytz
# - pytz
- requests
- scipy >= 1.6.0
- statsmodels
Expand Down
2 changes: 1 addition & 1 deletion ci/requirements-py3.12.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ dependencies:
- pytest-rerunfailures
- conda-forge::pytest-remotedata # version in default channel is old
- python=3.12
- pytz
# - pytz
- requests
- scipy >= 1.6.0
- statsmodels
Expand Down
2 changes: 1 addition & 1 deletion ci/requirements-py3.9-min.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ dependencies:
- pytest-mock
- pytest-timeout
- python=3.9
- pytz
# - pytz
- requests
- pip:
- h5py==3.0.0
Expand Down
2 changes: 1 addition & 1 deletion ci/requirements-py3.9.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ dependencies:
- pytest-rerunfailures
- conda-forge::pytest-remotedata # version in default channel is old
- python=3.9
- pytz
# - pytz
- requests
- scipy >= 1.6.0
- statsmodels
Expand Down
32 changes: 16 additions & 16 deletions docs/tutorials/solarposition.ipynb

Large diffs are not rendered by default.

20 changes: 10 additions & 10 deletions pvlib/location.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@

import h5py
import pandas as pd
import pytz
# import pytz

from pvlib import atmosphere, clearsky, irradiance, solarposition
from pvlib._deprecation import warn_deprecated
# from pvlib._deprecation import warn_deprecated
from pvlib.tools import _degrees_to_index


Expand Down Expand Up @@ -42,7 +42,7 @@
Positive is east of the prime meridian.
Use decimal degrees notation.

tz : str, int, float, zoneinfo.ZoneInfo, datetime.timezone, or pytz timezone (deprecated), default 'UTC'.

Check failure on line 45 in pvlib/location.py

View workflow job for this annotation

GitHub Actions / flake8-linter

E501 line too long (109 > 79 characters)
See http://en.wikipedia.org/wiki/List_of_tz_database_time_zones for a
list of valid time zone strings, or use the function
zoneinfo.available_timezones().
Expand Down Expand Up @@ -90,14 +90,14 @@
self.tz = tz
elif isinstance(tz, datetime.timezone):
self.tz = ZoneInfo(str(tz))
elif isinstance(tz, pytz.BaseTzInfo):
warn_deprecated(
"0.11.3",
message='pytz timezones are deprecated',
alternative='use zoneinfo.ZoneInfo from the standard library',
obj_type='function argument type',
)
self.tz = ZoneInfo(tz.zone)
# elif isinstance(tz, pytz.BaseTzInfo):
# warn_deprecated(
# "0.11.3",
# message='pytz-based timezones are deprecated for locations',
# alternative='use zoneinfo.ZoneInfo from the standard library',

Check failure on line 97 in pvlib/location.py

View workflow job for this annotation

GitHub Actions / flake8-linter

E501 line too long (80 > 79 characters)
# obj_type='function argument type',
# )
# self.tz = ZoneInfo(tz.zone)
else:
raise TypeError(f'Invalid tz specification: {tz}')

Expand Down
17 changes: 12 additions & 5 deletions pvlib/tests/test_location.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from .conftest import assert_frame_equal, assert_index_equal

import pytest
import pytz
# import pytz

import pvlib
from pvlib import location
Expand All @@ -26,10 +26,17 @@ def test_location_all():
Location(32.2, -111, 'US/Arizona', 700, 'Tucson')


@pytest.mark.parametrize('tz', [
pytz.timezone('US/Arizona'), ZoneInfo('US/Arizona'), 'America/Phoenix',
-7, -7.0, datetime.timezone.utc
])
@pytest.mark.parametrize(
'tz',
[
F438 # pytz.timezone('US/Arizona'),
ZoneInfo('US/Arizona'),
'America/Phoenix',
-7,
-7.0,
datetime.timezone.utc,
]
)
def test_location_tz(tz):
Location(32.2, -111, tz)

Expand Down
37 changes: 22 additions & 15 deletions pvlib/tests/test_solarposition.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
import calendar
import datetime
import warnings
import zoneinfo

import numpy as np
import pandas as pd

from .conftest import assert_frame_equal, assert_series_equal
from numpy.testing import assert_allclose
import pandas as pd
import pytest
import pytz

from pvlib.location import Location
from pvlib import solarposition, spa

from .conftest import (
requires_ephem, requires_spa_c, requires_numba, requires_pandas_2_0
from pvlib.location import Location
from pvlib.tests.conftest import (
assert_frame_equal, assert_series_equal, requires_ephem, requires_spa_c,
requires_numba, requires_pandas_2_0,
)


# setup times and locations to be tested.
times = pd.date_range(start=datetime.datetime(2014, 6, 24),
end=datetime.datetime(2014, 6, 26), freq='15min')
Expand Down Expand Up @@ -348,13 +347,12 @@ def test_calc_time():
# validation from USNO solar position calculator online

epoch = datetime.datetime(1970, 1, 1)
epoch_dt = pytz.utc.localize(epoch)
epoch_dt = epoch.replace(tzinfo=zoneinfo.ZoneInfo("UTC"))

loc = tus
actual_time = pytz.timezone(loc.tz).localize(
datetime.datetime(2014, 10, 10, 8, 30))
lb = pytz.timezone(loc.tz).localize(datetime.datetime(2014, 10, 10, tol))
ub = pytz.timezone(loc.tz).localize(datetime.datetime(2014, 10, 10, 10))
actual_time = datetime.datetime(2014, 10, 10, 8, 30, tzinfo=loc.tz)
lb = datetime.datetime(2014, 10, 10, tol, tzinfo=loc.tz)
ub = datetime.datetime(2014, 10, 10, 10, tzinfo=loc.tz)
alt = solarposition.calc_time(lb, ub, loc.latitude, loc.longitude,
'alt', math.radians(24.7))
az = solarposition.calc_time(lb, ub, loc.latitude, loc.longitude,
Expand Down Expand Up @@ -724,9 +722,12 @@ def test_hour_angle_with_tricky_timezones():
'2014-09-07 02:00:00',
]).tz_localize('America/Santiago', nonexistent='shift_forward')

with pytest.raises(pytz.exceptions.NonExistentTimeError):
# should raise `pytz.exceptions.NonExistentTimeError`
with pytest.raises(Exception, match="2014-09-07 00:00:00") as exc_info:
times.normalize()

assert exc_info.type.__name__ == "NonExistentTimeError"

# should not raise `pytz.exceptions.NonExistentTimeError`
solarposition.hour_angle(times, longitude, eot)

Expand All @@ -738,9 +739,15 @@ def test_hour_angle_with_tricky_timezones():
'2014-11-02 02:00:00',
]).tz_localize('America/Havana', ambiguous=[True, True, False, False])

with pytest.raises(pytz.exceptions.AmbiguousTimeError):
# should raise `pytz.exceptions.AmbiguousTimeError`
with pytest.raises(
Exception,
match="Cannot infer dst time from 2014-11-02 00:00:00",
) as exc_info:
solarposition.hour_angle(times, longitude, eot)

assert exc_info.type.__name__ == "AmbiguousTimeError"


def test_sun_rise_set_transit_geometric(expected_rise_set_spa, golden_mst):
"""Test geometric calculations for sunrise, sunset, and transit times"""
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ dependencies = [
'h5py >=3.12, <4',
'numpy >=1.19.3, <3',
'pandas >=1.3.0, <3',
'pytz',
# 'pytz',
'requests >=2, <3',
'scipy >=1.6.0, <2',
]
Expand Down
Loading
0