Normalize the indexes/indexers given for re-indexing or alignment. Wrap any arbitrary array or `pandas.Index` as an Xarray `PandasIndex` associated with its corresponding dimension coordinate variable.
(
indexes: Mapping[Any, Any | T_DuckArray],
)
| 96 | |
| 97 | |
| 98 | def _normalize_indexes( |
| 99 | indexes: Mapping[Any, Any | T_DuckArray], |
| 100 | ) -> Indexes: |
| 101 | """Normalize the indexes/indexers given for re-indexing or alignment. |
| 102 | |
| 103 | Wrap any arbitrary array or `pandas.Index` as an Xarray `PandasIndex` |
| 104 | associated with its corresponding dimension coordinate variable. |
| 105 | |
| 106 | """ |
| 107 | xr_indexes: dict[Hashable, Index] = {} |
| 108 | xr_variables: dict[Hashable, Variable] |
| 109 | |
| 110 | if isinstance(indexes, Indexes): |
| 111 | xr_variables = dict(indexes.variables) |
| 112 | else: |
| 113 | xr_variables = {} |
| 114 | |
| 115 | for k, idx in indexes.items(): |
| 116 | if not isinstance(idx, Index): |
| 117 | if getattr(idx, "dims", (k,)) != (k,): |
| 118 | raise AlignmentError( |
| 119 | f"Indexer has dimensions {idx.dims} that are different " |
| 120 | f"from that to be indexed along '{k}'" |
| 121 | ) |
| 122 | data: T_DuckArray = as_compatible_data(idx) |
| 123 | pd_idx = safe_cast_to_index(data) |
| 124 | if pd_idx.name != k: |
| 125 | pd_idx = pd_idx.copy() |
| 126 | pd_idx.name = k |
| 127 | if isinstance(pd_idx, pd.MultiIndex): |
| 128 | idx = PandasMultiIndex(pd_idx, k) |
| 129 | else: |
| 130 | idx = PandasIndex(pd_idx, k, coord_dtype=data.dtype) |
| 131 | xr_variables.update(idx.create_variables()) |
| 132 | xr_indexes[k] = idx |
| 133 | |
| 134 | return Indexes(xr_indexes, xr_variables) |
| 135 | |
| 136 | |
| 137 | CoordNamesAndDims = tuple[tuple[Hashable, tuple[Hashable, ...]], ...] |
no test coverage detected
searching dependent graphs…