xarray icon indicating copy to clipboard operation
xarray copied to clipboard

unable to stack on IntervalIndex

Open jmineau opened this issue 5 months ago • 1 comments

What happened?

Previously, in v2025.03.1 datasets were able to be stacked upon IntervalIndex dimensions. This is no longer the case. I now receive the following error:

TypeError: Cannot interpret 'interval[datetime64[ns], left]' as a data type

What did you expect to happen?

I expected to be able to stack on IntervalIndex dimensions as this was previous behavior.

Minimal Complete Verifiable Example

import numpy as np
import pandas as pd
import xarray as xr

t0 = pd.Timestamp('2024-01-01 00:00:00')
t1 = pd.Timestamp('2024-01-07 00:00:00')
dt = pd.Timedelta('1d')
t = pd.interval_range(start=t0, end=t1+dt, freq=dt, closed='left')

xmin = -112.15
xmax = -111.75
ymin = 40.45
ymax = 40.95
dx = dy = 0.05

x = np.arange(xmin, xmax + dx, dx)
y = np.arange(ymin, ymax + dy, dy)

ds = xr.Dataset(
    {
        'data': (['time', 'y', 'x'], np.random.rand(len(t), len(y), len(x))),
    },
    coords={
        'time': t,
        'y': y,
        'x': x,
    }
)

ds.stack(state=('time', 'y', 'x'))

MVCE confirmation

  • [x] Minimal example — the example is as focused as reasonably possible to demonstrate the underlying issue in xarray.
  • [x] Complete example — the example is self-contained, including all data and the text of any traceback.
  • [x] Verifiable example — the example copy & pastes into an IPython prompt or Binder notebook, returning the result.
  • [x] New issue — a search of GitHub Issues suggests this is not a duplicate.
  • [x] Recent environment — the issue occurs with the latest version of xarray and its dependencies.

Relevant log output

>>> ds.stack(state=('time', 'y', 'x'))
Traceback (most recent call last):
  File "<python-input-3>", line 1, in <module>
    ds.stack(state=('time', 'y', 'x'))
    ~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/uufs/chpc.utah.edu/common/home/u6036966/software/python/miniforge3/envs/xarray_interval/lib/python3.13/site-packages/xarray/core/dataset.py", line 2313, in __repr__
    return formatting.dataset_repr(self)
           ~~~~~~~~~~~~~~~~~~~~~~~^^^^^^
  File "/uufs/chpc.utah.edu/common/home/u6036966/software/python/miniforge3/envs/xarray_interval/lib/python3.13/reprlib.py", line 21, in wrapper
    result = user_function(self)
  File "/uufs/chpc.utah.edu/common/home/u6036966/software/python/miniforge3/envs/xarray_interval/lib/python3.13/site-packages/xarray/core/formatting.py", line 752, in dataset_repr
    summary.append(coords_repr(ds.coords, col_width=col_width, max_rows=max_rows))
                   ~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/uufs/chpc.utah.edu/common/home/u6036966/software/python/miniforge3/envs/xarray_interval/lib/python3.13/site-packages/xarray/core/formatting.py", line 441, in coords_repr
    return _mapping_repr(
        coords,
    ...<5 lines>...
        max_rows=max_rows,
    )
  File "/uufs/chpc.utah.edu/common/home/u6036966/software/python/miniforge3/envs/xarray_interval/lib/python3.13/site-packages/xarray/core/formatting.py", line 415, in _mapping_repr
    summarizer(k, v, col_width, **summarizer_kwargs[k])
    ~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/uufs/chpc.utah.edu/common/home/u6036966/software/python/miniforge3/envs/xarray_interval/lib/python3.13/site-packages/xarray/core/formatting.py", line 351, in summarize_variable
    values_str = inline_variable_array_repr(variable, values_width)
  File "/uufs/chpc.utah.edu/common/home/u6036966/software/python/miniforge3/envs/xarray_interval/lib/python3.13/site-packages/xarray/core/formatting.py", line 305, in inline_variable_array_repr
    return var._data._repr_inline_(max_width)
           ~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^
  File "/uufs/chpc.utah.edu/common/home/u6036966/software/python/miniforge3/envs/xarray_interval/lib/python3.13/site-packages/xarray/core/indexing.py", line 2057, in _repr_inline_
    return format_array_flat(self._get_array_subset(), max_width)
                             ~~~~~~~~~~~~~~~~~~~~~~^^
  File "/uufs/chpc.utah.edu/common/home/u6036966/software/python/miniforge3/envs/xarray_interval/lib/python3.13/site-packages/xarray/core/indexing.py", line 2049, in _get_array_subset
    return np.asarray(subset)
           ~~~~~~~~~~^^^^^^^^
  File "/uufs/chpc.utah.edu/common/home/u6036966/software/python/miniforge3/envs/xarray_interval/lib/python3.13/site-packages/xarray/core/indexing.py", line 1983, in __array__
    return np.asarray(
           ~~~~~~~~~~^
        self.array.get_level_values(self.level).values, dtype=dtype
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    )
    ^
TypeError: Cannot interpret 'interval[datetime64[ns], left]' as a data type

Anything else we need to know?

This seems similar to #10312

Environment

>>> xr.show_versions()

INSTALLED VERSIONS

commit: None python: 3.13.5 | packaged by conda-forge | (main, Jun 16 2025, 08:27:50) [GCC 13.3.0] python-bits: 64 OS: Linux OS-release: 4.18.0-553.44.1.el8_10.x86_64 machine: x86_64 processor: x86_64 byteorder: little LC_ALL: None LANG: en_US.UTF-8 LOCALE: ('en_US', 'UTF-8') libhdf5: None libnetcdf: None

xarray: 2025.6.1 pandas: 2.3.0 numpy: 2.3.0 scipy: None netCDF4: None pydap: None h5netcdf: None h5py: None zarr: None cftime: None nc_time_axis: None iris: None bottleneck: None dask: None distributed: None matplotlib: None cartopy: None seaborn: None numbagg: None fsspec: None cupy: None pint: None sparse: None flox: None numpy_groupies: None setuptools: None pip: 25.1.1 conda: None pytest: None mypy: None IPython: None sphinx: None

jmineau avatar Jun 22 '25 06:06 jmineau

Thanks for opening your first issue here at xarray! Be sure to follow the issue template! If you have an idea for a solution, we would really welcome a Pull Request with proposed changes. See the Contributing Guide for more. It may take us a while to respond here, but we really value your contribution. Contributors like you help make xarray better. Thank you!

welcome[bot] avatar Jun 22 '25 06:06 welcome[bot]

Hi @jmineau thanks for the detailed bug report!

Are you still experiencing this issue, i just tried to replicate it with the latest xarray and could not get the error

ianhi avatar Sep 10 '25 17:09 ianhi