Skip to content

Commit 0e8debf

Browse files
authored
drop_vars; deprecate drop for variables (pydata#3475)
* Deprecate drop for vars, in favor of drop_vars * docs tweaks * handle scalars as vars * allow warning in old whatsnew * add drop_sel, adjust deprecations based on comments * whatsnew * docs * old-whatsnew * docstring * pendingdeprecationwarning * whatsnew * whatsnew * move units tests to drop_sel * is_scalar (but retain isinstance for mypy)
1 parent 4dce93f commit 0e8debf

17 files changed

+286
-196
lines changed

doc/data-structures.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -393,14 +393,14 @@ methods (like pandas) for transforming datasets into new objects.
393393

394394
For removing variables, you can select and drop an explicit list of
395395
variables by indexing with a list of names or using the
396-
:py:meth:`~xarray.Dataset.drop` methods to return a new ``Dataset``. These
396+
:py:meth:`~xarray.Dataset.drop_vars` methods to return a new ``Dataset``. These
397397
operations keep around coordinates:
398398

399399
.. ipython:: python
400400
401401
ds[['temperature']]
402402
ds[['temperature', 'temperature_double']]
403-
ds.drop('temperature')
403+
ds.drop_vars('temperature')
404404
405405
To remove a dimension, you can use :py:meth:`~xarray.Dataset.drop_dims` method.
406406
Any variables using that dimension are dropped:

doc/indexing.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -232,14 +232,14 @@ Using indexing to *assign* values to a subset of dataset (e.g.,
232232
Dropping labels and dimensions
233233
------------------------------
234234

235-
The :py:meth:`~xarray.Dataset.drop` method returns a new object with the listed
235+
The :py:meth:`~xarray.Dataset.drop_sel` method returns a new object with the listed
236236
index labels along a dimension dropped:
237237

238238
.. ipython:: python
239239
240-
ds.drop(space=['IN', 'IL'])
240+
ds.drop_sel(space=['IN', 'IL'])
241241
242-
``drop`` is both a ``Dataset`` and ``DataArray`` method.
242+
``drop_sel`` is both a ``Dataset`` and ``DataArray`` method.
243243

244244
Use :py:meth:`~xarray.Dataset.drop_dims` to drop a full dimension from a Dataset.
245245
Any variables with these dimensions are also dropped:

doc/whats-new.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@ Breaking changes
3838

3939
New Features
4040
~~~~~~~~~~~~
41+
- :py:meth:`Dataset.drop_sel` & :py:meth:`DataArray.drop_sel` have been added for dropping labels.
42+
:py:meth:`Dataset.drop_vars` & :py:meth:`DataArray.drop_vars` have been added for
43+
dropping variables (including coordinates). The existing ``drop`` methods remain as a backward compatible
44+
option for dropping either lables or variables, but using the more specific methods is encouraged.
45+
(:pull:`3475`)
46+
By `Maximilian Roos <https://github.com/max-sixty>`_
4147
- :py:meth:`Dataset.transpose` and :py:meth:`DataArray.transpose` now support an ellipsis (`...`)
4248
to represent all 'other' dimensions. For example, to move one dimension to the front,
4349
use `.transpose('x', ...)`. (:pull:`3421`)
@@ -3752,6 +3758,7 @@ Enhancements
37523758
explicitly listed variables or index labels:
37533759

37543760
.. ipython:: python
3761+
:okwarning:
37553762
37563763
# drop variables
37573764
ds = xray.Dataset({'x': 0, 'y': 1})

xarray/core/concat.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,7 @@ def ensure_common_dims(vars):
388388
result = result.set_coords(coord_names)
389389
result.encoding = result_encoding
390390

391-
result = result.drop(unlabeled_dims, errors="ignore")
391+
result = result.drop_vars(unlabeled_dims, errors="ignore")
392392

393393
if coord is not None:
394394
# add concat dimension last to ensure that its in the final Dataset

xarray/core/dataarray.py

Lines changed: 54 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
TypeVar,
1717
Union,
1818
cast,
19-
overload,
2019
)
2120

2221
import numpy as np
@@ -53,7 +52,7 @@
5352
from .formatting import format_item
5453
from .indexes import Indexes, default_indexes
5554
from .options import OPTIONS
56-
from .utils import Default, ReprObject, _default, _check_inplace, either_dict_or_kwargs
55+
from .utils import Default, ReprObject, _check_inplace, _default, either_dict_or_kwargs
5756
from .variable import (
5857
IndexVariable,
5958
Variable,
@@ -249,7 +248,7 @@ class DataArray(AbstractArray, DataWithCoords):
249248
Dictionary for holding arbitrary metadata.
250249
"""
251250

252-
_accessors: Optional[Dict[str, Any]]
251+
_accessors: Optional[Dict[str, Any]] # noqa
253252
_coords: Dict[Any, Variable]
254253
_indexes: Optional[Dict[Hashable, pd.Index]]
255254
_name: Optional[Hashable]
@@ -1890,41 +1889,72 @@ def transpose(self, *dims: Hashable, transpose_coords: bool = None) -> "DataArra
18901889
def T(self) -> "DataArray":
18911890
return self.transpose()
18921891

1893-
# Drop coords
1894-
@overload
1895-
def drop(
1896-
self, labels: Union[Hashable, Iterable[Hashable]], *, errors: str = "raise"
1892+
def drop_vars(
1893+
self, names: Union[Hashable, Iterable[Hashable]], *, errors: str = "raise"
18971894
) -> "DataArray":
1898-
...
1895+
"""Drop variables from this DataArray.
1896+
1897+
Parameters
1898+
----------
1899+
names : hashable or iterable of hashables
1900+
Name(s) of variables to drop.
1901+
errors: {'raise', 'ignore'}, optional
1902+
If 'raise' (default), raises a ValueError error if any of the variable
1903+
passed are not in the dataset. If 'ignore', any given names that are in the
1904+
DataArray are dropped and no error is raised.
1905+
1906+
Returns
1907+
-------
1908+
dropped : Dataset
1909+
1910+
"""
1911+
ds = self._to_temp_dataset().drop_vars(names, errors=errors)
1912+
return self._from_temp_dataset(ds)
18991913

1900-
# Drop index labels along dimension
1901-
@overload # noqa: F811
19021914
def drop(
1903-
self, labels: Any, dim: Hashable, *, errors: str = "raise" # array-like
1915+
self,
1916+
labels: Mapping = None,
1917+
dim: Hashable = None,
1918+
*,
1919+
errors: str = "raise",
1920+
**labels_kwargs,
19041921
) -> "DataArray":
1905-
...
1922+
"""Backward compatible method based on `drop_vars` and `drop_sel`
19061923
1907-
def drop(self, labels, dim=None, *, errors="raise"): # noqa: F811
1908-
"""Drop coordinates or index labels from this DataArray.
1924+
Using either `drop_vars` or `drop_sel` is encouraged
1925+
"""
1926+
ds = self._to_temp_dataset().drop(labels, dim, errors=errors)
1927+
return self._from_temp_dataset(ds)
1928+
1929+
def drop_sel(
1930+
self,
1931+
labels: Mapping[Hashable, Any] = None,
1932+
*,
1933+
errors: str = "raise",
1934+
**labels_kwargs,
1935+
) -> "DataArray":
1936+
"""Drop index labels from this DataArray.
19091937
19101938
Parameters
19111939
----------
1912-
labels : hashable or sequence of hashables
1913-
Name(s) of coordinates or index labels to drop.
1914-
If dim is not None, labels can be any array-like.
1915-
dim : hashable, optional
1916-
Dimension along which to drop index labels. By default (if
1917-
``dim is None``), drops coordinates rather than index labels.
1940+
labels : Mapping[Hashable, Any]
1941+
Index labels to drop
19181942
errors: {'raise', 'ignore'}, optional
19191943
If 'raise' (default), raises a ValueError error if
1920-
any of the coordinates or index labels passed are not
1921-
in the array. If 'ignore', any given labels that are in the
1922-
array are dropped and no error is raised.
1944+
any of the index labels passed are not
1945+
in the dataset. If 'ignore', any given labels that are in the
1946+
dataset are dropped and no error is raised.
1947+
**labels_kwargs : {dim: label, ...}, optional
1948+
The keyword arguments form of ``dim`` and ``labels``
1949+
19231950
Returns
19241951
-------
19251952
dropped : DataArray
19261953
"""
1927-
ds = self._to_temp_dataset().drop(labels, dim, errors=errors)
1954+
if labels_kwargs or isinstance(labels, dict):
1955+
labels = either_dict_or_kwargs(labels, labels_kwargs, "drop")
1956+
1957+
ds = self._to_temp_dataset().drop_sel(labels, errors=errors)
19281958
return self._from_temp_dataset(ds)
19291959

19301960
def dropna(

0 commit comments

Comments
 (0)