4
4
5
5
import pandas as pd
6
6
7
- from . import utils
7
+ from . import utils , dtypes
8
8
from .alignment import align
9
9
from .merge import merge
10
10
from .variable import IndexVariable , Variable , as_variable
14
14
15
15
def concat (objs , dim = None , data_vars = 'all' , coords = 'different' ,
16
16
compat = 'equals' , positions = None , indexers = None , mode = None ,
17
- concat_over = None ):
17
+ concat_over = None , fill_value = dtypes . NA ):
18
18
"""Concatenate xarray objects along a new or existing dimension.
19
19
20
20
Parameters
@@ -66,6 +66,8 @@ def concat(objs, dim=None, data_vars='all', coords='different',
66
66
List of integer arrays which specifies the integer positions to which
67
67
to assign each dataset along the concatenated dimension. If not
68
68
supplied, objects are concatenated in the provided order.
69
+ fill_value : scalar, optional
70
+ Value to use for newly missing values
69
71
indexers, mode, concat_over : deprecated
70
72
71
73
Returns
@@ -117,7 +119,7 @@ def concat(objs, dim=None, data_vars='all', coords='different',
117
119
else :
118
120
raise TypeError ('can only concatenate xarray Dataset and DataArray '
119
121
'objects, got %s' % type (first_obj ))
120
- return f (objs , dim , data_vars , coords , compat , positions )
122
+ return f (objs , dim , data_vars , coords , compat , positions , fill_value )
121
123
122
124
123
125
def _calc_concat_dim_coord (dim ):
@@ -212,7 +214,8 @@ def process_subset_opt(opt, subset):
212
214
return concat_over , equals
213
215
214
216
215
- def _dataset_concat (datasets , dim , data_vars , coords , compat , positions ):
217
+ def _dataset_concat (datasets , dim , data_vars , coords , compat , positions ,
218
+ fill_value = dtypes .NA ):
216
219
"""
217
220
Concatenate a sequence of datasets along a new or existing dimension
218
221
"""
@@ -225,7 +228,8 @@ def _dataset_concat(datasets, dim, data_vars, coords, compat, positions):
225
228
dim , coord = _calc_concat_dim_coord (dim )
226
229
# Make sure we're working on a copy (we'll be loading variables)
227
230
datasets = [ds .copy () for ds in datasets ]
228
- datasets = align (* datasets , join = 'outer' , copy = False , exclude = [dim ])
231
+ datasets = align (* datasets , join = 'outer' , copy = False , exclude = [dim ],
232
+ fill_value = fill_value )
229
233
230
234
concat_over , equals = _calc_concat_over (datasets , dim , data_vars , coords )
231
235
@@ -317,7 +321,7 @@ def ensure_common_dims(vars):
317
321
318
322
319
323
def _dataarray_concat (arrays , dim , data_vars , coords , compat ,
320
- positions ):
324
+ positions , fill_value = dtypes . NA ):
321
325
arrays = list (arrays )
322
326
323
327
if data_vars != 'all' :
@@ -336,14 +340,15 @@ def _dataarray_concat(arrays, dim, data_vars, coords, compat,
336
340
datasets .append (arr ._to_temp_dataset ())
337
341
338
342
ds = _dataset_concat (datasets , dim , data_vars , coords , compat ,
339
- positions )
343
+ positions , fill_value )
340
344
result = arrays [0 ]._from_temp_dataset (ds , name )
341
345
342
346
result .name = result_name (arrays )
343
347
return result
344
348
345
349
346
- def _auto_concat (datasets , dim = None , data_vars = 'all' , coords = 'different' ):
350
+ def _auto_concat (datasets , dim = None , data_vars = 'all' , coords = 'different' ,
351
+ fill_value = dtypes .NA ):
347
352
if len (datasets ) == 1 and dim is None :
348
353
# There is nothing more to combine, so kick out early.
349
354
return datasets [0 ]
@@ -366,7 +371,8 @@ def _auto_concat(datasets, dim=None, data_vars='all', coords='different'):
366
371
'supply the ``concat_dim`` argument '
367
372
'explicitly' )
368
373
dim , = concat_dims
369
- return concat (datasets , dim = dim , data_vars = data_vars , coords = coords )
374
+ return concat (datasets , dim = dim , data_vars = data_vars ,
375
+ coords = coords , fill_value = fill_value )
370
376
371
377
372
378
_CONCAT_DIM_DEFAULT = utils .ReprObject ('<inferred>' )
@@ -442,7 +448,8 @@ def _check_shape_tile_ids(combined_tile_ids):
442
448
443
449
444
450
def _combine_nd (combined_ids , concat_dims , data_vars = 'all' ,
445
- coords = 'different' , compat = 'no_conflicts' ):
451
+ coords = 'different' , compat = 'no_conflicts' ,
452
+ fill_value = dtypes .NA ):
446
453
"""
447
454
Concatenates and merges an N-dimensional structure of datasets.
448
455
@@ -472,13 +479,14 @@ def _combine_nd(combined_ids, concat_dims, data_vars='all',
472
479
dim = concat_dim ,
473
480
data_vars = data_vars ,
474
481
coords = coords ,
475
- compat = compat )
482
+ compat = compat ,
483
+ fill_value = fill_value )
476
484
combined_ds = list (combined_ids .values ())[0 ]
477
485
return combined_ds
478
486
479
487
480
488
def _auto_combine_all_along_first_dim (combined_ids , dim , data_vars ,
481
- coords , compat ):
489
+ coords , compat , fill_value = dtypes . NA ):
482
490
# Group into lines of datasets which must be combined along dim
483
491
# need to sort by _new_tile_id first for groupby to work
484
492
# TODO remove all these sorted OrderedDicts once python >= 3.6 only
@@ -490,7 +498,8 @@ def _auto_combine_all_along_first_dim(combined_ids, dim, data_vars,
490
498
combined_ids = OrderedDict (sorted (group ))
491
499
datasets = combined_ids .values ()
492
500
new_combined_ids [new_id ] = _auto_combine_1d (datasets , dim , compat ,
493
- data_vars , coords )
501
+ data_vars , coords ,
502
+ fill_value )
494
503
return new_combined_ids
495
504
496
505
@@ -500,18 +509,20 @@ def vars_as_keys(ds):
500
509
501
510
def _auto_combine_1d (datasets , concat_dim = _CONCAT_DIM_DEFAULT ,
502
511
compat = 'no_conflicts' ,
503
- data_vars = 'all' , coords = 'different' ):
512
+ data_vars = 'all' , coords = 'different' ,
513
+ fill_value = dtypes .NA ):
504
514
# This is just the old auto_combine function (which only worked along 1D)
505
515
if concat_dim is not None :
506
516
dim = None if concat_dim is _CONCAT_DIM_DEFAULT else concat_dim
507
517
sorted_datasets = sorted (datasets , key = vars_as_keys )
508
518
grouped_by_vars = itertools .groupby (sorted_datasets , key = vars_as_keys )
509
519
concatenated = [_auto_concat (list (ds_group ), dim = dim ,
510
- data_vars = data_vars , coords = coords )
520
+ data_vars = data_vars , coords = coords ,
521
+ fill_value = fill_value )
511
522
for id , ds_group in grouped_by_vars ]
512
523
else :
513
524
concatenated = datasets
514
- merged = merge (concatenated , compat = compat )
525
+ merged = merge (concatenated , compat = compat , fill_value = fill_value )
515
526
return merged
516
527
517
528
@@ -521,7 +532,7 @@ def _new_tile_id(single_id_ds_pair):
521
532
522
533
523
534
def _auto_combine (datasets , concat_dims , compat , data_vars , coords ,
524
- infer_order_from_coords , ids ):
535
+ infer_order_from_coords , ids , fill_value = dtypes . NA ):
525
536
"""
526
537
Calls logic to decide concatenation order before concatenating.
527
538
"""
@@ -550,12 +561,14 @@ def _auto_combine(datasets, concat_dims, compat, data_vars, coords,
550
561
551
562
# Repeatedly concatenate then merge along each dimension
552
563
combined = _combine_nd (combined_ids , concat_dims , compat = compat ,
553
- data_vars = data_vars , coords = coords )
564
+ data_vars = data_vars , coords = coords ,
565
+ fill_value = fill_value )
554
566
return combined
555
567
556
568
557
569
def auto_combine (datasets , concat_dim = _CONCAT_DIM_DEFAULT ,
558
- compat = 'no_conflicts' , data_vars = 'all' , coords = 'different' ):
570
+ compat = 'no_conflicts' , data_vars = 'all' , coords = 'different' ,
571
+ fill_value = dtypes .NA ):
559
572
"""Attempt to auto-magically combine the given datasets into one.
560
573
This method attempts to combine a list of datasets into a single entity by
561
574
inspecting metadata and using a combination of concat and merge.
@@ -596,6 +609,8 @@ def auto_combine(datasets, concat_dim=_CONCAT_DIM_DEFAULT,
596
609
Details are in the documentation of concat
597
610
coords : {'minimal', 'different', 'all' or list of str}, optional
598
611
Details are in the documentation of conca
612
+ fill_value : scalar, optional
613
+ Value to use for newly missing values
599
614
600
615
Returns
601
616
-------
@@ -622,4 +637,4 @@ def auto_combine(datasets, concat_dim=_CONCAT_DIM_DEFAULT,
622
637
return _auto_combine (datasets , concat_dims = concat_dims , compat = compat ,
623
638
data_vars = data_vars , coords = coords ,
624
639
infer_order_from_coords = infer_order_from_coords ,
625
- ids = False )
640
+ ids = False , fill_value = fill_value )
0 commit comments