Skip to content

Commit

Permalink
rebase this PR
Browse files Browse the repository at this point in the history
  • Loading branch information
headtr1ck committed Oct 26, 2022
1 parent 076bd8e commit 657dc26
Show file tree
Hide file tree
Showing 2 changed files with 183 additions and 0 deletions.
2 changes: 2 additions & 0 deletions doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ Documentation
By `Jimmy Westling <https://github.com/illviljan>`_.
- Add documentation of specific BackendEntrypoints (:pull:`7200`).
By `Michael Niklas <https://github.com/headtr1ck>`_.
- Add examples to docstring for :py:meth:`DataArray.drop_vars`, :py:meth:`DataArray.reindex_like`, :py:meth:`DataArray.interp_like`. (:issue:`6793`, :pull:`7123`)
By `Daniel Goman <https://github.com/DanielGoman>`_.

Internal Changes
~~~~~~~~~~~~~~~~
Expand Down
181 changes: 181 additions & 0 deletions xarray/core/dataarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -1834,6 +1834,99 @@ def reindex_like(
Another dataset array, with this array's data but coordinates from
the other object.
Examples
--------
>>> data = np.arange(12).reshape(4, 3)
>>> da1 = xr.DataArray(
... data=data,
... dims=["x", "y"],
... coords={"x": [10, 20, 30, 40], "y": [70, 80, 90]},
... )
>>> da1
<xarray.DataArray (x: 4, y: 3)>
array([[ 0, 1, 2],
[ 3, 4, 5],
[ 6, 7, 8],
[ 9, 10, 11]])
Coordinates:
* x (x) int64 10 20 30 40
* y (y) int64 70 80 90
>>> da2 = xr.DataArray(
... data=data,
... dims=["x", "y"],
... coords={"x": [40, 30, 20, 10], "y": [90, 80, 70]},
... )
>>> da2
<xarray.DataArray (x: 4, y: 3)>
array([[ 0, 1, 2],
[ 3, 4, 5],
[ 6, 7, 8],
[ 9, 10, 11]])
Coordinates:
* x (x) int64 40 30 20 10
* y (y) int64 90 80 70
Reindexing with both DataArrays having the same coordinates set, but in different order
>>> da1.reindex_like(da2)
<xarray.DataArray (x: 4, y: 3)>
array([[11, 10, 9],
[ 8, 7, 6],
[ 5, 4, 3],
[ 2, 1, 0]])
Coordinates:
* x (x) int64 40 30 20 10
* y (y) int64 90 80 70
Reindexing with the other array having coordinates which the source array doesn't have
>>> data = np.arange(12).reshape(4, 3)
>>> da1 = xr.DataArray(
... data=data,
... dims=["x", "y"],
... coords={"x": [10, 20, 30, 40], "y": [70, 80, 90]},
... )
>>> da2 = xr.DataArray(
... data=data,
... dims=["x", "y"],
... coords={"x": [20, 10, 29, 39], "y": [70, 80, 90]},
... )
>>> da1.reindex_like(da2)
<xarray.DataArray (x: 4, y: 3)>
array([[ 3., 4., 5.],
[ 0., 1., 2.],
[nan, nan, nan],
[nan, nan, nan]])
Coordinates:
* x (x) int64 20 10 29 39
* y (y) int64 70 80 90
Filling missing values with the previous valid index with respect to the coordinates' value
>>> da1.reindex_like(da2, method="ffill")
<xarray.DataArray (x: 4, y: 3)>
array([[3, 4, 5],
[0, 1, 2],
[3, 4, 5],
[6, 7, 8]])
Coordinates:
* x (x) int64 20 10 29 39
* y (y) int64 70 80 90
Filling missing values while tolerating specified error for inexact matches
>>> da1.reindex_like(da2, method="ffill", tolerance=5)
<xarray.DataArray (x: 4, y: 3)>
array([[ 3., 4., 5.],
[ 0., 1., 2.],
[nan, nan, nan],
[nan, nan, nan]])
Coordinates:
* x (x) int64 20 10 29 39
* y (y) int64 70 80 90
Filling missing values with manually specified values
>>> da1.reindex_like(da2, fill_value=19)
<xarray.DataArray (x: 4, y: 3)>
array([[ 3, 4, 5],
[ 0, 1, 2],
[19, 19, 19],
[19, 19, 19]])
Coordinates:
* x (x) int64 20 10 29 39
* y (y) int64 70 80 90
See Also
--------
DataArray.reindex
Expand Down Expand Up @@ -2130,6 +2223,58 @@ def interp_like(
Another dataarray by interpolating this dataarray's data along the
coordinates of the other object.
Examples
--------
>>> data = np.arange(12).reshape(4, 3)
>>> da1 = xr.DataArray(
... data=data,
... dims=["x", "y"],
... coords={"x": [10, 20, 30, 40], "y": [70, 80, 90]},
... )
>>> da1
<xarray.DataArray (x: 4, y: 3)>
array([[ 0, 1, 2],
[ 3, 4, 5],
[ 6, 7, 8],
[ 9, 10, 11]])
Coordinates:
* x (x) int64 10 20 30 40
* y (y) int64 70 80 90
>>> da2 = xr.DataArray(
... data=data,
... dims=["x", "y"],
... coords={"x": [10, 20, 29, 39], "y": [70, 80, 90]},
... )
>>> da2
<xarray.DataArray (x: 4, y: 3)>
array([[ 0, 1, 2],
[ 3, 4, 5],
[ 6, 7, 8],
[ 9, 10, 11]])
Coordinates:
* x (x) int64 10 20 29 39
* y (y) int64 70 80 90
Interpolate the values in the coordinates of the other DataArray with respect to the source's values
>>> da2.interp_like(da1)
<xarray.DataArray (x: 4, y: 3)>
array([[0. , 1. , 2. ],
[3. , 4. , 5. ],
[6.3, 7.3, 8.3],
[nan, nan, nan]])
Coordinates:
* x (x) int64 10 20 30 40
* y (y) int64 70 80 90
Could also extrapolate missing values
>>> da2.interp_like(da1, kwargs={"fill_value": "extrapolate"})
<xarray.DataArray (x: 4, y: 3)>
array([[ 0. , 1. , 2. ],
[ 3. , 4. , 5. ],
[ 6.3, 7.3, 8.3],
[ 9.3, 10.3, 11.3]])
Coordinates:
* x (x) int64 10 20 30 40
* y (y) int64 70 80 90
Notes
-----
scipy is required.
Expand Down Expand Up @@ -2791,6 +2936,42 @@ def drop_vars(
-------
dropped : Dataset
New Dataset copied from `self` with variables removed.
Examples
-------
>>> data = np.arange(12).reshape(4, 3)
>>> da = xr.DataArray(
... data=data,
... dims=["x", "y"],
... coords={"x": [10, 20, 30, 40], "y": [70, 80, 90]},
... )
>>> da
<xarray.DataArray (x: 4, y: 3)>
array([[ 0, 1, 2],
[ 3, 4, 5],
[ 6, 7, 8],
[ 9, 10, 11]])
Coordinates:
* x (x) int64 10 20 30 40
* y (y) int64 70 80 90
Removing a single variable
>>> da.drop_vars("x")
<xarray.DataArray (x: 4, y: 3)>
array([[ 0, 1, 2],
[ 3, 4, 5],
[ 6, 7, 8],
[ 9, 10, 11]])
Coordinates:
* y (y) int64 70 80 90
Dimensions without coordinates: x
Removing a list of variables
>>> da.drop_vars(["x", "y"])
<xarray.DataArray (x: 4, y: 3)>
array([[ 0, 1, 2],
[ 3, 4, 5],
[ 6, 7, 8],
[ 9, 10, 11]])
Dimensions without coordinates: x, y
"""
ds = self._to_temp_dataset().drop_vars(names, errors=errors)
return self._from_temp_dataset(ds)
Expand Down

0 comments on commit 657dc26

Please sign in to comment.