Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions cf_xarray/coding.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ def encode_multi_index_as_compress(ds, idxnames=None):
encoded = ds.reset_index(idxnames)
for idxname in idxnames:
mindex = ds.indexes[idxname]
if any(any(char.isspace() for char in name) for name in mindex.names):
raise ValueError(
f"MultiIndex level names for {idxname!r} must not contain whitespace: "
f"{mindex.names!r}. Rename the levels before encoding."
)
coords = dict(zip(mindex.names, mindex.levels, strict=False))
encoded.update(coords)
for c in coords:
Expand Down
19 changes: 19 additions & 0 deletions cf_xarray/tests/test_coding.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,25 @@
import cf_xarray as cfxr


@pytest.mark.parametrize("level_name", ["north south", "lat\titude", "lat\nitude"])
@pytest.mark.parametrize("idxnames", [None, "point"])
def test_compression_rejects_whitespace_in_level_names(level_name, idxnames):
index = pd.MultiIndex.from_product([[1, 2], [3, 4]], names=(level_name, "lon"))
dataset = xr.Dataset(coords=xr.Coordinates.from_pandas_multiindex(index, "point"))
before = dataset.copy(deep=True)
with pytest.raises(ValueError, match="whitespace"):
cfxr.encode_multi_index_as_compress(dataset, idxnames)
xr.testing.assert_identical(dataset, before)


def test_compression_preserves_noncompressed_names_with_spaces():
index = pd.MultiIndex.from_product([[1, 2], [3, 4]], names=("lat", "lon"))
dataset = xr.Dataset(coords=xr.Coordinates.from_pandas_multiindex(index, "point"))
dataset["unrelated variable"] = xr.DataArray(42)
encoded = cfxr.encode_multi_index_as_compress(dataset)
xr.testing.assert_identical(cfxr.decode_compress_to_multi_index(encoded), dataset)


@pytest.mark.parametrize("with_data", [False, True])
def test_compression_preserves_unrelated_coordinates(with_data):
index = pd.MultiIndex.from_product([[1, 2], [3, 4]], names=("x", "y"))
Expand Down
4 changes: 4 additions & 0 deletions doc/coding.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ convention could be used for either {py:class}`pandas.MultiIndex` objects or `py
`cf_xarray` provides {py:func}`encode_multi_index_as_compress` and {py:func}`decode_compress_to_multi_index` to encode MultiIndex-ed
dimensions using "compression by gathering".

MultiIndex level names must not contain whitespace, because the CF `compress`
attribute stores them as a space-separated list. Rename such levels before encoding;
unrelated variables are not subject to this restriction.

Here's a test dataset

```{code-cell}
Expand Down
Loading