diff --git a/doc/whats-new.rst b/doc/whats-new.rst index 55773af92b3..72d67785fc5 100644 --- a/doc/whats-new.rst +++ b/doc/whats-new.rst @@ -61,8 +61,10 @@ Bug fixes By `Deepak Cherian `_. -- Removed usages of `pytest.config`, which is deprecated (:issue:`2988`:) +- Removed usages of `pytest.config`, which is deprecated (:issue:`2988`) By `Maximilian Roos `_. +- Fixed performance issues with cftime installed (:issue:`3000`) + By `0x0L `_. .. _whats-new.0.12.1: diff --git a/xarray/coding/cftimeindex.py b/xarray/coding/cftimeindex.py index 6ce7831a5bc..cf10d6238aa 100644 --- a/xarray/coding/cftimeindex.py +++ b/xarray/coding/cftimeindex.py @@ -184,7 +184,7 @@ def get_date_type(self): def assert_all_valid_date_type(data): import cftime - if data.size: + if len(data) > 0: sample = data[0] date_type = type(sample) if not isinstance(sample, cftime.datetime): @@ -229,12 +229,12 @@ class CFTimeIndex(pd.Index): date_type = property(get_date_type) def __new__(cls, data, name=None): + assert_all_valid_date_type(data) if name is None and hasattr(data, 'name'): name = data.name result = object.__new__(cls) result._data = np.array(data, dtype='O') - assert_all_valid_date_type(result._data) result.name = name return result diff --git a/xarray/core/utils.py b/xarray/core/utils.py index 94787dd35e2..386019a3dbf 100644 --- a/xarray/core/utils.py +++ b/xarray/core/utils.py @@ -62,7 +62,7 @@ def wrapper(*args, **kwargs): def _maybe_cast_to_cftimeindex(index: pd.Index) -> pd.Index: from ..coding.cftimeindex import CFTimeIndex - if index.dtype == 'O': + if len(index) > 0 and index.dtype == 'O': try: return CFTimeIndex(index) except (ImportError, TypeError): diff --git a/xarray/tests/test_dataarray.py b/xarray/tests/test_dataarray.py index 43af27d0696..1e2360fc5e7 100644 --- a/xarray/tests/test_dataarray.py +++ b/xarray/tests/test_dataarray.py @@ -1760,6 +1760,12 @@ def test_stack_unstack(self): orig = DataArray([[0, 1], [2, 3]], dims=['x', 'y'], attrs={'foo': 2}) assert_identical(orig, orig.unstack()) + # test GH3000 + a = orig[:0, :1].stack(dim=('x', 'y')).dim.to_index() + b = pd.MultiIndex(levels=[pd.Int64Index([]), pd.Int64Index([0])], + labels=[[], []], names=['x', 'y']) + pd.util.testing.assert_index_equal(a, b) + actual = orig.stack(z=['x', 'y']).unstack('z').drop(['x', 'y']) assert_identical(orig, actual)