Skip to content

Commit 5f9ca06

Browse files
committed
don't drop scalar quantile coords
1 parent 5c441c9 commit 5f9ca06

File tree

2 files changed

+50
-28
lines changed

2 files changed

+50
-28
lines changed

xarray/core/groupby.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -584,11 +584,12 @@ def quantile(self, q, dim=None, interpolation="linear", keep_attrs=None):
584584
Returns
585585
-------
586586
quantiles : Variable
587-
If `q` is a single quantile, then the result
588-
is a scalar. If multiple percentiles are given, first axis of
589-
the result corresponds to the quantile and a quantile dimension
590-
is added to the return array. The other dimensions are the
591-
dimensions that remain after the reduction of the array.
587+
If `q` is a single quantile, then the result is a
588+
scalar. If multiple percentiles are given, first axis of
589+
the result corresponds to the quantile. In either case a
590+
quantile dimension is added to the return array. The other
591+
dimensions are the dimensions that remain after the
592+
reduction of the array.
592593
593594
See Also
594595
--------
@@ -607,8 +608,6 @@ def quantile(self, q, dim=None, interpolation="linear", keep_attrs=None):
607608
keep_attrs=keep_attrs,
608609
)
609610

610-
if np.asarray(q, dtype=np.float64).ndim == 0:
611-
out = out.drop_vars("quantile")
612611
return out
613612

614613
def where(self, cond, other=dtypes.NA):

xarray/tests/test_groupby.py

Lines changed: 44 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -137,57 +137,73 @@ def test_da_groupby_empty():
137137

138138
def test_da_groupby_quantile():
139139

140-
array = xr.DataArray([1, 2, 3, 4, 5, 6], [("x", [1, 1, 1, 2, 2, 2])])
140+
array = xr.DataArray(
141+
data=[1, 2, 3, 4, 5, 6], coords={"x": [1, 1, 1, 2, 2, 2]}, dims="x"
142+
)
141143

142144
# Scalar quantile
143-
expected = xr.DataArray([2, 5], [("x", [1, 2])])
145+
expected = xr.DataArray(
146+
data=[2, 5], coords={"x": [1, 2], "quantile": 0.5}, dims="x"
147+
)
144148
actual = array.groupby("x").quantile(0.5)
145149
assert_identical(expected, actual)
146150

147151
# Vector quantile
148-
expected = xr.DataArray([[1, 3], [4, 6]], [("x", [1, 2]), ("quantile", [0, 1])])
152+
expected = xr.DataArray(
153+
data=[[1, 3], [4, 6]],
154+
coords={"x": [1, 2], "quantile": [0, 1]},
155+
dims=("x", "quantile"),
156+
)
149157
actual = array.groupby("x").quantile([0, 1])
150158
assert_identical(expected, actual)
151159

152160
# Multiple dimensions
153161
array = xr.DataArray(
154-
[[1, 11, 26], [2, 12, 22], [3, 13, 23], [4, 16, 24], [5, 15, 25]],
155-
[("x", [1, 1, 1, 2, 2]), ("y", [0, 0, 1])],
162+
data=[[1, 11, 26], [2, 12, 22], [3, 13, 23], [4, 16, 24], [5, 15, 25]],
163+
coords={"x": [1, 1, 1, 2, 2], "y": [0, 0, 1]},
164+
dims=("x", "y"),
156165
)
157166

158167
actual_x = array.groupby("x").quantile(0, dim=...)
159-
expected_x = xr.DataArray([1, 4], [("x", [1, 2])])
168+
expected_x = xr.DataArray(
169+
data=[1, 4], coords={"x": [1, 2], "quantile": 0}, dims="x"
170+
)
160171
assert_identical(expected_x, actual_x)
161172

162173
actual_y = array.groupby("y").quantile(0, dim=...)
163-
expected_y = xr.DataArray([1, 22], [("y", [0, 1])])
174+
expected_y = xr.DataArray(
175+
data=[1, 22], coords={"y": [0, 1], "quantile": 0}, dims="y"
176+
)
164177
assert_identical(expected_y, actual_y)
165178

166179
actual_xx = array.groupby("x").quantile(0)
167180
expected_xx = xr.DataArray(
168-
[[1, 11, 22], [4, 15, 24]], [("x", [1, 2]), ("y", [0, 0, 1])]
181+
data=[[1, 11, 22], [4, 15, 24]],
182+
coords={"x": [1, 2], "y": [0, 0, 1], "quantile": 0},
183+
dims=("x", "y"),
169184
)
170185
assert_identical(expected_xx, actual_xx)
171186

172187
actual_yy = array.groupby("y").quantile(0)
173188
expected_yy = xr.DataArray(
174-
[[1, 26], [2, 22], [3, 23], [4, 24], [5, 25]],
175-
[("x", [1, 1, 1, 2, 2]), ("y", [0, 1])],
189+
data=[[1, 26], [2, 22], [3, 23], [4, 24], [5, 25]],
190+
coords={"x": [1, 1, 1, 2, 2], "y": [0, 1], "quantile": 0},
191+
dims=("x", "y"),
176192
)
177193
assert_identical(expected_yy, actual_yy)
178194

179195
times = pd.date_range("2000-01-01", periods=365)
180196
x = [0, 1]
181197
foo = xr.DataArray(
182198
np.reshape(np.arange(365 * 2), (365, 2)),
183-
coords=dict(time=times, x=x),
199+
coords={"time": times, "x": x},
184200
dims=("time", "x"),
185201
)
186202
g = foo.groupby(foo.time.dt.month)
187203

188204
actual = g.quantile(0, dim=...)
189205
expected = xr.DataArray(
190-
[
206+
data=[
191207
0.0,
192208
62.0,
193209
120.0,
@@ -201,12 +217,17 @@ def test_da_groupby_quantile():
201217
610.0,
202218
670.0,
203219
],
204-
[("month", np.arange(1, 13))],
220+
coords={"month": np.arange(1, 13), "quantile": 0},
221+
dims="month",
205222
)
206223
assert_identical(expected, actual)
207224

208225
actual = g.quantile(0, dim="time")[:2]
209-
expected = xr.DataArray([[0.0, 1], [62.0, 63]], [("month", [1, 2]), ("x", [0, 1])])
226+
expected = xr.DataArray(
227+
data=[[0.0, 1], [62.0, 63]],
228+
coords={"month": [1, 2], "x": [0, 1], "quantile": 0},
229+
dims=("month", "x"),
230+
)
210231
assert_identical(expected, actual)
211232

212233

@@ -216,7 +237,9 @@ def test_ds_groupby_quantile():
216237
)
217238

218239
# Scalar quantile
219-
expected = xr.Dataset({"a": ("x", [2, 5])}, coords={"x": [1, 2]})
240+
expected = xr.Dataset(
241+
data_vars={"a": ("x", [2, 5])}, coords={"quantile": 0.5, "x": [1, 2]}
242+
)
220243
actual = ds.groupby("x").quantile(0.5)
221244
assert_identical(expected, actual)
222245

@@ -240,24 +263,24 @@ def test_ds_groupby_quantile():
240263
)
241264

242265
actual_x = ds.groupby("x").quantile(0, dim=...)
243-
expected_x = xr.Dataset({"a": ("x", [1, 4])}, coords={"x": [1, 2]})
266+
expected_x = xr.Dataset({"a": ("x", [1, 4])}, coords={"x": [1, 2], "quantile": 0})
244267
assert_identical(expected_x, actual_x)
245268

246269
actual_y = ds.groupby("y").quantile(0, dim=...)
247-
expected_y = xr.Dataset({"a": ("y", [1, 22])}, coords={"y": [0, 1]})
270+
expected_y = xr.Dataset({"a": ("y", [1, 22])}, coords={"y": [0, 1], "quantile": 0})
248271
assert_identical(expected_y, actual_y)
249272

250273
actual_xx = ds.groupby("x").quantile(0)
251274
expected_xx = xr.Dataset(
252275
{"a": (("x", "y"), [[1, 11, 22], [4, 15, 24]])},
253-
coords={"x": [1, 2], "y": [0, 0, 1]},
276+
coords={"x": [1, 2], "y": [0, 0, 1], "quantile": 0},
254277
)
255278
assert_identical(expected_xx, actual_xx)
256279

257280
actual_yy = ds.groupby("y").quantile(0)
258281
expected_yy = xr.Dataset(
259282
{"a": (("x", "y"), [[1, 26], [2, 22], [3, 23], [4, 24], [5, 25]])},
260-
coords={"x": [1, 1, 1, 2, 2], "y": [0, 1]},
283+
coords={"x": [1, 1, 1, 2, 2], "y": [0, 1], "quantile": 0},
261284
).transpose()
262285
assert_identical(expected_yy, actual_yy)
263286

@@ -290,14 +313,14 @@ def test_ds_groupby_quantile():
290313
],
291314
)
292315
},
293-
coords={"month": np.arange(1, 13)},
316+
coords={"month": np.arange(1, 13), "quantile": 0},
294317
)
295318
assert_identical(expected, actual)
296319

297320
actual = g.quantile(0, dim="time").isel(month=slice(None, 2))
298321
expected = xr.Dataset(
299322
data_vars={"a": (("month", "x"), [[0.0, 1], [62.0, 63]])},
300-
coords={"month": [1, 2], "x": [0, 1]},
323+
coords={"month": [1, 2], "x": [0, 1], "quantile": 0},
301324
)
302325
assert_identical(expected, actual)
303326

0 commit comments

Comments
 (0)