Skip to content
Open
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
6 changes: 6 additions & 0 deletions doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ Bug Fixes
differ from the dtype returned by the matching numpy-backed bottleneck path,
notably ``object`` instead of ``float64`` for boolean inputs.
By `Matthew Rocklin <https://github.com/mrocklin>`_.
- Fixed a :py:class:`KeyError` when building a multi-index (e.g. via
:py:meth:`Dataset.stack` or :py:meth:`Dataset.set_index`) from levels whose
name is falsy but valid, such as an empty string ``""``. Such names were
incorrectly replaced by the default ``{dim}_level_{i}`` name instead of being
preserved.
By `Kropiunig <https://github.com/Kropiunig>`_.


Documentation
Expand Down
2 changes: 1 addition & 1 deletion xarray/core/indexes.py
Original file line number Diff line number Diff line change
Expand Up @@ -1038,7 +1038,7 @@ def __init__(self, array: Any, dim: Hashable, level_coords_dtype: Any = None):
# default index level names
names = []
for i, idx in enumerate(self.index.levels):
name = idx.name or f"{dim}_level_{i}"
name = idx.name if idx.name is not None else f"{dim}_level_{i}"
if name == dim:
raise ValueError(
f"conflicting multi-index level name {name!r} with dimension {dim!r}"
Expand Down
6 changes: 6 additions & 0 deletions xarray/tests/test_indexes.py
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,12 @@ def test_constructor(self) -> None:
index = PandasMultiIndex(pd_idx, "x")
assert list(index.index.names) == ["x_level_0", "x_level_1"]

# a falsy but valid level name (e.g. "") must be preserved rather than
# replaced by the default ``{dim}_level_{i}`` name
pd_idx = pd.MultiIndex.from_arrays([foo_data, bar_data], names=("", "bar"))
index = PandasMultiIndex(pd_idx, "x")
assert list(index.index.names) == ["", "bar"]

def test_from_variables(self) -> None:
v_level1 = xr.Variable(
"x", [1, 2, 3], attrs={"unit": "m"}, encoding={"dtype": np.int32}
Expand Down
Loading