Set DataArray (multi-)indexes using one or more existing coordinates. This legacy method is limited to pandas (multi-)indexes and 1-dimensional "dimension" coordinates. See :py:meth:`~DataArray.set_xindex` for setting a pandas or a custom Xarray-compatible in
(
self,
indexes: Mapping[Any, Hashable | Sequence[Hashable]] | None = None,
append: bool = False,
**indexes_kwargs: Hashable | Sequence[Hashable],
)
| 2768 | return self._from_temp_dataset(ds) |
| 2769 | |
| 2770 | def set_index( |
| 2771 | self, |
| 2772 | indexes: Mapping[Any, Hashable | Sequence[Hashable]] | None = None, |
| 2773 | append: bool = False, |
| 2774 | **indexes_kwargs: Hashable | Sequence[Hashable], |
| 2775 | ) -> Self: |
| 2776 | """Set DataArray (multi-)indexes using one or more existing |
| 2777 | coordinates. |
| 2778 | |
| 2779 | This legacy method is limited to pandas (multi-)indexes and |
| 2780 | 1-dimensional "dimension" coordinates. See |
| 2781 | :py:meth:`~DataArray.set_xindex` for setting a pandas or a custom |
| 2782 | Xarray-compatible index from one or more arbitrary coordinates. |
| 2783 | |
| 2784 | Parameters |
| 2785 | ---------- |
| 2786 | indexes : {dim: index, ...} |
| 2787 | Mapping from names matching dimensions and values given |
| 2788 | by (lists of) the names of existing coordinates or variables to set |
| 2789 | as new (multi-)index. |
| 2790 | append : bool, default: False |
| 2791 | If True, append the supplied index(es) to the existing index(es). |
| 2792 | Otherwise replace the existing index(es). |
| 2793 | **indexes_kwargs : optional |
| 2794 | The keyword arguments form of ``indexes``. |
| 2795 | One of indexes or indexes_kwargs must be provided. |
| 2796 | |
| 2797 | Returns |
| 2798 | ------- |
| 2799 | obj : DataArray |
| 2800 | Another DataArray, with this data but replaced coordinates. |
| 2801 | |
| 2802 | Examples |
| 2803 | -------- |
| 2804 | >>> arr = xr.DataArray( |
| 2805 | ... data=np.ones((2, 3)), |
| 2806 | ... dims=["x", "y"], |
| 2807 | ... coords={"x": range(2), "y": range(3), "a": ("x", [3, 4])}, |
| 2808 | ... ) |
| 2809 | >>> arr |
| 2810 | <xarray.DataArray (x: 2, y: 3)> Size: 48B |
| 2811 | array([[1., 1., 1.], |
| 2812 | [1., 1., 1.]]) |
| 2813 | Coordinates: |
| 2814 | * x (x) int64 16B 0 1 |
| 2815 | a (x) int64 16B 3 4 |
| 2816 | * y (y) int64 24B 0 1 2 |
| 2817 | >>> arr.set_index(x="a") |
| 2818 | <xarray.DataArray (x: 2, y: 3)> Size: 48B |
| 2819 | array([[1., 1., 1.], |
| 2820 | [1., 1., 1.]]) |
| 2821 | Coordinates: |
| 2822 | * x (x) int64 16B 3 4 |
| 2823 | * y (y) int64 24B 0 1 2 |
| 2824 | |
| 2825 | See Also |
| 2826 | -------- |
| 2827 | DataArray.reset_index |