Skip to content

Commit 34ab7c4

Browse files
0x0L0x0L
0x0L
authored and
0x0L
committed
dataset fix and more tests
1 parent 420c308 commit 34ab7c4

File tree

3 files changed

+26
-6
lines changed

3 files changed

+26
-6
lines changed

xarray/core/dataset.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3254,11 +3254,11 @@ def rank(self, dim, pct=False, keep_attrs=False):
32543254

32553255
variables = OrderedDict()
32563256
for name, var in iteritems(self.variables):
3257-
if name in self.data_vars and dim in var.dims:
3258-
variables[name] = var.rank(dim, pct=pct)
3259-
variables.update({
3260-
k: self.variables[k] for k in var.dims
3261-
if k not in variables and k in self.variables})
3257+
if name in self.data_vars:
3258+
if dim in var.dims:
3259+
variables[name] = var.rank(dim, pct=pct)
3260+
else:
3261+
variables[name] = var
32623262

32633263
coord_names = set(k for k in self.coords if k in variables)
32643264
attrs = self.attrs if keep_attrs else None

xarray/tests/test_dataset.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3414,9 +3414,19 @@ def test_quantile(self):
34143414
@requires_bottleneck
34153415
def test_rank(self):
34163416
ds = create_test_data(seed=1234)
3417-
x = ds.rank('dim3').var3
3417+
# only ds.var3 depends on dim3
3418+
z = ds.rank('dim3')
3419+
self.assertItemsEqual(['var3'], list(z.data_vars))
3420+
# same as dataarray version
3421+
x = z.var3
34183422
y = ds.var3.rank('dim3')
34193423
self.assertDataArrayEqual(x, y)
3424+
# coordinates stick
3425+
self.assertItemsEqual(list(z.coords), list(ds.coords))
3426+
self.assertItemsEqual(list(x.coords), list(y.coords))
3427+
# invalid dim
3428+
with raises_regex(ValueError, 'does not contain'):
3429+
x.rank('invalid_dim')
34203430

34213431
def test_count(self):
34223432
ds = Dataset({'x': ('a', [np.nan, 1]), 'y': 0, 'z': np.nan})

xarray/tests/test_variable.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1355,6 +1355,13 @@ def test_quantile_dask_raises(self):
13551355
with raises_regex(TypeError, 'arrays stored as dask'):
13561356
v.quantile(0.5, dim='x')
13571357

1358+
@requires_dask
1359+
@requires_bottleneck
1360+
def test_rank_dask_raises(self):
1361+
v = Variable(['x'], [3.0, 1.0, np.nan, 2.0, 4.0]).chunk(2)
1362+
with raises_regex(TypeError, 'arrays stored as dask'):
1363+
v.rank('x')
1364+
13581365
@requires_bottleneck
13591366
def test_rank(self):
13601367
import bottleneck as bn
@@ -1376,6 +1383,9 @@ def test_rank(self):
13761383
v = Variable(['x'], [3.0, 1.0, np.nan, 2.0, 4.0])
13771384
v_expect = Variable(['x'], [0.75, 0.25, np.nan, 0.5, 1.0])
13781385
self.assertVariableEqual(v.rank('x', pct=True), v_expect)
1386+
# invalid dim
1387+
with raises_regex(ValueError, 'not found'):
1388+
v.rank('y')
13791389

13801390
def test_big_endian_reduce(self):
13811391
# regression test for GH489

0 commit comments

Comments
 (0)