Skip to content

Commit

Permalink
Update open_dataset backend to ensure compatibility with new explicit…
Browse files Browse the repository at this point in the history
… index model (#7150)

* Update open_dataset backend to ensure compatibility with new explicit index model

* Avoid generation of Indexes object

* Add test ensuring backend compatibility with multiindices

* Update xarray/tests/test_backends_api.py

* Use stack to construct multi index in test

* Mention open_dataset backend multi-index compatibility in whats-new

* remove _create_multiindex utility function

* remove pandas import

* Update doc/whats-new.rst

Co-authored-by: Deepak Cherian <dcherian@users.noreply.github.com>
Co-authored-by: Benoit Bovy <bbovy@gfz-potsdam.de>
Co-authored-by: Benoit Bovy <benbovy@gmail.com>
  • Loading branch information
4 people authored Oct 12, 2022
1 parent abc0b5d commit 3599f87
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
2 changes: 2 additions & 0 deletions doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ Bug fixes
:py:meth:`DataArray.to_index` for multi-index levels (convert to single index).
(:issue:`6836`, :pull:`7105`)
By `Benoît Bovy <https://github.com/benbovy>`_.
- Support for open_dataset backends that return datasets containing multi-indexes (:issue:`7139`, :pull:`7150`)
By `Lukas Bindreiter <https://github.com/lukasbindreiter>`_.

Documentation
~~~~~~~~~~~~~
Expand Down
2 changes: 1 addition & 1 deletion xarray/backends/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ def _get_mtime(filename_or_obj):

def _protect_dataset_variables_inplace(dataset, cache):
for name, variable in dataset.variables.items():
if name not in variable.dims:
if name not in dataset._indexes:
# no need to protect IndexVariable objects
data = indexing.CopyOnWriteArray(variable._data)
if cache:
Expand Down
19 changes: 19 additions & 0 deletions xarray/tests/test_backends_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,25 @@ def open_dataset(
assert_identical(expected, actual)


def test_multiindex() -> None:
# GH7139
# Check that we properly handle backends that change index variables
dataset = xr.Dataset(coords={"coord1": ["A", "B"], "coord2": [1, 2]})
dataset = dataset.stack(z=["coord1", "coord2"])

class MultiindexBackend(xr.backends.BackendEntrypoint):
def open_dataset(
self,
filename_or_obj,
drop_variables=None,
**kwargs,
) -> xr.Dataset:
return dataset.copy(deep=True)

loaded = xr.open_dataset("fake_filename", engine=MultiindexBackend)
assert_identical(dataset, loaded)


class PassThroughBackendEntrypoint(xr.backends.BackendEntrypoint):
"""Access an object passed to the `open_dataset` method."""

Expand Down

0 comments on commit 3599f87

Please sign in to comment.