Skip to content
forked from pydata/xarray

Commit 29396fe

Browse files
committed
Begin support for combine, merge.
1 parent 0d1825e commit 29396fe

File tree

2 files changed

+24
-15
lines changed

2 files changed

+24
-15
lines changed

xarray/core/combine.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,7 @@ def _combine_1d(
243243
dim=concat_dim,
244244
data_vars=data_vars,
245245
coords=coords,
246+
compat=compat,
246247
fill_value=fill_value,
247248
join=join,
248249
)
@@ -592,6 +593,7 @@ def combine_by_coords(
592593
concat_dims=concat_dims,
593594
data_vars=data_vars,
594595
coords=coords,
596+
compat=compat,
595597
fill_value=fill_value,
596598
join=join,
597599
)
@@ -823,6 +825,7 @@ def _old_auto_combine(
823825
dim=dim,
824826
data_vars=data_vars,
825827
coords=coords,
828+
compat=compat,
826829
fill_value=fill_value,
827830
join=join,
828831
)
@@ -841,6 +844,7 @@ def _auto_concat(
841844
coords="different",
842845
fill_value=dtypes.NA,
843846
join="outer",
847+
compat="no_conflicts"
844848
):
845849
if len(datasets) == 1 and dim is None:
846850
# There is nothing more to combine, so kick out early.
@@ -867,5 +871,6 @@ def _auto_concat(
867871
)
868872
dim, = concat_dims
869873
return concat(
870-
datasets, dim=dim, data_vars=data_vars, coords=coords, fill_value=fill_value
874+
datasets, dim=dim, data_vars=data_vars, coords=coords, fill_value=fill_value,
875+
compat=compat
871876
)

xarray/core/merge.py

+18-14
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
"broadcast_equals": 2,
4545
"minimal": 3,
4646
"no_conflicts": 4,
47+
"override": 5,
4748
}
4849
)
4950

@@ -81,7 +82,7 @@ def unique_variable(name, variables, compat="broadcast_equals"):
8182
variables : list of xarray.Variable
8283
List of Variable objects, all of which go by the same name in different
8384
inputs.
84-
compat : {'identical', 'equals', 'broadcast_equals', 'no_conflicts'}, optional
85+
compat : {'identical', 'equals', 'broadcast_equals', 'no_conflicts', 'override'}, optional
8586
Type of equality check to use.
8687
8788
Returns
@@ -106,17 +107,20 @@ def unique_variable(name, variables, compat="broadcast_equals"):
106107
if compat == "no_conflicts":
107108
combine_method = "fillna"
108109

109-
for var in variables[1:]:
110-
if not getattr(out, compat)(var):
111-
raise MergeError(
112-
"conflicting values for variable %r on "
113-
"objects to be combined:\n"
114-
"first value: %r\nsecond value: %r" % (name, out, var)
115-
)
116-
if combine_method:
117-
# TODO: add preservation of attrs into fillna
118-
out = getattr(out, combine_method)(var)
119-
out.attrs = var.attrs
110+
if compat == "override":
111+
out = variables[0]
112+
else:
113+
for var in variables[1:]:
114+
if not getattr(out, compat)(var):
115+
raise MergeError(
116+
"conflicting values for variable %r on "
117+
"objects to be combined:\n"
118+
"first value: %r\nsecond value: %r" % (name, out, var)
119+
)
120+
if combine_method:
121+
# TODO: add preservation of attrs into fillna
122+
out = getattr(out, combine_method)(var)
123+
out.attrs = var.attrs
120124

121125
return out
122126

@@ -152,7 +156,7 @@ def merge_variables(
152156
priority_vars : mapping with Variable or None values, optional
153157
If provided, variables are always taken from this dict in preference to
154158
the input variable dictionaries, without checking for conflicts.
155-
compat : {'identical', 'equals', 'broadcast_equals', 'minimal', 'no_conflicts'}, optional
159+
compat : {'identical', 'equals', 'broadcast_equals', 'minimal', 'no_conflicts', 'override'}, optional
156160
Type of equality check to use when checking for conflicts.
157161
158162
Returns
@@ -449,7 +453,7 @@ def merge_core(
449453
----------
450454
objs : list of mappings
451455
All values must be convertable to labeled arrays.
452-
compat : {'identical', 'equals', 'broadcast_equals', 'no_conflicts'}, optional
456+
compat : {'identical', 'equals', 'broadcast_equals', 'no_conflicts', 'override'}, optional
453457
Compatibility checks to use when merging variables.
454458
join : {'outer', 'inner', 'left', 'right'}, optional
455459
How to combine objects with different indexes.

0 commit comments

Comments
 (0)