Skip to content

Commit c648cfd

Browse files
committed
Better swap_dims
1 parent 491b9b1 commit c648cfd

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

xarray/tests/test_state_machine.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,13 +76,19 @@ def rename_vars(self, newname):
7676
note(f"> renaming {oldname} to {newname}")
7777

7878
@rule()
79-
@precondition(lambda self: len(self.dataset._variables) >= 2)
79+
@precondition(
80+
lambda self: (
81+
len(self.dataset._variables) >= 2
82+
and (set(self.dataset.dims) & set(self.dataset._variables))
83+
)
84+
)
8085
def swap_dims(self):
8186
ds = self.dataset
82-
dim = random.choice(tuple(ds.dims))
83-
84-
to = dim + "_" if "_" not in dim else dim[:-1]
85-
assert to in ds._variables
87+
# need a dimension coordinate for swapping
88+
dim = random.choice(tuple(set(ds.dims) & set(ds._variables)))
89+
to = random.choice(
90+
[name for name, var in ds._variables.items() if var.size == ds.sizes[dim]]
91+
)
8692
self.dataset = ds.swap_dims({dim: to})
8793
note(f"> swapping {dim} to {to}")
8894

0 commit comments

Comments
 (0)