Skip to content

Commit 8da3f67

Browse files
DavidMertzshoyer
authored andcommitted
Merge broadcast_like docstrings, analyze implementation problem (#3130)
* Merge broadcast_like docstrings, analyze implementation problem * PEP8 fixes * PEP8 fixes * Fix dataarray bug * Fix broadcast_like logic error * Remove unused import * Add back additional broadcast_like tests * lint
1 parent 539fb4a commit 8da3f67

File tree

4 files changed

+64
-18
lines changed

4 files changed

+64
-18
lines changed

doc/whats-new.rst

+2-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ New functions/methods
2525
~~~~~~~~~~~~~~~~~~~~~
2626

2727
- Added :py:meth:`DataArray.broadcast_like` and :py:meth:`Dataset.broadcast_like`.
28-
By `Deepak Cherian <https://github.com/dcherian>`_.
28+
By `Deepak Cherian <https://github.com/dcherian>`_ and `David Mertz
29+
<http://github.com/DavidMertz>`_.
2930

3031
Enhancements
3132
~~~~~~~~~~~~

xarray/core/dataarray.py

+44-3
Original file line numberDiff line numberDiff line change
@@ -998,25 +998,66 @@ def sel_points(self, dim='points', method=None, tolerance=None,
998998
def broadcast_like(self,
999999
other: Union['DataArray', Dataset],
10001000
exclude=None) -> 'DataArray':
1001-
"""Broadcast this DataArray against another Dataset or DataArray.
1001+
"""Broadcast a DataArray to the shape of another DataArray or Dataset
1002+
10021003
This is equivalent to xr.broadcast(other, self)[1]
10031004
1005+
xarray objects are broadcast against each other in arithmetic
1006+
operations, so this method is not be necessary for most uses.
1007+
1008+
If no change is needed, the input data is returned to the output
1009+
without being copied.
1010+
1011+
If new coords are added by the broadcast, their values are
1012+
NaN filled.
1013+
10041014
Parameters
10051015
----------
10061016
other : Dataset or DataArray
10071017
Object against which to broadcast this array.
1018+
10081019
exclude : sequence of str, optional
10091020
Dimensions that must not be broadcasted
1010-
"""
10111021
1022+
Returns
1023+
-------
1024+
new_da: xr.DataArray
1025+
1026+
Examples
1027+
--------
1028+
1029+
>>> arr1
1030+
<xarray.DataArray (x: 2, y: 3)>
1031+
array([[0.840235, 0.215216, 0.77917 ],
1032+
[0.726351, 0.543824, 0.875115]])
1033+
Coordinates:
1034+
* x (x) <U1 'a' 'b'
1035+
* y (y) <U1 'a' 'b' 'c'
1036+
>>> arr2
1037+
<xarray.DataArray (x: 3, y: 2)>
1038+
array([[0.612611, 0.125753],
1039+
[0.853181, 0.948818],
1040+
[0.180885, 0.33363 ]])
1041+
Coordinates:
1042+
* x (x) <U1 'a' 'b' 'c'
1043+
* y (y) <U1 'a' 'b'
1044+
>>> arr1.broadcast_like(arr2)
1045+
<xarray.DataArray (x: 3, y: 3)>
1046+
array([[0.840235, 0.215216, 0.77917 ],
1047+
[0.726351, 0.543824, 0.875115],
1048+
[ nan, nan, nan]])
1049+
Coordinates:
1050+
* x (x) object 'a' 'b' 'c'
1051+
* y (y) object 'a' 'b' 'c'
1052+
"""
10121053
if exclude is None:
10131054
exclude = set()
10141055
args = align(other, self, join='outer', copy=False, exclude=exclude)
10151056

10161057
dims_map, common_coords = _get_broadcast_dims_map_common_coords(
10171058
args, exclude)
10181059

1019-
return _broadcast_helper(self, exclude, dims_map, common_coords)
1060+
return _broadcast_helper(args[1], exclude, dims_map, common_coords)
10201061

10211062
def reindex_like(self, other: Union['DataArray', Dataset],
10221063
method: Optional[str] = None, tolerance=None,

xarray/core/dataset.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -2039,7 +2039,7 @@ def broadcast_like(self,
20392039
dims_map, common_coords = _get_broadcast_dims_map_common_coords(
20402040
args, exclude)
20412041

2042-
return _broadcast_helper(self, exclude, dims_map, common_coords)
2042+
return _broadcast_helper(args[1], exclude, dims_map, common_coords)
20432043

20442044
def reindex_like(self, other, method=None, tolerance=None, copy=True,
20452045
fill_value=dtypes.NA):

xarray/tests/test_dataarray.py

+17-13
Original file line numberDiff line numberDiff line change
@@ -1256,19 +1256,23 @@ def test_coords_non_string(self):
12561256
assert_identical(actual, expected)
12571257

12581258
def test_broadcast_like(self):
1259-
original1 = DataArray(np.random.randn(5),
1260-
[('x', range(5))])
1261-
1262-
original2 = DataArray(np.random.randn(6),
1263-
[('y', range(6))])
1264-
1265-
expected1, expected2 = broadcast(original1, original2)
1266-
1267-
assert_identical(original1.broadcast_like(original2),
1268-
expected1.transpose('y', 'x'))
1269-
1270-
assert_identical(original2.broadcast_like(original1),
1271-
expected2)
1259+
arr1 = DataArray(np.ones((2, 3)), dims=['x', 'y'],
1260+
coords={'x': ['a', 'b'], 'y': ['a', 'b', 'c']})
1261+
arr2 = DataArray(np.ones((3, 2)), dims=['x', 'y'],
1262+
coords={'x': ['a', 'b', 'c'], 'y': ['a', 'b']})
1263+
orig1, orig2 = broadcast(arr1, arr2)
1264+
new1 = arr1.broadcast_like(arr2)
1265+
new2 = arr2.broadcast_like(arr1)
1266+
1267+
assert orig1.identical(new1)
1268+
assert orig2.identical(new2)
1269+
1270+
orig3 = DataArray(np.random.randn(5), [('x', range(5))])
1271+
orig4 = DataArray(np.random.randn(6), [('y', range(6))])
1272+
new3, new4 = broadcast(orig3, orig4)
1273+
1274+
assert_identical(orig3.broadcast_like(orig4), new3.transpose('y', 'x'))
1275+
assert_identical(orig4.broadcast_like(orig3), new4)
12721276

12731277
def test_reindex_like(self):
12741278
foo = DataArray(np.random.randn(5, 6),

0 commit comments

Comments
 (0)