Skip to content

Commit ae03616

Browse files
authored
Fix multi-index with categorical values. (#3860)
* Fix bug for multi-index with categorical values. See issue #3674. * Blacked. * Add line in whats-new.rst. * Remove forgotten print. Co-authored-by: Matthieu Ancellin <[email protected]>
1 parent 0d95eba commit ae03616

File tree

3 files changed

+16
-0
lines changed

3 files changed

+16
-0
lines changed

doc/whats-new.rst

+3
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ Bug fixes
5858
indexed variable (:issue:`3252`).
5959
By `David Huard <https://github.com/huard>`_.
6060

61+
62+
- Fix use of multi-index with categorical values (:issue:`3674`).
63+
By `Matthieu Ancellin <https://github.com/mancellin>`_.
6164
- Fix alignment with ``join="override"`` when some dimensions are unindexed. (:issue:`3681`).
6265
By `Deepak Cherian <https://github.com/dcherian>`_.
6366
- Fix :py:meth:`Dataset.swap_dims` and :py:meth:`DataArray.swap_dims` producing

xarray/core/indexes.py

+2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ def remove_unused_levels_categories(index):
2222
for i, level in enumerate(index.levels):
2323
if isinstance(level, pd.CategoricalIndex):
2424
level = level[index.codes[i]].remove_unused_categories()
25+
else:
26+
level = level[index.codes[i]]
2527
levels.append(level)
2628
index = pd.MultiIndex.from_arrays(levels, names=index.names)
2729
elif isinstance(index, pd.CategoricalIndex):

xarray/tests/test_dataset.py

+11
Original file line numberDiff line numberDiff line change
@@ -1458,6 +1458,17 @@ def test_categorical_reindex(self):
14581458
actual = ds.reindex(cat=["foo"])["cat"].values
14591459
assert (actual == np.array(["foo"])).all()
14601460

1461+
def test_categorical_multiindex(self):
1462+
i1 = pd.Series([0, 0])
1463+
cat = pd.CategoricalDtype(categories=["foo", "baz", "bar"])
1464+
i2 = pd.Series(["baz", "bar"], dtype=cat)
1465+
1466+
df = pd.DataFrame({"i1": i1, "i2": i2, "values": [1, 2]}).set_index(
1467+
["i1", "i2"]
1468+
)
1469+
actual = df.to_xarray()
1470+
assert actual["values"].shape == (1, 2)
1471+
14611472
def test_sel_drop(self):
14621473
data = Dataset({"foo": ("x", [1, 2, 3])}, {"x": [0, 1, 2]})
14631474
expected = Dataset({"foo": 1})

0 commit comments

Comments
 (0)