ENH: support zoneinfo tzinfos by jbrockmendel · Pull Request #46425 · pandas-dev/pandas (original) (raw)

Yes, or if the user is on Windows and doesn't have tzdata installed, which is actually probably a reasonably common failure mode — user doesn't care about tzdata because they're not using zoneinfo, then they import pandas and this constructor fails when trying to import the timezones module.

I imagine it's pretty easy to make the impact of this minimal. One way to do it:

cdef tzinfo utc_zoneinfo = None

cdef bool is_utc_zoneinfo(tzinfo tz): global utc_zoneinfo if utc_zoneinfo is None: try: utc_zoneinfo = ZoneInfo("UTC") except ZoneInfoNotFoundError: return False

return tz is utc_zoneinfo

Presumably this function will get inlined wherever you call it, and in the "common case" where ZoneInfo("UTC") is easily imported it's going to be two identity checks instead of 1.

If you are very concerned with performance I'd probably be trying to lazy-import zoneinfo in general anyway, in which case you have more "off-roads" to improve performance for people who don't use zoneinfo, though hopefully in the not-too-distant future pandas will switch to using zoneinfo or pytz-deprecation-shim anyway, at which point you'll be fine with eagerly importing.