MCPcopy
hub / github.com/pydata/xarray / merge_attrs

Function merge_attrs

xarray/structure/merge.py:636–685  ·  view source on GitHub ↗

Combine attributes from different variables according to combine_attrs

(variable_attrs, combine_attrs, context=None)

Source from the content-addressed store, hash-verified

634
635
636def merge_attrs(variable_attrs, combine_attrs, context=None):
637 """Combine attributes from different variables according to combine_attrs"""
638 if not variable_attrs:
639 # no attributes to merge
640 return None
641
642 if callable(combine_attrs):
643 return combine_attrs(variable_attrs, context=context)
644 elif combine_attrs == "drop":
645 return {}
646 elif combine_attrs == "override":
647 return dict(variable_attrs[0])
648 elif combine_attrs == "no_conflicts":
649 result = dict(variable_attrs[0])
650 for attrs in variable_attrs[1:]:
651 try:
652 result = compat_dict_union(result, attrs)
653 except ValueError as e:
654 raise MergeError(
655 "combine_attrs='no_conflicts', but some values are not "
656 f"the same. Merging {result} with {attrs}"
657 ) from e
658 return result
659 elif combine_attrs == "drop_conflicts":
660 result = {}
661 dropped_keys = set()
662
663 for attrs in variable_attrs:
664 for key, value in attrs.items():
665 if key in dropped_keys:
666 continue
667
668 if key not in result:
669 result[key] = value
670 elif not equivalent_attrs(result[key], value):
671 del result[key]
672 dropped_keys.add(key)
673
674 return result
675 elif combine_attrs == "identical":
676 result = dict(variable_attrs[0])
677 for attrs in variable_attrs[1:]:
678 if not dict_equiv(result, attrs):
679 raise MergeError(
680 f"combine_attrs='identical', but attrs differ. First is {result} "
681 f", other is {attrs}."
682 )
683 return result
684 else:
685 raise ValueError(f"Unrecognised value for combine_attrs={combine_attrs}")
686
687
688class _MergeResult(NamedTuple):

Callers 11

_binary_opMethod · 0.90
concatMethod · 0.90
_binary_opMethod · 0.90
concatMethod · 0.90
_dataset_concatFunction · 0.90
_dataarray_concatFunction · 0.90
apply_dataarray_vfuncFunction · 0.90
apply_dataset_vfuncFunction · 0.90
apply_variable_ufuncFunction · 0.90
merge_collectedFunction · 0.85
merge_coreFunction · 0.85

Calls 6

compat_dict_unionFunction · 0.90
dict_equivFunction · 0.90
MergeErrorClass · 0.85
equivalent_attrsFunction · 0.85
itemsMethod · 0.80
addMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…