@@ -137,57 +137,73 @@ def test_da_groupby_empty():
137
137
138
138
def test_da_groupby_quantile ():
139
139
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
+ )
141
143
142
144
# 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
+ )
144
148
actual = array .groupby ("x" ).quantile (0.5 )
145
149
assert_identical (expected , actual )
146
150
147
151
# 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
+ )
149
157
actual = array .groupby ("x" ).quantile ([0 , 1 ])
150
158
assert_identical (expected , actual )
151
159
152
160
# Multiple dimensions
153
161
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" ),
156
165
)
157
166
158
167
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
+ )
160
171
assert_identical (expected_x , actual_x )
161
172
162
173
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
+ )
164
177
assert_identical (expected_y , actual_y )
165
178
166
179
actual_xx = array .groupby ("x" ).quantile (0 )
167
180
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" ),
169
184
)
170
185
assert_identical (expected_xx , actual_xx )
171
186
172
187
actual_yy = array .groupby ("y" ).quantile (0 )
173
188
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" ),
176
192
)
177
193
assert_identical (expected_yy , actual_yy )
178
194
179
195
times = pd .date_range ("2000-01-01" , periods = 365 )
180
196
x = [0 , 1 ]
181
197
foo = xr .DataArray (
182
198
np .reshape (np .arange (365 * 2 ), (365 , 2 )),
183
- coords = dict ( time = times , x = x ) ,
199
+ coords = { " time" : times , "x" : x } ,
184
200
dims = ("time" , "x" ),
185
201
)
186
202
g = foo .groupby (foo .time .dt .month )
187
203
188
204
actual = g .quantile (0 , dim = ...)
189
205
expected = xr .DataArray (
190
- [
206
+ data = [
191
207
0.0 ,
192
208
62.0 ,
193
209
120.0 ,
@@ -201,12 +217,111 @@ def test_da_groupby_quantile():
201
217
610.0 ,
202
218
670.0 ,
203
219
],
204
- [("month" , np .arange (1 , 13 ))],
220
+ coords = {"month" : np .arange (1 , 13 ), "quantile" : 0 },
221
+ dims = "month" ,
205
222
)
206
223
assert_identical (expected , actual )
207
224
208
225
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
+ )
231
+ assert_identical (expected , actual )
232
+
233
+
234
+ def test_ds_groupby_quantile ():
235
+ ds = xr .Dataset (
236
+ data_vars = {"a" : ("x" , [1 , 2 , 3 , 4 , 5 , 6 ])}, coords = {"x" : [1 , 1 , 1 , 2 , 2 , 2 ]}
237
+ )
238
+
239
+ # Scalar quantile
240
+ expected = xr .Dataset (
241
+ data_vars = {"a" : ("x" , [2 , 5 ])}, coords = {"quantile" : 0.5 , "x" : [1 , 2 ]}
242
+ )
243
+ actual = ds .groupby ("x" ).quantile (0.5 )
244
+ assert_identical (expected , actual )
245
+
246
+ # Vector quantile
247
+ expected = xr .Dataset (
248
+ data_vars = {"a" : (("x" , "quantile" ), [[1 , 3 ], [4 , 6 ]])},
249
+ coords = {"x" : [1 , 2 ], "quantile" : [0 , 1 ]},
250
+ )
251
+ actual = ds .groupby ("x" ).quantile ([0 , 1 ])
252
+ assert_identical (expected , actual )
253
+
254
+ # Multiple dimensions
255
+ ds = xr .Dataset (
256
+ data_vars = {
257
+ "a" : (
258
+ ("x" , "y" ),
259
+ [[1 , 11 , 26 ], [2 , 12 , 22 ], [3 , 13 , 23 ], [4 , 16 , 24 ], [5 , 15 , 25 ]],
260
+ )
261
+ },
262
+ coords = {"x" : [1 , 1 , 1 , 2 , 2 ], "y" : [0 , 0 , 1 ]},
263
+ )
264
+
265
+ actual_x = ds .groupby ("x" ).quantile (0 , dim = ...)
266
+ expected_x = xr .Dataset ({"a" : ("x" , [1 , 4 ])}, coords = {"x" : [1 , 2 ], "quantile" : 0 })
267
+ assert_identical (expected_x , actual_x )
268
+
269
+ actual_y = ds .groupby ("y" ).quantile (0 , dim = ...)
270
+ expected_y = xr .Dataset ({"a" : ("y" , [1 , 22 ])}, coords = {"y" : [0 , 1 ], "quantile" : 0 })
271
+ assert_identical (expected_y , actual_y )
272
+
273
+ actual_xx = ds .groupby ("x" ).quantile (0 )
274
+ expected_xx = xr .Dataset (
275
+ {"a" : (("x" , "y" ), [[1 , 11 , 22 ], [4 , 15 , 24 ]])},
276
+ coords = {"x" : [1 , 2 ], "y" : [0 , 0 , 1 ], "quantile" : 0 },
277
+ )
278
+ assert_identical (expected_xx , actual_xx )
279
+
280
+ actual_yy = ds .groupby ("y" ).quantile (0 )
281
+ expected_yy = xr .Dataset (
282
+ {"a" : (("x" , "y" ), [[1 , 26 ], [2 , 22 ], [3 , 23 ], [4 , 24 ], [5 , 25 ]])},
283
+ coords = {"x" : [1 , 1 , 1 , 2 , 2 ], "y" : [0 , 1 ], "quantile" : 0 },
284
+ ).transpose ()
285
+ assert_identical (expected_yy , actual_yy )
286
+
287
+ times = pd .date_range ("2000-01-01" , periods = 365 )
288
+ x = [0 , 1 ]
289
+ foo = xr .Dataset (
290
+ {"a" : (("time" , "x" ), np .reshape (np .arange (365 * 2 ), (365 , 2 )))},
291
+ coords = dict (time = times , x = x ),
292
+ )
293
+ g = foo .groupby (foo .time .dt .month )
294
+
295
+ actual = g .quantile (0 , dim = ...)
296
+ expected = xr .Dataset (
297
+ {
298
+ "a" : (
299
+ "month" ,
300
+ [
301
+ 0.0 ,
302
+ 62.0 ,
303
+ 120.0 ,
304
+ 182.0 ,
305
+ 242.0 ,
306
+ 304.0 ,
307
+ 364.0 ,
308
+ 426.0 ,
309
+ 488.0 ,
310
+ 548.0 ,
311
+ 610.0 ,
312
+ 670.0 ,
313
+ ],
314
+ )
315
+ },
316
+ coords = {"month" : np .arange (1 , 13 ), "quantile" : 0 },
317
+ )
318
+ assert_identical (expected , actual )
319
+
320
+ actual = g .quantile (0 , dim = "time" ).isel (month = slice (None , 2 ))
321
+ expected = xr .Dataset (
322
+ data_vars = {"a" : (("month" , "x" ), [[0.0 , 1 ], [62.0 , 63 ]])},
323
+ coords = {"month" : [1 , 2 ], "x" : [0 , 1 ], "quantile" : 0 },
324
+ )
210
325
assert_identical (expected , actual )
211
326
212
327
0 commit comments