Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
lkstrp opened this issue Jun 12, 2024 · 0 comments · Fixed by #310
Closed

Comments

@lkstrp
Copy link
Member

lkstrp commented Jun 12, 2024

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant