Skip to content

Commit a996655

Browse files
committed
Update concat for multi-variable indexes.
When concatenating along a dimension associated with a multi-variable index, avoid merging the other dimensions. Instead we rely on a strict equality check along those dimensions (7551a7a)
1 parent 303748c commit a996655

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

xarray/structure/concat.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -324,9 +324,15 @@ def _calc_concat_over(datasets, dim, dim_names, data_vars: T_DataVars, coords, c
324324
"""
325325
Determine which dataset variables need to be concatenated in the result,
326326
"""
327-
# Return values
327+
# variables to be concatenated
328328
concat_over = set()
329+
# variables checked for equality
329330
equals = {}
331+
# skip merging these variables.
332+
# if concatenating over a dimension 'x' that is associated with an indexo ver 2 variables,
333+
# 'x' and 'y', then we assert join="equals" on `y` and don't need to merge it.
334+
# that assertion happens in the align step prior to this function being called
335+
skip_merge = set()
330336

331337
if dim in dim_names:
332338
concat_over_existing_dim = True
@@ -341,6 +347,9 @@ def _calc_concat_over(datasets, dim, dim_names, data_vars: T_DataVars, coords, c
341347
if dim in ds:
342348
ds = ds.set_coords(dim)
343349
concat_over.update(k for k, v in ds.variables.items() if dim in v.dims)
350+
for index, idx_coords in ds.xindexes.group_by_index():
351+
if dim in idx_coords:
352+
skip_merge.update(idx_coords.keys())
344353
concat_dim_lengths.append(ds.sizes.get(dim, 1))
345354

346355
def process_subset_opt(opt, subset):
@@ -438,7 +447,7 @@ def process_subset_opt(opt, subset):
438447

439448
process_subset_opt(data_vars, "data_vars")
440449
process_subset_opt(coords, "coords")
441-
return concat_over, equals, concat_dim_lengths
450+
return concat_over, equals, concat_dim_lengths, skip_merge
442451

443452

444453
# determine dimensional coordinate names and a dict mapping name to DataArray
@@ -542,12 +551,12 @@ def _dataset_concat(
542551
]
543552

544553
# determine which variables to concatenate
545-
concat_over, equals, concat_dim_lengths = _calc_concat_over(
554+
concat_over, equals, concat_dim_lengths, skip_merge = _calc_concat_over(
546555
datasets, dim_name, dim_names, data_vars, coords, compat
547556
)
548557

549558
# determine which variables to merge, and then merge them according to compat
550-
variables_to_merge = (coord_names | data_names) - concat_over
559+
variables_to_merge = (coord_names | data_names) - concat_over - skip_merge
551560

552561
result_vars = {}
553562
result_indexes = {}

0 commit comments

Comments
 (0)