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

Function _normalize_indexes

xarray/structure/alignment.py:98–134  ·  view source on GitHub ↗

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],
)

Source from the content-addressed store, hash-verified

96
97
98def _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
137CoordNamesAndDims = tuple[tuple[Hashable, tuple[Hashable, ...]], ...]

Callers 1

__init__Method · 0.85

Calls 10

create_variablesMethod · 0.95
as_compatible_dataFunction · 0.90
safe_cast_to_indexFunction · 0.90
PandasMultiIndexClass · 0.90
PandasIndexClass · 0.90
IndexesClass · 0.90
AlignmentErrorClass · 0.85
itemsMethod · 0.80
copyMethod · 0.45
updateMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…