@@ -258,27 +258,118 @@ def test_concat_join_kwarg(self):
258
258
)
259
259
assert_identical (actual , expected )
260
260
261
- def test_concat_combine_attrs_kwarg (self ):
262
- ds1 = Dataset ({"a" : ("x" , [0 ])}, coords = {"x" : [0 ]}, attrs = {"b" : 42 })
263
- ds2 = Dataset ({"a" : ("x" , [0 ])}, coords = {"x" : [1 ]}, attrs = {"b" : 42 , "c" : 43 })
264
-
265
- expected = {}
266
- expected ["drop" ] = Dataset ({"a" : ("x" , [0 , 0 ])}, {"x" : [0 , 1 ]})
267
- expected ["no_conflicts" ] = Dataset (
268
- {"a" : ("x" , [0 , 0 ])}, {"x" : [0 , 1 ]}, {"b" : 42 , "c" : 43 }
269
- )
270
- expected ["override" ] = Dataset ({"a" : ("x" , [0 , 0 ])}, {"x" : [0 , 1 ]}, {"b" : 42 })
271
-
272
- with raises_regex (ValueError , "combine_attrs='identical'" ):
273
- actual = concat ([ds1 , ds2 ], dim = "x" , combine_attrs = "identical" )
274
- with raises_regex (ValueError , "combine_attrs='no_conflicts'" ):
275
- ds3 = ds2 .copy (deep = True )
276
- ds3 .attrs ["b" ] = 44
277
- actual = concat ([ds1 , ds3 ], dim = "x" , combine_attrs = "no_conflicts" )
261
+ @pytest .mark .parametrize (
262
+ "combine_attrs, var1_attrs, var2_attrs, expected_attrs, expect_exception" ,
263
+ [
264
+ (
265
+ "no_conflicts" ,
266
+ {"a" : 1 , "b" : 2 },
267
+ {"a" : 1 , "c" : 3 },
268
+ {"a" : 1 , "b" : 2 , "c" : 3 },
269
+ False ,
270
+ ),
271
+ ("no_conflicts" , {"a" : 1 , "b" : 2 }, {}, {"a" : 1 , "b" : 2 }, False ),
272
+ ("no_conflicts" , {}, {"a" : 1 , "c" : 3 }, {"a" : 1 , "c" : 3 }, False ),
273
+ (
274
+ "no_conflicts" ,
275
+ {"a" : 1 , "b" : 2 },
276
+ {"a" : 4 , "c" : 3 },
277
+ {"a" : 1 , "b" : 2 , "c" : 3 },
278
+ True ,
279
+ ),
280
+ ("drop" , {"a" : 1 , "b" : 2 }, {"a" : 1 , "c" : 3 }, {}, False ),
281
+ ("identical" , {"a" : 1 , "b" : 2 }, {"a" : 1 , "b" : 2 }, {"a" : 1 , "b" : 2 }, False ),
282
+ ("identical" , {"a" : 1 , "b" : 2 }, {"a" : 1 , "c" : 3 }, {"a" : 1 , "b" : 2 }, True ),
283
+ (
284
+ "override" ,
285
+ {"a" : 1 , "b" : 2 },
286
+ {"a" : 4 , "b" : 5 , "c" : 3 },
287
+ {"a" : 1 , "b" : 2 },
288
+ False ,
289
+ ),
290
+ (
291
+ "drop_conflicts" ,
292
+ {"a" : 41 , "b" : 42 , "c" : 43 },
293
+ {"b" : 2 , "c" : 43 , "d" : 44 },
294
+ {"a" : 41 , "c" : 43 , "d" : 44 },
295
+ False ,
296
+ ),
297
+ ],
298
+ )
299
+ def test_concat_combine_attrs_kwarg (
300
+ self , combine_attrs , var1_attrs , var2_attrs , expected_attrs , expect_exception
301
+ ):
302
+ ds1 = Dataset ({"a" : ("x" , [0 ])}, coords = {"x" : [0 ]}, attrs = var1_attrs )
303
+ ds2 = Dataset ({"a" : ("x" , [0 ])}, coords = {"x" : [1 ]}, attrs = var2_attrs )
304
+
305
+ if expect_exception :
306
+ with pytest .raises (ValueError , match = f"combine_attrs='{ combine_attrs } '" ):
307
+ concat ([ds1 , ds2 ], dim = "x" , combine_attrs = combine_attrs )
308
+ else :
309
+ actual = concat ([ds1 , ds2 ], dim = "x" , combine_attrs = combine_attrs )
310
+ expected = Dataset (
311
+ {"a" : ("x" , [0 , 0 ])}, {"x" : [0 , 1 ]}, attrs = expected_attrs
312
+ )
278
313
279
- for combine_attrs in expected :
314
+ assert_identical (actual , expected )
315
+
316
+ @pytest .mark .skip (reason = "not implemented, yet (see #4827)" )
317
+ @pytest .mark .parametrize (
318
+ "combine_attrs, attrs1, attrs2, expected_attrs, expect_exception" ,
319
+ [
320
+ (
321
+ "no_conflicts" ,
322
+ {"a" : 1 , "b" : 2 },
323
+ {"a" : 1 , "c" : 3 },
324
+ {"a" : 1 , "b" : 2 , "c" : 3 },
325
+ False ,
326
+ ),
327
+ ("no_conflicts" , {"a" : 1 , "b" : 2 }, {}, {"a" : 1 , "b" : 2 }, False ),
328
+ ("no_conflicts" , {}, {"a" : 1 , "c" : 3 }, {"a" : 1 , "c" : 3 }, False ),
329
+ (
330
+ "no_conflicts" ,
331
+ {"a" : 1 , "b" : 2 },
332
+ {"a" : 4 , "c" : 3 },
333
+ {"a" : 1 , "b" : 2 , "c" : 3 },
334
+ True ,
335
+ ),
336
+ ("drop" , {"a" : 1 , "b" : 2 }, {"a" : 1 , "c" : 3 }, {}, False ),
337
+ ("identical" , {"a" : 1 , "b" : 2 }, {"a" : 1 , "b" : 2 }, {"a" : 1 , "b" : 2 }, False ),
338
+ ("identical" , {"a" : 1 , "b" : 2 }, {"a" : 1 , "c" : 3 }, {"a" : 1 , "b" : 2 }, True ),
339
+ (
340
+ "override" ,
341
+ {"a" : 1 , "b" : 2 },
342
+ {"a" : 4 , "b" : 5 , "c" : 3 },
343
+ {"a" : 1 , "b" : 2 },
344
+ False ,
345
+ ),
346
+ (
347
+ "drop_conflicts" ,
348
+ {"a" : 41 , "b" : 42 , "c" : 43 },
349
+ {"b" : 2 , "c" : 43 , "d" : 44 },
350
+ {"a" : 41 , "c" : 43 , "d" : 44 },
351
+ False ,
352
+ ),
353
+ ],
354
+ )
355
+ def test_concat_combine_attrs_kwarg_variables (
356
+ self , combine_attrs , attrs1 , attrs2 , expected_attrs , expect_exception
357
+ ):
358
+ """check that combine_attrs is used on data variables and coords"""
359
+ ds1 = Dataset ({"a" : ("x" , [0 ], attrs1 )}, coords = {"x" : ("x" , [0 ], attrs1 )})
360
+ ds2 = Dataset ({"a" : ("x" , [0 ], attrs2 )}, coords = {"x" : ("x" , [1 ], attrs2 )})
361
+
362
+ if expect_exception :
363
+ with pytest .raises (ValueError , match = f"combine_attrs='{ combine_attrs } '" ):
364
+ concat ([ds1 , ds2 ], dim = "x" , combine_attrs = combine_attrs )
365
+ else :
280
366
actual = concat ([ds1 , ds2 ], dim = "x" , combine_attrs = combine_attrs )
281
- assert_identical (actual , expected [combine_attrs ])
367
+ expected = Dataset (
368
+ {"a" : ("x" , [0 , 0 ], expected_attrs )},
369
+ {"x" : ("x" , [0 , 1 ], expected_attrs )},
370
+ )
371
+
372
+ assert_identical (actual , expected )
282
373
283
374
def test_concat_promote_shape (self ):
284
375
# mixed dims within variables
0 commit comments