Skip to content

Commit 2514e90

Browse files
committed
Better setting of Coordinates in UniqueGrouper
1 parent 0f0f21e commit 2514e90

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

xarray/groupers.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import datetime
1010
from abc import ABC, abstractmethod
1111
from dataclasses import dataclass, field
12-
from typing import Any, Literal, cast
12+
from typing import TYPE_CHECKING, Any, Literal, cast
1313

1414
import numpy as np
1515
import pandas as pd
@@ -205,21 +205,31 @@ def _factorize_dummy(self) -> EncodedGroups:
205205
group_indices: GroupIndices = tuple(slice(i, i + 1) for i in range(size))
206206
size_range = np.arange(size)
207207
full_index: pd.Index
208+
unique_coord: _DummyGroup | Variable
208209
if isinstance(self.group, _DummyGroup):
209210
codes = self.group.to_dataarray().copy(data=size_range)
210211
unique_coord = self.group
211212
full_index = pd.RangeIndex(self.group.size)
213+
coords = Coordinates()
212214
else:
213215
codes = self.group.copy(data=size_range)
214216
unique_coord = self.group.variable.to_base_variable()
215217
full_index = self.group_as_index
218+
if isinstance(full_index, pd.MultiIndex):
219+
coords = Coordinates.from_pandas_multiindex(
220+
full_index, dim=self.group.name
221+
)
222+
else:
223+
if TYPE_CHECKING:
224+
assert isinstance(unique_coord, Variable)
225+
coords = _coordinates_from_variable(unique_coord)
216226

217227
return EncodedGroups(
218228
codes=codes,
219229
group_indices=group_indices,
220230
full_index=full_index,
221231
unique_coord=unique_coord,
222-
coords=Coordinates(),
232+
coords=coords,
223233
)
224234

225235

0 commit comments

Comments
 (0)