You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Operations on arrays with indexes of object dtype are significantly slower if cftime library (not a minimal dependency) is not installed.
This is due to the fact that _maybe_cast_to_cftimeindex eagerly tries to import cftime every time it is called. Failed imports are not cached, so this is re-attempted many times over the course of an operation, leading to significant slowdown. If cftime is installed (e.g., by including the io extra dependency group), the issue is not encountered.
I dug a little bit: issue was similarly reported in #5929 and then resolved by #5640, but regressed back in #9561.
This one instance can be resolved by patching the _maybe_cast_to_cftimeindex helper to use utils.module_available, which is lru-cached. Something like
def _maybe_cast_to_cftimeindex(index: pd.Index) -> pd.Index:
from xarray.coding.cftimeindex import CFTimeIndex
if (
len(index) > 0
and index.dtype == "O"
and utils.module_available("cftime") # this is new
and not isinstance(index, CFTimeIndex)
):
try:
return CFTimeIndex(index)
except (ImportError, TypeError):
return index
else:
return index
I propose this in #11558.
Similarly happy with any other resolution, if you don't like this one.
Activity