Skip to content

[PERFORMANCE] object indexes repeatedly attempt to import missing cftime (regression) #11559

Closed

Description

@peterhron

What is your issue?

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

  1. added
    needs triageIssue that has not been reviewed by xarray team member
    on Sep 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    needs triageIssue that has not been reviewed by xarray team member

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions