Assign new coordinates (and indexes) to a Coordinates object, returning a new object with all the original coordinates in addition to the new ones. Parameters ---------- coords : mapping of dim to coord, optional A mapping whose keys are the names of the
(self, coords: Mapping | None = None, **coords_kwargs: Any)
| 654 | self._update_coords(coords, indexes) |
| 655 | |
| 656 | def assign(self, coords: Mapping | None = None, **coords_kwargs: Any) -> Self: |
| 657 | """Assign new coordinates (and indexes) to a Coordinates object, returning |
| 658 | a new object with all the original coordinates in addition to the new ones. |
| 659 | |
| 660 | Parameters |
| 661 | ---------- |
| 662 | coords : mapping of dim to coord, optional |
| 663 | A mapping whose keys are the names of the coordinates and values are the |
| 664 | coordinates to assign. The mapping will generally be a dict or |
| 665 | :class:`Coordinates`. |
| 666 | |
| 667 | * If a value is a standard data value — for example, a ``DataArray``, |
| 668 | scalar, or array — the data is simply assigned as a coordinate. |
| 669 | |
| 670 | * A coordinate can also be defined and attached to an existing dimension |
| 671 | using a tuple with the first element the dimension name and the second |
| 672 | element the values for this new coordinate. |
| 673 | |
| 674 | **coords_kwargs |
| 675 | The keyword arguments form of ``coords``. |
| 676 | One of ``coords`` or ``coords_kwargs`` must be provided. |
| 677 | |
| 678 | Returns |
| 679 | ------- |
| 680 | new_coords : Coordinates |
| 681 | A new Coordinates object with the new coordinates (and indexes) |
| 682 | in addition to all the existing coordinates. |
| 683 | |
| 684 | Examples |
| 685 | -------- |
| 686 | >>> coords = xr.Coordinates() |
| 687 | >>> coords |
| 688 | Coordinates: |
| 689 | *empty* |
| 690 | |
| 691 | >>> coords.assign(x=[1, 2]) |
| 692 | Coordinates: |
| 693 | * x (x) int64 16B 1 2 |
| 694 | |
| 695 | >>> midx = pd.MultiIndex.from_product([["a", "b"], [0, 1]]) |
| 696 | >>> coords.assign(xr.Coordinates.from_pandas_multiindex(midx, "y")) |
| 697 | Coordinates: |
| 698 | * y (y) object 32B MultiIndex |
| 699 | * y_level_0 (y) object 32B 'a' 'a' 'b' 'b' |
| 700 | * y_level_1 (y) int64 32B 0 1 0 1 |
| 701 | |
| 702 | """ |
| 703 | # TODO: this doesn't support a callable, which is inconsistent with `DataArray.assign_coords` |
| 704 | coords = either_dict_or_kwargs(coords, coords_kwargs, "assign") |
| 705 | new_coords = self.copy() |
| 706 | new_coords.update(coords) |
| 707 | return new_coords |
| 708 | |
| 709 | def _overwrite_indexes( |
| 710 | self, |