Skip to content

Commit 3bc33ee

Browse files
authored
Add docs to reindex_like re broadcasting (#8327)
* Add docs to `reindex_like` re broadcasting This wasn't clear to me so I added some examples & a reference to `broadcast_like`
1 parent 8f3a302 commit 3bc33ee

File tree

2 files changed

+35
-16
lines changed

2 files changed

+35
-16
lines changed

xarray/core/dataarray.py

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1859,8 +1859,10 @@ def reindex_like(
18591859
copy: bool = True,
18601860
fill_value=dtypes.NA,
18611861
) -> Self:
1862-
"""Conform this object onto the indexes of another object, filling in
1863-
missing values with ``fill_value``. The default fill value is NaN.
1862+
"""
1863+
Conform this object onto the indexes of another object, for indexes which the
1864+
objects share. Missing values are filled with ``fill_value``. The default fill
1865+
value is NaN.
18641866
18651867
Parameters
18661868
----------
@@ -1948,20 +1950,14 @@ def reindex_like(
19481950
* x (x) int64 40 30 20 10
19491951
* y (y) int64 90 80 70
19501952
1951-
Reindexing with the other array having coordinates which the source array doesn't have:
1953+
Reindexing with the other array having additional coordinates:
19521954
1953-
>>> data = np.arange(12).reshape(4, 3)
1954-
>>> da1 = xr.DataArray(
1955-
... data=data,
1956-
... dims=["x", "y"],
1957-
... coords={"x": [10, 20, 30, 40], "y": [70, 80, 90]},
1958-
... )
1959-
>>> da2 = xr.DataArray(
1955+
>>> da3 = xr.DataArray(
19601956
... data=data,
19611957
... dims=["x", "y"],
19621958
... coords={"x": [20, 10, 29, 39], "y": [70, 80, 90]},
19631959
... )
1964-
>>> da1.reindex_like(da2)
1960+
>>> da1.reindex_like(da3)
19651961
<xarray.DataArray (x: 4, y: 3)>
19661962
array([[ 3., 4., 5.],
19671963
[ 0., 1., 2.],
@@ -1973,7 +1969,7 @@ def reindex_like(
19731969
19741970
Filling missing values with the previous valid index with respect to the coordinates' value:
19751971
1976-
>>> da1.reindex_like(da2, method="ffill")
1972+
>>> da1.reindex_like(da3, method="ffill")
19771973
<xarray.DataArray (x: 4, y: 3)>
19781974
array([[3, 4, 5],
19791975
[0, 1, 2],
@@ -1985,7 +1981,7 @@ def reindex_like(
19851981
19861982
Filling missing values while tolerating specified error for inexact matches:
19871983
1988-
>>> da1.reindex_like(da2, method="ffill", tolerance=5)
1984+
>>> da1.reindex_like(da3, method="ffill", tolerance=5)
19891985
<xarray.DataArray (x: 4, y: 3)>
19901986
array([[ 3., 4., 5.],
19911987
[ 0., 1., 2.],
@@ -1997,7 +1993,7 @@ def reindex_like(
19971993
19981994
Filling missing values with manually specified values:
19991995
2000-
>>> da1.reindex_like(da2, fill_value=19)
1996+
>>> da1.reindex_like(da3, fill_value=19)
20011997
<xarray.DataArray (x: 4, y: 3)>
20021998
array([[ 3, 4, 5],
20031999
[ 0, 1, 2],
@@ -2007,9 +2003,28 @@ def reindex_like(
20072003
* x (x) int64 20 10 29 39
20082004
* y (y) int64 70 80 90
20092005
2006+
Note that unlike ``broadcast_like``, ``reindex_like`` doesn't create new dimensions:
2007+
2008+
>>> da1.sel(x=20)
2009+
<xarray.DataArray (y: 3)>
2010+
array([3, 4, 5])
2011+
Coordinates:
2012+
x int64 20
2013+
* y (y) int64 70 80 90
2014+
2015+
...so ``b`` in not added here:
2016+
2017+
>>> da1.sel(x=20).reindex_like(da1)
2018+
<xarray.DataArray (y: 3)>
2019+
array([3, 4, 5])
2020+
Coordinates:
2021+
x int64 20
2022+
* y (y) int64 70 80 90
2023+
20102024
See Also
20112025
--------
20122026
DataArray.reindex
2027+
DataArray.broadcast_like
20132028
align
20142029
"""
20152030
return alignment.reindex_like(

xarray/core/dataset.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3422,8 +3422,10 @@ def reindex_like(
34223422
copy: bool = True,
34233423
fill_value: Any = xrdtypes.NA,
34243424
) -> Self:
3425-
"""Conform this object onto the indexes of another object, filling in
3426-
missing values with ``fill_value``. The default fill value is NaN.
3425+
"""
3426+
Conform this object onto the indexes of another object, for indexes which the
3427+
objects share. Missing values are filled with ``fill_value``. The default fill
3428+
value is NaN.
34273429
34283430
Parameters
34293431
----------
@@ -3469,7 +3471,9 @@ def reindex_like(
34693471
See Also
34703472
--------
34713473
Dataset.reindex
3474+
DataArray.reindex_like
34723475
align
3476+
34733477
"""
34743478
return alignment.reindex_like(
34753479
self,

0 commit comments

Comments
 (0)