Convert all index coordinates into a :py:class:`pandas.Index`. Parameters ---------- ordered_dims : sequence of hashable, optional Possibly reordered version of this object's dimensions indicating the order in which dimensions should appear on the res
(self, ordered_dims: Sequence[Hashable] | None = None)
| 119 | raise NotImplementedError() |
| 120 | |
| 121 | def to_index(self, ordered_dims: Sequence[Hashable] | None = None) -> pd.Index: |
| 122 | """Convert all index coordinates into a :py:class:`pandas.Index`. |
| 123 | |
| 124 | Parameters |
| 125 | ---------- |
| 126 | ordered_dims : sequence of hashable, optional |
| 127 | Possibly reordered version of this object's dimensions indicating |
| 128 | the order in which dimensions should appear on the result. |
| 129 | |
| 130 | Returns |
| 131 | ------- |
| 132 | pandas.Index |
| 133 | Index subclass corresponding to the outer-product of all dimension |
| 134 | coordinates. This will be a MultiIndex if this object is has more |
| 135 | than more dimension. |
| 136 | """ |
| 137 | if ordered_dims is None: |
| 138 | ordered_dims = list(self.dims) |
| 139 | elif set(ordered_dims) != set(self.dims): |
| 140 | raise ValueError( |
| 141 | "ordered_dims must match dims, but does not: " |
| 142 | f"{ordered_dims} vs {self.dims}" |
| 143 | ) |
| 144 | |
| 145 | if len(ordered_dims) == 0: |
| 146 | raise ValueError("no valid index for a 0-dimensional object") |
| 147 | elif len(ordered_dims) == 1: |
| 148 | (dim,) = ordered_dims |
| 149 | return self._data.get_index(dim) |
| 150 | else: |
| 151 | indexes = [self._data.get_index(k) for k in ordered_dims] |
| 152 | |
| 153 | # compute the sizes of the repeat and tile for the cartesian product |
| 154 | # (taken from pandas.core.reshape.util) |
| 155 | index_lengths = np.fromiter( |
| 156 | (len(index) for index in indexes), dtype=np.intp |
| 157 | ) |
| 158 | cumprod_lengths = np.cumprod(index_lengths) |
| 159 | |
| 160 | if cumprod_lengths[-1] == 0: |
| 161 | # if any factor is empty, the cartesian product is empty |
| 162 | repeat_counts = np.zeros_like(cumprod_lengths) |
| 163 | |
| 164 | else: |
| 165 | # sizes of the repeats |
| 166 | repeat_counts = cumprod_lengths[-1] / cumprod_lengths |
| 167 | # sizes of the tiles |
| 168 | tile_counts = np.roll(cumprod_lengths, 1) |
| 169 | tile_counts[0] = 1 |
| 170 | |
| 171 | # loop over the indexes |
| 172 | # for each MultiIndex or Index compute the cartesian product of the codes |
| 173 | |
| 174 | code_list = [] |
| 175 | level_list = [] |
| 176 | names = [] |
| 177 | |
| 178 | for i, index in enumerate(indexes): |