|
1 | 1 | from __future__ import annotations
|
2 | 2 |
|
3 |
| -from collections.abc import Hashable, Iterator, Mapping, Sequence |
| 3 | +from collections.abc import Callable, Hashable, Iterable, Iterator, Mapping, Sequence |
4 | 4 | from contextlib import contextmanager
|
5 | 5 | from typing import (
|
6 | 6 | TYPE_CHECKING,
|
|
21 | 21 | assert_no_index_corrupted,
|
22 | 22 | create_default_index_implicit,
|
23 | 23 | )
|
24 |
| -from xarray.core.types import DataVars, Self, T_DataArray, T_Xarray |
| 24 | +from xarray.core.types import DataVars, ErrorOptions, Self, T_DataArray, T_Xarray |
25 | 25 | from xarray.core.utils import (
|
26 | 26 | Frozen,
|
27 | 27 | ReprObject,
|
@@ -561,6 +561,35 @@ def merge(self, other: Mapping[Any, Any] | None) -> Dataset:
|
561 | 561 | variables=coords, coord_names=coord_names, indexes=indexes
|
562 | 562 | )
|
563 | 563 |
|
| 564 | + def __or__(self, other: Mapping[Any, Any] | None) -> Coordinates: |
| 565 | + """Merge two sets of coordinates to create a new Coordinates object |
| 566 | +
|
| 567 | + The method implements the logic used for joining coordinates in the |
| 568 | + result of a binary operation performed on xarray objects: |
| 569 | +
|
| 570 | + - If two index coordinates conflict (are not equal), an exception is |
| 571 | + raised. You must align your data before passing it to this method. |
| 572 | + - If an index coordinate and a non-index coordinate conflict, the non- |
| 573 | + index coordinate is dropped. |
| 574 | + - If two non-index coordinates conflict, both are dropped. |
| 575 | +
|
| 576 | + Parameters |
| 577 | + ---------- |
| 578 | + other : dict-like, optional |
| 579 | + A :py:class:`Coordinates` object or any mapping that can be turned |
| 580 | + into coordinates. |
| 581 | +
|
| 582 | + Returns |
| 583 | + ------- |
| 584 | + merged : Coordinates |
| 585 | + A new Coordinates object with merged coordinates. |
| 586 | +
|
| 587 | + See Also |
| 588 | + -------- |
| 589 | + Coordinates.merge |
| 590 | + """ |
| 591 | + return self.merge(other).coords |
| 592 | + |
564 | 593 | def __setitem__(self, key: Hashable, value: Any) -> None:
|
565 | 594 | self.update({key: value})
|
566 | 595 |
|
@@ -719,6 +748,108 @@ def copy(
|
719 | 748 | ),
|
720 | 749 | )
|
721 | 750 |
|
| 751 | + def drop_vars( |
| 752 | + self, |
| 753 | + names: str |
| 754 | + | Iterable[Hashable] |
| 755 | + | Callable[ |
| 756 | + [Coordinates | Dataset | DataArray | DataTree], |
| 757 | + str | Iterable[Hashable], |
| 758 | + ], |
| 759 | + *, |
| 760 | + errors: ErrorOptions = "raise", |
| 761 | + ) -> Self: |
| 762 | + """Drop variables from this Coordinates object. |
| 763 | +
|
| 764 | + Note that indexes that depend on these variables will also be dropped. |
| 765 | +
|
| 766 | + Parameters |
| 767 | + ---------- |
| 768 | + names : hashable or iterable or callable |
| 769 | + Name(s) of variables to drop. If a callable, this is object is passed as its |
| 770 | + only argument and its result is used. |
| 771 | + errors : {"raise", "ignore"}, default: "raise" |
| 772 | + Error treatment. |
| 773 | +
|
| 774 | + - ``'raise'``: raises a :py:class:`ValueError` error if any of the variable |
| 775 | + passed are not in the dataset |
| 776 | + - ``'ignore'``: any given names that are in the dataset are dropped and no |
| 777 | + error is raised. |
| 778 | + """ |
| 779 | + return cast(Self, self.to_dataset().drop_vars(names, errors=errors).coords) |
| 780 | + |
| 781 | + def drop_dims( |
| 782 | + self, |
| 783 | + drop_dims: str | Iterable[Hashable], |
| 784 | + *, |
| 785 | + errors: ErrorOptions = "raise", |
| 786 | + ) -> Self: |
| 787 | + """Drop dimensions and associated variables from this dataset. |
| 788 | +
|
| 789 | + Parameters |
| 790 | + ---------- |
| 791 | + drop_dims : str or Iterable of Hashable |
| 792 | + Dimension or dimensions to drop. |
| 793 | + errors : {"raise", "ignore"}, default: "raise" |
| 794 | + If 'raise', raises a ValueError error if any of the |
| 795 | + dimensions passed are not in the dataset. If 'ignore', any given |
| 796 | + dimensions that are in the dataset are dropped and no error is raised. |
| 797 | +
|
| 798 | + Returns |
| 799 | + ------- |
| 800 | + obj : Coordinates |
| 801 | + Coordinates object without the given dimensions (or any coordinates |
| 802 | + containing those dimensions). |
| 803 | + """ |
| 804 | + return cast(Self, self.to_dataset().drop_dims(drop_dims, errors=errors).coords) |
| 805 | + |
| 806 | + def rename_dims( |
| 807 | + self, |
| 808 | + dims_dict: Mapping[Any, Hashable] | None = None, |
| 809 | + **dims: Hashable, |
| 810 | + ) -> Self: |
| 811 | + """Returns a new object with renamed dimensions only. |
| 812 | +
|
| 813 | + Parameters |
| 814 | + ---------- |
| 815 | + dims_dict : dict-like, optional |
| 816 | + Dictionary whose keys are current dimension names and |
| 817 | + whose values are the desired names. The desired names must |
| 818 | + not be the name of an existing dimension or Variable in the Coordinates. |
| 819 | + **dims : optional |
| 820 | + Keyword form of ``dims_dict``. |
| 821 | + One of dims_dict or dims must be provided. |
| 822 | +
|
| 823 | + Returns |
| 824 | + ------- |
| 825 | + renamed : Coordinates |
| 826 | + Coordinates object with renamed dimensions. |
| 827 | + """ |
| 828 | + return cast(Self, self.to_dataset().rename_dims(dims_dict, **dims).coords) |
| 829 | + |
| 830 | + def rename_vars( |
| 831 | + self, |
| 832 | + name_dict: Mapping[Any, Hashable] | None = None, |
| 833 | + **names: Hashable, |
| 834 | + ) -> Coordinates: |
| 835 | + """Returns a new object with renamed variables. |
| 836 | +
|
| 837 | + Parameters |
| 838 | + ---------- |
| 839 | + name_dict : dict-like, optional |
| 840 | + Dictionary whose keys are current variable or coordinate names and |
| 841 | + whose values are the desired names. |
| 842 | + **names : optional |
| 843 | + Keyword form of ``name_dict``. |
| 844 | + One of name_dict or names must be provided. |
| 845 | +
|
| 846 | + Returns |
| 847 | + ------- |
| 848 | + renamed : Coordinates |
| 849 | + Coordinates object with renamed variables |
| 850 | + """ |
| 851 | + return cast(Self, self.to_dataset().rename_vars(name_dict, **names).coords) |
| 852 | + |
722 | 853 |
|
723 | 854 | class DatasetCoordinates(Coordinates):
|
724 | 855 | """Dictionary like container for Dataset coordinates (variables + indexes).
|
|
0 commit comments