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

Function reindex_variables

xarray/structure/alignment.py:43–95  ·  view source on GitHub ↗

Conform a dictionary of variables onto a new set of variables reindexed with dimension positional indexers and possibly filled with missing values. Not public API.

(
    variables: Mapping[Any, Variable],
    dim_pos_indexers: Mapping[Any, Any],
    copy: bool = True,
    fill_value: Any = dtypes.NA,
    sparse: bool = False,
)

Source from the content-addressed store, hash-verified

41
42
43def reindex_variables(
44 variables: Mapping[Any, Variable],
45 dim_pos_indexers: Mapping[Any, Any],
46 copy: bool = True,
47 fill_value: Any = dtypes.NA,
48 sparse: bool = False,
49) -> dict[Hashable, Variable]:
50 """Conform a dictionary of variables onto a new set of variables reindexed
51 with dimension positional indexers and possibly filled with missing values.
52
53 Not public API.
54
55 """
56 new_variables = {}
57 dim_sizes = calculate_dimensions(variables)
58
59 masked_dims = set()
60 unchanged_dims = set()
61 for dim, indxr in dim_pos_indexers.items():
62 # Negative values in dim_pos_indexers mean values missing in the new index
63 # See ``Index.reindex_like``.
64 if (indxr < 0).any():
65 masked_dims.add(dim)
66 elif np.array_equal(indxr, np.arange(dim_sizes.get(dim, 0))):
67 unchanged_dims.add(dim)
68
69 for name, var in variables.items():
70 if isinstance(fill_value, dict):
71 fill_value_ = fill_value.get(name, dtypes.NA)
72 else:
73 fill_value_ = fill_value
74
75 if sparse:
76 var = var._as_sparse(fill_value=fill_value_)
77 indxr = tuple(
78 slice(None) if d in unchanged_dims else dim_pos_indexers.get(d, slice(None))
79 for d in var.dims
80 )
81 needs_masking = any(d in masked_dims for d in var.dims)
82
83 if needs_masking:
84 new_var = var._getitem_with_mask(indxr, fill_value=fill_value_)
85 elif all(is_full_slice(k) for k in indxr):
86 # no reindexing necessary
87 # here we need to manually deal with copying data, since
88 # we neither created a new ndarray nor used fancy indexing
89 new_var = var.copy(deep=copy)
90 else:
91 new_var = var[indxr]
92
93 new_variables[name] = new_var
94
95 return new_variables
96
97
98def _normalize_indexes(

Callers 1

_dataset_concatFunction · 0.90

Calls 10

calculate_dimensionsFunction · 0.90
is_full_sliceFunction · 0.90
itemsMethod · 0.80
arangeMethod · 0.80
_getitem_with_maskMethod · 0.80
anyMethod · 0.45
addMethod · 0.45
getMethod · 0.45
_as_sparseMethod · 0.45
copyMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…