Repository: Ouranosinc/xclim

Issue:
generic.fit incorrectly orders dimensions
* xclim version: 0.18.1-beta

### Description
<!--Describe what you were trying to get done.
Tell us what happened, what went wrong, and what you expected to happen.-->
Works as long as time is the first dimension, breaks otherwise (I think). 
For example, 
`xclim.indices.generic.fit(da)`  where `da` has dimensions `(site, time)`.

Discussion:

Patch:
diff --git a/HISTORY.rst b/HISTORY.rst
index 8aa7b5b3..a53a3b5c 100644
--- a/HISTORY.rst
+++ b/HISTORY.rst
@@ -21,6 +21,8 @@ New features and enhancements
 
 Bug fixes
 ~~~~~~~~~
+* Fix `generic.fit` dimension ordering. This caused errors when "time" was not the first dimension in a DataArray.
+
 
 Internal changes
 ~~~~~~~~~~~~~~~~
diff --git a/xclim/indices/generic.py b/xclim/indices/generic.py
index 0d23bc93..3465fc00 100644
--- a/xclim/indices/generic.py
+++ b/xclim/indices/generic.py
@@ -143,9 +143,7 @@ def fit(da: xr.DataArray, dist: str = "norm"):
     coords["dparams"] = dist_params
 
     # Dimensions for the distribution parameters
-    dims = ["dparams"]
-    dims.extend(da.dims)
-    dims.remove("time")
+    dims = [d if d != "time" else "dparams" for d in da.dims]
 
     out = xr.DataArray(data=data, coords=coords, dims=dims)
     out.attrs = da.attrs


Test patch:
diff --git a/tests/test_ensembles.py b/tests/test_ensembles.py
index 3a838f34..5efd4902 100644
--- a/tests/test_ensembles.py
+++ b/tests/test_ensembles.py
@@ -340,7 +340,8 @@ class TestEnsembleReduction:
             make_graph=False,
             variable_weights=var_weights,
         )
-        assert ids == [4, 10, 12, 13, 16]
+        # Results here may change according to sklearn version, hence the *isin* intead of ==
+        assert all(np.isin([4, 12, 13, 16], ids))
         assert len(ids) == 5
 
     def test_kmeans_modelweights(self):
diff --git a/tests/test_generic.py b/tests/test_generic.py
index 901380a3..6cc6e272 100644
--- a/tests/test_generic.py
+++ b/tests/test_generic.py
@@ -59,6 +59,11 @@ class TestFA(object):
         out = generic.fit(da, "lognorm").values
         assert np.isnan(out[:, 0, 0]).all()
 
+    def test_dims_order(self):
+        da = self.da.transpose()
+        p = generic.fit(da)
+        assert p.dims[-1] == "dparams"
+
 
 class TestFrequencyAnalysis:
     def test_simple(self, ndq_series):