From d1524324859299f062e3034605115c00f2f505bb Mon Sep 17 00:00:00 2001 From: nawaaaaaAaar <198336470+nawaaaaaAaar@users.noreply.github.com> Date: Thu, 22 Oct 2026 14:30:20 +0000 Subject: [PATCH] Reject whitespace in compressed MultiIndex level names --- cf_xarray/coding.py | 5 +++++ cf_xarray/tests/test_coding.py | 19 +++++++++++++++++++ doc/coding.md | 4 ++++ 3 files changed, 28 insertions(+) diff --git a/cf_xarray/coding.py b/cf_xarray/coding.py index a80356e9..c5522116 100644 --- a/cf_xarray/coding.py +++ b/cf_xarray/coding.py @@ -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: diff --git a/cf_xarray/tests/test_coding.py b/cf_xarray/tests/test_coding.py index a509715a..eba4db1a 100644 --- a/cf_xarray/tests/test_coding.py +++ b/cf_xarray/tests/test_coding.py @@ -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")) diff --git a/doc/coding.md b/doc/coding.md index e4063469..39f2eeb3 100644 --- a/doc/coding.md +++ b/doc/coding.md @@ -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}