Skip to content
forked from pydata/xarray

Commit

Permalink
drop_vars; deprecate drop for variables (pydata#3475)
Browse files Browse the repository at this point in the history
* 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)
  • Loading branch information
max-sixty authored Nov 7, 2019
1 parent 4dce93f commit 0e8debf
Show file tree
Hide file tree
Showing 17 changed files with 286 additions and 196 deletions.
4 changes: 2 additions & 2 deletions doc/data-structures.rst
Original file line number Diff line number Diff line change
Expand Up @@ -393,14 +393,14 @@ methods (like pandas) for transforming datasets into new objects.

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

.. ipython:: python
ds[['temperature']]
ds[['temperature', 'temperature_double']]
ds.drop('temperature')
ds.drop_vars('temperature')
To remove a dimension, you can use :py:meth:`~xarray.Dataset.drop_dims` method.
Any variables using that dimension are dropped:
Expand Down
6 changes: 3 additions & 3 deletions doc/indexing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -232,14 +232,14 @@ Using indexing to *assign* values to a subset of dataset (e.g.,
Dropping labels and dimensions
------------------------------

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

.. ipython:: python
ds.drop(space=['IN', 'IL'])
ds.drop_sel(space=['IN', 'IL'])
``drop`` is both a ``Dataset`` and ``DataArray`` method.
``drop_sel`` is both a ``Dataset`` and ``DataArray`` method.

Use :py:meth:`~xarray.Dataset.drop_dims` to drop a full dimension from a Dataset.
Any variables with these dimensions are also dropped:
Expand Down
7 changes: 7 additions & 0 deletions doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ Breaking changes

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

.. ipython:: python
:okwarning:
# drop variables
ds = xray.Dataset({'x': 0, 'y': 1})
Expand Down
2 changes: 1 addition & 1 deletion xarray/core/concat.py
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@ def ensure_common_dims(vars):
result = result.set_coords(coord_names)
result.encoding = result_encoding

result = result.drop(unlabeled_dims, errors="ignore")
result = result.drop_vars(unlabeled_dims, errors="ignore")

if coord is not None:
# add concat dimension last to ensure that its in the final Dataset
Expand Down
78 changes: 54 additions & 24 deletions xarray/core/dataarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
TypeVar,
Union,
cast,
overload,
)

import numpy as np
Expand Down Expand Up @@ -53,7 +52,7 @@
from .formatting import format_item
from .indexes import Indexes, default_indexes
from .options import OPTIONS
from .utils import Default, ReprObject, _default, _check_inplace, either_dict_or_kwargs
from .utils import Default, ReprObject, _check_inplace, _default, either_dict_or_kwargs
from .variable import (
IndexVariable,
Variable,
Expand Down Expand Up @@ -249,7 +248,7 @@ class DataArray(AbstractArray, DataWithCoords):
Dictionary for holding arbitrary metadata.
"""

_accessors: Optional[Dict[str, Any]]
_accessors: Optional[Dict[str, Any]] # noqa
_coords: Dict[Any, Variable]
_indexes: Optional[Dict[Hashable, pd.Index]]
_name: Optional[Hashable]
Expand Down Expand Up @@ -1890,41 +1889,72 @@ def transpose(self, *dims: Hashable, transpose_coords: bool = None) -> "DataArra
def T(self) -> "DataArray":
return self.transpose()

# Drop coords
@overload
def drop(
self, labels: Union[Hashable, Iterable[Hashable]], *, errors: str = "raise"
def drop_vars(
self, names: Union[Hashable, Iterable[Hashable]], *, errors: str = "raise"
) -> "DataArray":
...
"""Drop variables from this DataArray.
Parameters
----------
names : hashable or iterable of hashables
Name(s) of variables to drop.
errors: {'raise', 'ignore'}, optional
If 'raise' (default), raises a ValueError error if any of the variable
passed are not in the dataset. If 'ignore', any given names that are in the
DataArray are dropped and no error is raised.
Returns
-------
dropped : Dataset
"""
ds = self._to_temp_dataset().drop_vars(names, errors=errors)
return self._from_temp_dataset(ds)

# Drop index labels along dimension
@overload # noqa: F811
def drop(
self, labels: Any, dim: Hashable, *, errors: str = "raise" # array-like
self,
labels: Mapping = None,
dim: Hashable = None,
*,
errors: str = "raise",
**labels_kwargs,
) -> "DataArray":
...
"""Backward compatible method based on `drop_vars` and `drop_sel`
def drop(self, labels, dim=None, *, errors="raise"): # noqa: F811
"""Drop coordinates or index labels from this DataArray.
Using either `drop_vars` or `drop_sel` is encouraged
"""
ds = self._to_temp_dataset().drop(labels, dim, errors=errors)
return self._from_temp_dataset(ds)

def drop_sel(
self,
labels: Mapping[Hashable, Any] = None,
*,
errors: str = "raise",
**labels_kwargs,
) -> "DataArray":
"""Drop index labels from this DataArray.
Parameters
----------
labels : hashable or sequence of hashables
Name(s) of coordinates or index labels to drop.
If dim is not None, labels can be any array-like.
dim : hashable, optional
Dimension along which to drop index labels. By default (if
``dim is None``), drops coordinates rather than index labels.
labels : Mapping[Hashable, Any]
Index labels to drop
errors: {'raise', 'ignore'}, optional
If 'raise' (default), raises a ValueError error if
any of the coordinates or index labels passed are not
in the array. If 'ignore', any given labels that are in the
array are dropped and no error is raised.
any of the index labels passed are not
in the dataset. If 'ignore', any given labels that are in the
dataset are dropped and no error is raised.
**labels_kwargs : {dim: label, ...}, optional
The keyword arguments form of ``dim`` and ``labels``
Returns
-------
dropped : DataArray
"""
ds = self._to_temp_dataset().drop(labels, dim, errors=errors)
if labels_kwargs or isinstance(labels, dict):
labels = either_dict_or_kwargs(labels, labels_kwargs, "drop")

ds = self._to_temp_dataset().drop_sel(labels, errors=errors)
return self._from_temp_dataset(ds)

def dropna(
Expand Down
Loading

0 comments on commit 0e8debf

Please sign in to comment.