Skip to content

Commit e17cf59

Browse files
malmans2keewis
andauthored
Don't drop unreduced variables (#5393)
* don't drop unreduced variables * Update xarray/tests/test_dataset.py Co-authored-by: keewis <[email protected]> * add comment Co-authored-by: keewis <[email protected]>
1 parent 5c59b66 commit e17cf59

File tree

2 files changed

+7
-3
lines changed

2 files changed

+7
-3
lines changed

xarray/core/dataset.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4987,7 +4987,10 @@ def reduce(
49874987
variables[name] = var
49884988
else:
49894989
if (
4990-
not numeric_only
4990+
# Some reduction functions (e.g. std, var) need to run on variables
4991+
# that don't have the reduce dims: PR5393
4992+
not reduce_dims
4993+
or not numeric_only
49914994
or np.issubdtype(var.dtype, np.number)
49924995
or (var.dtype == np.bool_)
49934996
):

xarray/tests/test_dataset.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4952,15 +4952,16 @@ def test_reduce_cumsum_test_dims(self, reduct, expected, func):
49524952
def test_reduce_non_numeric(self):
49534953
data1 = create_test_data(seed=44)
49544954
data2 = create_test_data(seed=44)
4955-
add_vars = {"var4": ["dim1", "dim2"]}
4955+
add_vars = {"var4": ["dim1", "dim2"], "var5": ["dim1"]}
49564956
for v, dims in sorted(add_vars.items()):
49574957
size = tuple(data1.dims[d] for d in dims)
49584958
data = np.random.randint(0, 100, size=size).astype(np.str_)
49594959
data1[v] = (dims, data, {"foo": "variable"})
49604960

4961-
assert "var4" not in data1.mean()
4961+
assert "var4" not in data1.mean() and "var5" not in data1.mean()
49624962
assert_equal(data1.mean(), data2.mean())
49634963
assert_equal(data1.mean(dim="dim1"), data2.mean(dim="dim1"))
4964+
assert "var4" not in data1.mean(dim="dim2") and "var5" in data1.mean(dim="dim2")
49644965

49654966
@pytest.mark.filterwarnings(
49664967
"ignore:Once the behaviour of DataArray:DeprecationWarning"

0 commit comments

Comments
 (0)