Skip to content

Commit 0dfcfdd

Browse files
DOC: Added examples to docstrings of DataArray methods (#7123) (#7123)
* rebase this PR * fix missing spaces Co-authored-by: Michael Niklas <mick.niklas@gmail.com>
1 parent cb17dad commit 0dfcfdd

File tree

2 files changed

+201
-0
lines changed

2 files changed

+201
-0
lines changed

doc/whats-new.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ Documentation
8181
By `Jimmy Westling <https://github.com/illviljan>`_.
8282
- Add documentation of specific BackendEntrypoints (:pull:`7200`).
8383
By `Michael Niklas <https://github.com/headtr1ck>`_.
84+
- Add examples to docstring for :py:meth:`DataArray.drop_vars`, :py:meth:`DataArray.reindex_like`, :py:meth:`DataArray.interp_like`. (:issue:`6793`, :pull:`7123`)
85+
By `Daniel Goman <https://github.com/DanielGoman>`_.
8486

8587
Internal Changes
8688
~~~~~~~~~~~~~~~~

xarray/core/dataarray.py

Lines changed: 199 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1834,6 +1834,109 @@ def reindex_like(
18341834
Another dataset array, with this array's data but coordinates from
18351835
the other object.
18361836
1837+
Examples
1838+
--------
1839+
>>> data = np.arange(12).reshape(4, 3)
1840+
>>> da1 = xr.DataArray(
1841+
... data=data,
1842+
... dims=["x", "y"],
1843+
... coords={"x": [10, 20, 30, 40], "y": [70, 80, 90]},
1844+
... )
1845+
>>> da1
1846+
<xarray.DataArray (x: 4, y: 3)>
1847+
array([[ 0, 1, 2],
1848+
[ 3, 4, 5],
1849+
[ 6, 7, 8],
1850+
[ 9, 10, 11]])
1851+
Coordinates:
1852+
* x (x) int64 10 20 30 40
1853+
* y (y) int64 70 80 90
1854+
>>> da2 = xr.DataArray(
1855+
... data=data,
1856+
... dims=["x", "y"],
1857+
... coords={"x": [40, 30, 20, 10], "y": [90, 80, 70]},
1858+
... )
1859+
>>> da2
1860+
<xarray.DataArray (x: 4, y: 3)>
1861+
array([[ 0, 1, 2],
1862+
[ 3, 4, 5],
1863+
[ 6, 7, 8],
1864+
[ 9, 10, 11]])
1865+
Coordinates:
1866+
* x (x) int64 40 30 20 10
1867+
* y (y) int64 90 80 70
1868+
1869+
Reindexing with both DataArrays having the same coordinates set, but in different order:
1870+
1871+
>>> da1.reindex_like(da2)
1872+
<xarray.DataArray (x: 4, y: 3)>
1873+
array([[11, 10, 9],
1874+
[ 8, 7, 6],
1875+
[ 5, 4, 3],
1876+
[ 2, 1, 0]])
1877+
Coordinates:
1878+
* x (x) int64 40 30 20 10
1879+
* y (y) int64 90 80 70
1880+
1881+
Reindexing with the other array having coordinates which the source array doesn't have:
1882+
1883+
>>> data = np.arange(12).reshape(4, 3)
1884+
>>> da1 = xr.DataArray(
1885+
... data=data,
1886+
... dims=["x", "y"],
1887+
... coords={"x": [10, 20, 30, 40], "y": [70, 80, 90]},
1888+
... )
1889+
>>> da2 = xr.DataArray(
1890+
... data=data,
1891+
... dims=["x", "y"],
1892+
... coords={"x": [20, 10, 29, 39], "y": [70, 80, 90]},
1893+
... )
1894+
>>> da1.reindex_like(da2)
1895+
<xarray.DataArray (x: 4, y: 3)>
1896+
array([[ 3., 4., 5.],
1897+
[ 0., 1., 2.],
1898+
[nan, nan, nan],
1899+
[nan, nan, nan]])
1900+
Coordinates:
1901+
* x (x) int64 20 10 29 39
1902+
* y (y) int64 70 80 90
1903+
1904+
Filling missing values with the previous valid index with respect to the coordinates' value:
1905+
1906+
>>> da1.reindex_like(da2, method="ffill")
1907+
<xarray.DataArray (x: 4, y: 3)>
1908+
array([[3, 4, 5],
1909+
[0, 1, 2],
1910+
[3, 4, 5],
1911+
[6, 7, 8]])
1912+
Coordinates:
1913+
* x (x) int64 20 10 29 39
1914+
* y (y) int64 70 80 90
1915+
1916+
Filling missing values while tolerating specified error for inexact matches:
1917+
1918+
>>> da1.reindex_like(da2, method="ffill", tolerance=5)
1919+
<xarray.DataArray (x: 4, y: 3)>
1920+
array([[ 3., 4., 5.],
1921+
[ 0., 1., 2.],
1922+
[nan, nan, nan],
1923+
[nan, nan, nan]])
1924+
Coordinates:
1925+
* x (x) int64 20 10 29 39
1926+
* y (y) int64 70 80 90
1927+
1928+
Filling missing values with manually specified values:
1929+
1930+
>>> da1.reindex_like(da2, fill_value=19)
1931+
<xarray.DataArray (x: 4, y: 3)>
1932+
array([[ 3, 4, 5],
1933+
[ 0, 1, 2],
1934+
[19, 19, 19],
1935+
[19, 19, 19]])
1936+
Coordinates:
1937+
* x (x) int64 20 10 29 39
1938+
* y (y) int64 70 80 90
1939+
18371940
See Also
18381941
--------
18391942
DataArray.reindex
@@ -2130,6 +2233,62 @@ def interp_like(
21302233
Another dataarray by interpolating this dataarray's data along the
21312234
coordinates of the other object.
21322235
2236+
Examples
2237+
--------
2238+
>>> data = np.arange(12).reshape(4, 3)
2239+
>>> da1 = xr.DataArray(
2240+
... data=data,
2241+
... dims=["x", "y"],
2242+
... coords={"x": [10, 20, 30, 40], "y": [70, 80, 90]},
2243+
... )
2244+
>>> da1
2245+
<xarray.DataArray (x: 4, y: 3)>
2246+
array([[ 0, 1, 2],
2247+
[ 3, 4, 5],
2248+
[ 6, 7, 8],
2249+
[ 9, 10, 11]])
2250+
Coordinates:
2251+
* x (x) int64 10 20 30 40
2252+
* y (y) int64 70 80 90
2253+
>>> da2 = xr.DataArray(
2254+
... data=data,
2255+
... dims=["x", "y"],
2256+
... coords={"x": [10, 20, 29, 39], "y": [70, 80, 90]},
2257+
... )
2258+
>>> da2
2259+
<xarray.DataArray (x: 4, y: 3)>
2260+
array([[ 0, 1, 2],
2261+
[ 3, 4, 5],
2262+
[ 6, 7, 8],
2263+
[ 9, 10, 11]])
2264+
Coordinates:
2265+
* x (x) int64 10 20 29 39
2266+
* y (y) int64 70 80 90
2267+
2268+
Interpolate the values in the coordinates of the other DataArray with respect to the source's values:
2269+
2270+
>>> da2.interp_like(da1)
2271+
<xarray.DataArray (x: 4, y: 3)>
2272+
array([[0. , 1. , 2. ],
2273+
[3. , 4. , 5. ],
2274+
[6.3, 7.3, 8.3],
2275+
[nan, nan, nan]])
2276+
Coordinates:
2277+
* x (x) int64 10 20 30 40
2278+
* y (y) int64 70 80 90
2279+
2280+
Could also extrapolate missing values:
2281+
2282+
>>> da2.interp_like(da1, kwargs={"fill_value": "extrapolate"})
2283+
<xarray.DataArray (x: 4, y: 3)>
2284+
array([[ 0. , 1. , 2. ],
2285+
[ 3. , 4. , 5. ],
2286+
[ 6.3, 7.3, 8.3],
2287+
[ 9.3, 10.3, 11.3]])
2288+
Coordinates:
2289+
* x (x) int64 10 20 30 40
2290+
* y (y) int64 70 80 90
2291+
21332292
Notes
21342293
-----
21352294
scipy is required.
@@ -2791,6 +2950,46 @@ def drop_vars(
27912950
-------
27922951
dropped : Dataset
27932952
New Dataset copied from `self` with variables removed.
2953+
2954+
Examples
2955+
-------
2956+
>>> data = np.arange(12).reshape(4, 3)
2957+
>>> da = xr.DataArray(
2958+
... data=data,
2959+
... dims=["x", "y"],
2960+
... coords={"x": [10, 20, 30, 40], "y": [70, 80, 90]},
2961+
... )
2962+
>>> da
2963+
<xarray.DataArray (x: 4, y: 3)>
2964+
array([[ 0, 1, 2],
2965+
[ 3, 4, 5],
2966+
[ 6, 7, 8],
2967+
[ 9, 10, 11]])
2968+
Coordinates:
2969+
* x (x) int64 10 20 30 40
2970+
* y (y) int64 70 80 90
2971+
2972+
Removing a single variable:
2973+
2974+
>>> da.drop_vars("x")
2975+
<xarray.DataArray (x: 4, y: 3)>
2976+
array([[ 0, 1, 2],
2977+
[ 3, 4, 5],
2978+
[ 6, 7, 8],
2979+
[ 9, 10, 11]])
2980+
Coordinates:
2981+
* y (y) int64 70 80 90
2982+
Dimensions without coordinates: x
2983+
2984+
Removing a list of variables:
2985+
2986+
>>> da.drop_vars(["x", "y"])
2987+
<xarray.DataArray (x: 4, y: 3)>
2988+
array([[ 0, 1, 2],
2989+
[ 3, 4, 5],
2990+
[ 6, 7, 8],
2991+
[ 9, 10, 11]])
2992+
Dimensions without coordinates: x, y
27942993
"""
27952994
ds = self._to_temp_dataset().drop_vars(names, errors=errors)
27962995
return self._from_temp_dataset(ds)

0 commit comments

Comments
 (0)