
The issue here is not the use of a CategoricalIndex but the category labels (x.categories) is an IntervalIndex which xarray doesn't like. I can avoid the error by converting x (and xlag) to a different dtype instead of pandas.Categorical before using pandas.crosstab, but then I lose any empty categories, which I need to keep in my real application. TypeError: Cannot interpret 'interval' as a data type opt/scitools/environments/default/2021_03_18-1/lib/python3.6/site-packages/xarray/core/indexing.py in _init_(self, array, dtype) > 1486 return PandasIndexAdapter(array, self._dtype) opt/scitools/environments/default/2021_03_18-1/lib/python3.6/site-packages/xarray/core/indexing.py in copy(self, deep)ġ485 array = (deep=True) if deep else self.array opt/scitools/environments/default/2021_03_18-1/lib/python3.6/site-packages/xarray/core/variable.py in copy(self, deep, data) > 1220 variables = ġ222 raise ValueError("Data must be dict-like") opt/scitools/environments/default/2021_03_18-1/lib/python3.6/site-packages/xarray/core/dataset.py in copy(self, deep, data) opt/scitools/environments/default/2021_03_18-1/lib/python3.6/site-packages/xarray/core/alignment.py in align(join, copy, indexes, exclude, fill_value, *objects)ģ53 # fast path for no reindexing necessary opt/scitools/environments/default/2021_03_18-1/lib/python3.6/site-packages/xarray/core/alignment.py in deep_align(objects, join, copy, indexes, exclude, raise_on_invalid, fill_value) > 611 coerced, join=join, copy=False, indexes=indexes, fill_value=fill_valueĦ13 collected = collect_variables_and_indexes(aligned) opt/scitools/environments/default/2021_03_18-1/lib/python3.6/site-packages/xarray/core/merge.py in merge_core(objects, compat, join, combine_attrs, priority_arg, explicit_coords, indexes, fill_value)Ħ09 coerced = coerce_pandas_values(objects) opt/scitools/environments/default/2021_03_18-1/lib/python3.6/site-packages/xarray/core/merge.py in dataset_update_method(dataset, other) > 3899 merge_result = dataset_update_method(self, other)ģ900 return self._replace(inplace=True, **merge_result._asdict()) opt/scitools/environments/default/2021_03_18-1/lib/python3.6/site-packages/xarray/core/dataset.py in update(self, other) opt/scitools/environments/default/2021_03_18-1/lib/python3.6/site-packages/xarray/core/dataset.py in _set_numpy_data_from_dataframe(self, idx, arrays, dims)ĥ062 data = np.zeros(shape, values.dtype)ġ431 def _delitem_(self, key: Hashable) -> None: > 5133 obj._set_numpy_data_from_dataframe(idx, arrays, dims) > 2820 return _dataframe(self)Ģ822 in from_dataframe(cls, dataframe, sparse)ĥ131 obj._set_sparse_data_from_dataframe(idx, arrays, dims) opt/scitools/environments/default/2021_03_18-1/lib/python3.6/site-packages/pandas/core/generic.py in to_xarray(self)Ģ818 return _series(self) (v1.0.1 gives "ValueError: all arrays must be same length"). It seems to me it ought to be straightfoward enough using pandas.crosstab followed by DataFrame.to_xarray() but I'm getting "TypeError: Cannot interpret 'interval' as a data type" in pandas v1.1.5. I want to create a multiway contingency table from my pandas dataframe and store it in an xarray.
