Align objects for merging, recursing into dictionary values. This function is not public API.
(
objects: Iterable[Any],
join: JoinOptions | CombineKwargDefault = "inner",
copy: bool = True,
indexes=None,
exclude: str | Iterable[Hashable] = frozenset(),
raise_on_invalid: bool = True,
fill_value=dtypes.NA,
)
| 969 | |
| 970 | |
| 971 | def deep_align( |
| 972 | objects: Iterable[Any], |
| 973 | join: JoinOptions | CombineKwargDefault = "inner", |
| 974 | copy: bool = True, |
| 975 | indexes=None, |
| 976 | exclude: str | Iterable[Hashable] = frozenset(), |
| 977 | raise_on_invalid: bool = True, |
| 978 | fill_value=dtypes.NA, |
| 979 | ) -> list[Any]: |
| 980 | """Align objects for merging, recursing into dictionary values. |
| 981 | |
| 982 | This function is not public API. |
| 983 | """ |
| 984 | from xarray.core.coordinates import Coordinates |
| 985 | from xarray.core.dataarray import DataArray |
| 986 | from xarray.core.dataset import Dataset |
| 987 | |
| 988 | if indexes is None: |
| 989 | indexes = {} |
| 990 | |
| 991 | def is_alignable(obj): |
| 992 | return isinstance(obj, Coordinates | DataArray | Dataset) |
| 993 | |
| 994 | positions: list[int] = [] |
| 995 | keys: list[type[object] | Hashable] = [] |
| 996 | out: list[Any] = [] |
| 997 | targets: list[Alignable] = [] |
| 998 | no_key: Final = object() |
| 999 | not_replaced: Final = object() |
| 1000 | for position, variables in enumerate(objects): |
| 1001 | if is_alignable(variables): |
| 1002 | positions.append(position) |
| 1003 | keys.append(no_key) |
| 1004 | targets.append(variables) |
| 1005 | out.append(not_replaced) |
| 1006 | elif is_dict_like(variables): |
| 1007 | current_out = {} |
| 1008 | for k, v in variables.items(): |
| 1009 | if is_alignable(v) and k not in indexes: |
| 1010 | # Skip variables in indexes for alignment, because these |
| 1011 | # should to be overwritten instead: |
| 1012 | # https://github.com/pydata/xarray/issues/725 |
| 1013 | # https://github.com/pydata/xarray/issues/3377 |
| 1014 | # TODO(shoyer): doing this here feels super-hacky -- can we |
| 1015 | # move it explicitly into merge instead? |
| 1016 | positions.append(position) |
| 1017 | keys.append(k) |
| 1018 | targets.append(v) |
| 1019 | current_out[k] = not_replaced |
| 1020 | else: |
| 1021 | current_out[k] = v |
| 1022 | out.append(current_out) |
| 1023 | elif raise_on_invalid: |
| 1024 | raise ValueError( |
| 1025 | "object to align is neither an xarray.Dataset, " |
| 1026 | f"an xarray.DataArray nor a dictionary: {variables!r}" |
| 1027 | ) |
| 1028 | else: |
no test coverage detected
searching dependent graphs…