Skip to content
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

Replace pytz with standard library's zoneinfo #2342

Open
markcampanelli opened this issue Dec 23, 2024 · 2 comments · May be fixed by #2343
Open

Replace pytz with standard library's zoneinfo #2342

markcampanelli opened this issue Dec 23, 2024 · 2 comments · May be fixed by #2343

Comments

@markcampanelli
Copy link
Contributor

markcampanelli commented Dec 23, 2024

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 #2339 #2343 for an example of pytz's full removal.

@kandersolar
Copy link
Member

kandersolar commented Dec 23, 2024

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 (pandas-dev/pandas#59089).

@markcampanelli
Copy link
Contributor Author

@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"))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants