pvlib-python icon indicating copy to clipboard operation
pvlib-python copied to clipboard

Replace pytz with standard library's zoneinfo

Open markcampanelli opened this issue 1 year ago • 4 comments

Is your feature request related to a problem? Please describe.

I find pytz to be somewhat confusing and cumbersome. Since Python 3.9, pytz can be replaced by the standard library's zoneinfo package for full and up-to-date IANA timezone support. Furthermore, it appears that localization of "naive" timedate timestamps is fully supported without using pytz. For example:

import datetime
from pprint import pprint
import zoneinfo


pprint(zoneinfo.available_timezones())
time_naive = datetime.datetime(1974, 6, 22, 18, 30, 15)
print(time_naive)
time_aware = time_naive.replace(tzinfo=zoneinfo.ZoneInfo("Etc/GMT+5"))
print(time_aware)
print(time_aware.astimezone(zoneinfo.ZoneInfo("UTC")))

In particular, the pvlib.location.Location class can be streamlined significantly by using only the zoneinfo.ZoneInfo class as the timezone attribute. Of course, pytz would still be lurking in the background due to its significant usage in pandas.

Describe the solution you'd like Switch from pytz to zoneinfo. We could optionally deprecate pytz usage initially in the pvlib.location.Location constructor, but my sense is that simply removing it entirely will not break many people who typically instead use timezone strings in the initializer. Also, https://pvlib-python.readthedocs.io/en/latest/user_guide/timetimezones.html would need a significant update.

Describe alternatives you've considered Keep using pytz.

Additional context See ~https://github.com/pvlib/pvlib-python/pull/2339~ https://github.com/pvlib/pvlib-python/pull/2343 for an example of pytz's full removal.

markcampanelli avatar Dec 23 '24 08:12 markcampanelli

I confess this is the first I've heard of zoneinfo, but it seems like the python community at large is agreed that switching from pytz to zoneinfo is a good idea. Thanks @markcampanelli for bringing it up!

I'm seeing some people saying that Windows doesn't include a tz database by default. Depending on tzdata may be necessary: https://docs.python.org/3/library/zoneinfo.html

Of course, pytz would still be lurking in the background due to its significant usage in pandas.

Good point, although it looks like pandas 3.0 will make pytz an optional dependency (https://github.com/pandas-dev/pandas/pull/59089).

kandersolar avatar Dec 23 '24 20:12 kandersolar

@kandersolar Did you happen to discover if there was a more explicit way to "localize" a datetime using ZoneInfo other than the following?

time_naive = datetime.datetime(1974, 6, 22, 18, 30, 15)
time_aware = time_naive.replace(tzinfo=zoneinfo.ZoneInfo("Etc/GMT+5"))

markcampanelli avatar Dec 24 '24 03:12 markcampanelli

I'm seeing some people saying that Windows doesn't include a tz database by default. Depending on tzdata may be necessary: https://docs.python.org/3/library/zoneinfo.html

It seems like this is only with significantly out of date Windows installations. All the Windows tests added in #2341 that use zoneinfo.ZoneInfo pass.

markcampanelli avatar Jan 10 '25 17:01 markcampanelli

UPDATE: #2341 updated the "internal" time-zone representation in location objects, i.e., pvlib.location.Location._zoneinfo, to use the zoneinfo library. Remaining work seems to be mostly deprecating/removing the pytz field and updating the documentation away from pytz throughout but especially in https://pvlib-python.readthedocs.io/en/stable/user_guide/timetimezones.html.

markcampanelli avatar Mar 12 '25 13:03 markcampanelli