Skip to content

xarray DeprecationWarning: Deleting a single level of a MultiIndex is deprecated. Previously #303

Closed
@lkstrp

Description

@lkstrp

Linopy throws a xarray DeprecationWarning quite a few times. If raised as exceptions, 89 tests fail. Mainly in test/test_linear_expression.py.

../site-packages/xarray/core/dataarray.py:3146: DeprecationWarning: Deleting a single level of a MultiIndex is deprecated. Previously, this deleted all levels of a MultiIndex. Please also drop the following variables: {'z'} to avoid an error in the future.

Needs a deeper dive, but here are some thoughts:
Only happens with multi index actions and when arrays are assigned to a dataset with a new reference. See some examples below. During the merge of two objects, xarray is dropping conflicting coords. But deleting them leads to further problems. I'm also not sure whether this is an xarray issue, because the code can only be adapted in a rather unpleasant way.

Links:
Dropping a MultiIndex variable raises an error after explicit indexes refactor · Issue #6505 · pydata/xarray
Explicit indexes by benbovy · Pull Request #5692 · pydata/xarray
python - Why cannot I add a DataArray to an existing Dataset with a MultiIndex? - Stack Overflow

Occurs in:

data[attr] = data[attr].astype(float)

linopy/linopy/model.py

Lines 457 to 476 in 8294530

labels = DataArray(-2, coords=data.coords)
self.check_force_dim_names(labels)
start = self._xCounter
end = start + labels.size
labels.data = np.arange(start, end).reshape(labels.shape)
self._xCounter += labels.size
if mask is not None:
labels = labels.where(mask, -1)
data = data.assign(labels=labels).assign_attrs(
label_range=(start, end), name=name, binary=binary, integer=integer
)
if self.chunk:
data = data.chunk(self.chunk)
variable = Variable(data, name=name, model=self)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions