Skip to content

Commit e30a89f

Browse files
committed
Drop duplicates before unstacking
1 parent c07688c commit e30a89f

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

xarray/tests/test_state_machine.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,15 @@ def get_not_multiindex_dims(ds: Dataset) -> set:
2222
return set(dims) - set(mindexes)
2323

2424

25+
def get_multiindex_dims(ds: Dataset) -> list:
26+
mindexes = [
27+
name
28+
for name, index in ds.xindexes.items()
29+
if isinstance(index, PandasMultiIndex)
30+
]
31+
return mindexes
32+
33+
2534
def get_dimension_coordinates(ds: Dataset) -> set:
2635
return set(ds.dims) & set(ds._variables)
2736

@@ -84,16 +93,19 @@ def stack(self, newname):
8493

8594
@rule()
8695
def unstack(self):
87-
# TODO: Drop duplicates
88-
self.dataset = self.dataset.unstack()
96+
choices = get_multiindex_dims(self.dataset)
97+
if choices:
98+
dim = random.choice(choices)
99+
self.dataset = self.dataset.drop_duplicates(dim).unstack(dim)
100+
else:
101+
self.dataset = self.dataset.unstack()
89102

90103
@rule(newname=UNIQUE_NAME)
91104
@precondition(lambda self: bool(get_dimension_coordinates(self.dataset)))
92105
def rename_vars(self, newname):
93106
# benbovy: "skip the default indexes invariant test when the name of an
94107
# existing dimension coordinate is passed as input kwarg or dict key
95108
# to .rename_vars()."
96-
97109
oldname = random.choice(tuple(get_dimension_coordinates(self.dataset)))
98110
self.check_default_indexes = False
99111
note(f"> renaming {oldname} to {newname}")

0 commit comments

Comments
 (0)