Return a new dictionary with copies of indexes, preserving unique indexes. Parameters ---------- deep : bool, default: True Whether the indexes are deep or shallow copied onto the new object. memo : dict if object id to copied objects or None, opt
(
self, deep: bool = True, memo: dict[int, T_PandasOrXarrayIndex] | None = None
)
| 1895 | return Indexes(indexes, self._variables, index_type=pd.Index) |
| 1896 | |
| 1897 | def copy_indexes( |
| 1898 | self, deep: bool = True, memo: dict[int, T_PandasOrXarrayIndex] | None = None |
| 1899 | ) -> tuple[dict[Hashable, T_PandasOrXarrayIndex], dict[Hashable, Variable]]: |
| 1900 | """Return a new dictionary with copies of indexes, preserving |
| 1901 | unique indexes. |
| 1902 | |
| 1903 | Parameters |
| 1904 | ---------- |
| 1905 | deep : bool, default: True |
| 1906 | Whether the indexes are deep or shallow copied onto the new object. |
| 1907 | memo : dict if object id to copied objects or None, optional |
| 1908 | To prevent infinite recursion deepcopy stores all copied elements |
| 1909 | in this dict. |
| 1910 | |
| 1911 | """ |
| 1912 | new_indexes: dict[Hashable, T_PandasOrXarrayIndex] = {} |
| 1913 | new_index_vars: dict[Hashable, Variable] = {} |
| 1914 | |
| 1915 | xr_idx: Index |
| 1916 | new_idx: T_PandasOrXarrayIndex |
| 1917 | for idx, coords in self.group_by_index(): |
| 1918 | if isinstance(idx, pd.Index): |
| 1919 | convert_new_idx = True |
| 1920 | dim = next(iter(coords.values())).dims[0] |
| 1921 | if isinstance(idx, pd.MultiIndex): |
| 1922 | xr_idx = PandasMultiIndex(idx, dim) |
| 1923 | else: |
| 1924 | xr_idx = PandasIndex(idx, dim) |
| 1925 | else: |
| 1926 | convert_new_idx = False |
| 1927 | xr_idx = idx |
| 1928 | |
| 1929 | new_idx = xr_idx._copy(deep=deep, memo=memo) # type: ignore[assignment] |
| 1930 | idx_vars = xr_idx.create_variables(coords) |
| 1931 | |
| 1932 | if convert_new_idx: |
| 1933 | new_idx = new_idx.index # type: ignore[attr-defined] |
| 1934 | |
| 1935 | new_indexes.update(dict.fromkeys(coords, new_idx)) |
| 1936 | new_index_vars.update(idx_vars) |
| 1937 | |
| 1938 | return new_indexes, new_index_vars |
| 1939 | |
| 1940 | def __iter__(self) -> Iterator[T_PandasOrXarrayIndex]: |
| 1941 | return iter(self._indexes) |