From 74ee35708261cc5acecb7896c5bc736f4fa66680 Mon Sep 17 00:00:00 2001 From: Julia Signell Date: Wed, 6 Aug 2025 10:49:46 -0400 Subject: [PATCH] Sort axis/dims before passing to numpy --- xarray/namedarray/core.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/xarray/namedarray/core.py b/xarray/namedarray/core.py index dac8162ca45..3c5f90c819e 100644 --- a/xarray/namedarray/core.py +++ b/xarray/namedarray/core.py @@ -905,6 +905,13 @@ def reduce( if dim is not None: axis = self.get_axis_num(dim) + # sorting the axis ensures that the performance is consistent + # regardless of the input order of the dims + if isinstance(axis, tuple): + axis = tuple(sorted(axis)) + elif isinstance(axis, list): + axis = sorted(axis) + with warnings.catch_warnings(): warnings.filterwarnings( "ignore", r"Mean of empty slice", category=RuntimeWarning