@@ -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,17 @@ 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
+ )
210
231
assert_identical (expected , actual )
211
232
212
233
@@ -216,7 +237,9 @@ def test_ds_groupby_quantile():
216
237
)
217
238
218
239
# 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
+ )
220
243
actual = ds .groupby ("x" ).quantile (0.5 )
221
244
assert_identical (expected , actual )
222
245
@@ -240,24 +263,24 @@ def test_ds_groupby_quantile():
240
263
)
241
264
242
265
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 })
244
267
assert_identical (expected_x , actual_x )
245
268
246
269
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 })
248
271
assert_identical (expected_y , actual_y )
249
272
250
273
actual_xx = ds .groupby ("x" ).quantile (0 )
251
274
expected_xx = xr .Dataset (
252
275
{"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 },
254
277
)
255
278
assert_identical (expected_xx , actual_xx )
256
279
257
280
actual_yy = ds .groupby ("y" ).quantile (0 )
258
281
expected_yy = xr .Dataset (
259
282
{"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 },
261
284
).transpose ()
262
285
assert_identical (expected_yy , actual_yy )
263
286
@@ -290,14 +313,14 @@ def test_ds_groupby_quantile():
290
313
],
291
314
)
292
315
},
293
- coords = {"month" : np .arange (1 , 13 )},
316
+ coords = {"month" : np .arange (1 , 13 ), "quantile" : 0 },
294
317
)
295
318
assert_identical (expected , actual )
296
319
297
320
actual = g .quantile (0 , dim = "time" ).isel (month = slice (None , 2 ))
298
321
expected = xr .Dataset (
299
322
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 },
301
324
)
302
325
assert_identical (expected , actual )
303
326
0 commit comments