Concatenate a sequence of datasets along a new or existing dimension
(
datasets: Iterable[T_Dataset],
dim: Hashable | T_Variable | T_DataArray | pd.Index,
data_vars: T_DataVars | CombineKwargDefault,
coords: ConcatOptions | Iterable[Hashable] | CombineKwargDefault,
compat: CompatOptions | CombineKwargDefault,
positions: Iterable[Iterable[int]] | None,
fill_value: Any,
join: JoinOptions | CombineKwargDefault,
combine_attrs: CombineAttrsOptions,
create_index_for_new_dim: bool,
*,
preexisting_dim: bool = False,
)
| 603 | |
| 604 | |
| 605 | def _dataset_concat( |
| 606 | datasets: Iterable[T_Dataset], |
| 607 | dim: Hashable | T_Variable | T_DataArray | pd.Index, |
| 608 | data_vars: T_DataVars | CombineKwargDefault, |
| 609 | coords: ConcatOptions | Iterable[Hashable] | CombineKwargDefault, |
| 610 | compat: CompatOptions | CombineKwargDefault, |
| 611 | positions: Iterable[Iterable[int]] | None, |
| 612 | fill_value: Any, |
| 613 | join: JoinOptions | CombineKwargDefault, |
| 614 | combine_attrs: CombineAttrsOptions, |
| 615 | create_index_for_new_dim: bool, |
| 616 | *, |
| 617 | preexisting_dim: bool = False, |
| 618 | ) -> T_Dataset: |
| 619 | """ |
| 620 | Concatenate a sequence of datasets along a new or existing dimension |
| 621 | """ |
| 622 | from xarray.core.dataarray import DataArray |
| 623 | from xarray.core.dataset import Dataset |
| 624 | |
| 625 | datasets = list(datasets) |
| 626 | |
| 627 | if not all(isinstance(dataset, Dataset) for dataset in datasets): |
| 628 | raise TypeError( |
| 629 | "The elements in the input list need to be either all 'Dataset's or all 'DataArray's" |
| 630 | ) |
| 631 | |
| 632 | dim_var: Variable | None |
| 633 | if isinstance(dim, DataArray): |
| 634 | dim_var = dim.variable |
| 635 | elif isinstance(dim, Variable): |
| 636 | dim_var = dim |
| 637 | else: |
| 638 | dim_var = None |
| 639 | |
| 640 | dim_name, index = _calc_concat_dim_index(dim) |
| 641 | |
| 642 | # Make sure we're working on a copy (we'll be loading variables) |
| 643 | datasets = [ds.copy() for ds in datasets] |
| 644 | datasets = list( |
| 645 | align( |
| 646 | *datasets, join=join, copy=False, exclude=[dim_name], fill_value=fill_value |
| 647 | ) |
| 648 | ) |
| 649 | |
| 650 | all_dims, dim_coords, dims_sizes, coord_names, data_names, vars_order = ( |
| 651 | _parse_datasets(datasets) |
| 652 | ) |
| 653 | if preexisting_dim: |
| 654 | # When concatenating DataTree objects, a dimension may be pre-existing |
| 655 | # because it exists elsewhere on the trees, even if it does not exist |
| 656 | # on the dataset objects at this node. |
| 657 | all_dims.add(dim_name) |
| 658 | indexed_dim_names = set(dim_coords) |
| 659 | |
| 660 | both_data_and_coords = coord_names & data_names |
| 661 | if both_data_and_coords: |
| 662 | raise ValueError( |
no test coverage detected
searching dependent graphs…