|
16 | 16 | TypeVar, |
17 | 17 | Union, |
18 | 18 | cast, |
19 | | - overload, |
20 | 19 | ) |
21 | 20 |
|
22 | 21 | import numpy as np |
|
54 | 53 | from .indexes import Indexes, default_indexes |
55 | 54 | from .merge import PANDAS_TYPES |
56 | 55 | from .options import OPTIONS |
57 | | -from .utils import Default, ReprObject, _default, _check_inplace, either_dict_or_kwargs |
| 56 | +from .utils import Default, ReprObject, _check_inplace, _default, either_dict_or_kwargs |
58 | 57 | from .variable import ( |
59 | 58 | IndexVariable, |
60 | 59 | Variable, |
@@ -250,7 +249,7 @@ class DataArray(AbstractArray, DataWithCoords): |
250 | 249 | Dictionary for holding arbitrary metadata. |
251 | 250 | """ |
252 | 251 |
|
253 | | - _accessors: Optional[Dict[str, Any]] |
| 252 | + _accessors: Optional[Dict[str, Any]] # noqa |
254 | 253 | _coords: Dict[Any, Variable] |
255 | 254 | _indexes: Optional[Dict[Hashable, pd.Index]] |
256 | 255 | _name: Optional[Hashable] |
@@ -1891,41 +1890,72 @@ def transpose(self, *dims: Hashable, transpose_coords: bool = None) -> "DataArra |
1891 | 1890 | def T(self) -> "DataArray": |
1892 | 1891 | return self.transpose() |
1893 | 1892 |
|
1894 | | - # Drop coords |
1895 | | - @overload |
1896 | | - def drop( |
1897 | | - self, labels: Union[Hashable, Iterable[Hashable]], *, errors: str = "raise" |
| 1893 | + def drop_vars( |
| 1894 | + self, names: Union[Hashable, Iterable[Hashable]], *, errors: str = "raise" |
1898 | 1895 | ) -> "DataArray": |
1899 | | - ... |
| 1896 | + """Drop variables from this DataArray. |
| 1897 | +
|
| 1898 | + Parameters |
| 1899 | + ---------- |
| 1900 | + names : hashable or iterable of hashables |
| 1901 | + Name(s) of variables to drop. |
| 1902 | + errors: {'raise', 'ignore'}, optional |
| 1903 | + If 'raise' (default), raises a ValueError error if any of the variable |
| 1904 | + passed are not in the dataset. If 'ignore', any given names that are in the |
| 1905 | + DataArray are dropped and no error is raised. |
| 1906 | +
|
| 1907 | + Returns |
| 1908 | + ------- |
| 1909 | + dropped : Dataset |
| 1910 | +
|
| 1911 | + """ |
| 1912 | + ds = self._to_temp_dataset().drop_vars(names, errors=errors) |
| 1913 | + return self._from_temp_dataset(ds) |
1900 | 1914 |
|
1901 | | - # Drop index labels along dimension |
1902 | | - @overload # noqa: F811 |
1903 | 1915 | def drop( |
1904 | | - self, labels: Any, dim: Hashable, *, errors: str = "raise" # array-like |
| 1916 | + self, |
| 1917 | + labels: Mapping = None, |
| 1918 | + dim: Hashable = None, |
| 1919 | + *, |
| 1920 | + errors: str = "raise", |
| 1921 | + **labels_kwargs, |
1905 | 1922 | ) -> "DataArray": |
1906 | | - ... |
| 1923 | + """Backward compatible method based on `drop_vars` and `drop_sel` |
1907 | 1924 |
|
1908 | | - def drop(self, labels, dim=None, *, errors="raise"): # noqa: F811 |
1909 | | - """Drop coordinates or index labels from this DataArray. |
| 1925 | + Using either `drop_vars` or `drop_sel` is encouraged |
| 1926 | + """ |
| 1927 | + ds = self._to_temp_dataset().drop(labels, dim, errors=errors) |
| 1928 | + return self._from_temp_dataset(ds) |
| 1929 | + |
| 1930 | + def drop_sel( |
| 1931 | + self, |
| 1932 | + labels: Mapping[Hashable, Any] = None, |
| 1933 | + *, |
| 1934 | + errors: str = "raise", |
| 1935 | + **labels_kwargs, |
| 1936 | + ) -> "DataArray": |
| 1937 | + """Drop index labels from this DataArray. |
1910 | 1938 |
|
1911 | 1939 | Parameters |
1912 | 1940 | ---------- |
1913 | | - labels : hashable or sequence of hashables |
1914 | | - Name(s) of coordinates or index labels to drop. |
1915 | | - If dim is not None, labels can be any array-like. |
1916 | | - dim : hashable, optional |
1917 | | - Dimension along which to drop index labels. By default (if |
1918 | | - ``dim is None``), drops coordinates rather than index labels. |
| 1941 | + labels : Mapping[Hashable, Any] |
| 1942 | + Index labels to drop |
1919 | 1943 | errors: {'raise', 'ignore'}, optional |
1920 | 1944 | If 'raise' (default), raises a ValueError error if |
1921 | | - any of the coordinates or index labels passed are not |
1922 | | - in the array. If 'ignore', any given labels that are in the |
1923 | | - array are dropped and no error is raised. |
| 1945 | + any of the index labels passed are not |
| 1946 | + in the dataset. If 'ignore', any given labels that are in the |
| 1947 | + dataset are dropped and no error is raised. |
| 1948 | + **labels_kwargs : {dim: label, ...}, optional |
| 1949 | + The keyword arguments form of ``dim`` and ``labels`` |
| 1950 | +
|
1924 | 1951 | Returns |
1925 | 1952 | ------- |
1926 | 1953 | dropped : DataArray |
1927 | 1954 | """ |
1928 | | - ds = self._to_temp_dataset().drop(labels, dim, errors=errors) |
| 1955 | + if labels_kwargs or isinstance(labels, dict): |
| 1956 | + labels = either_dict_or_kwargs(labels, labels_kwargs, "drop") |
| 1957 | + |
| 1958 | + ds = self._to_temp_dataset().drop_sel(labels, errors=errors) |
1929 | 1959 | return self._from_temp_dataset(ds) |
1930 | 1960 |
|
1931 | 1961 | def dropna( |
|
0 commit comments