Skip to content

Commit

Permalink
Fix automatic broadcasting when wrapping array api class (pydata#8669)
Browse files Browse the repository at this point in the history
* test automatic broadcasting

* fix bug

* whatsnew

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
TomNicholas and pre-commit-ci[bot] authored Jan 26, 2024
1 parent f5d22a6 commit 8704501
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
2 changes: 2 additions & 0 deletions doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ Deprecations
Bug fixes
~~~~~~~~~

- Fix bug with broadcasting when wrapping array API-compliant classes. (:issue:`8665`, :pull:`8669`)
By `Tom Nicholas <https://github.com/TomNicholas>`_.
- Ensure :py:meth:`DataArray.unstack` works when wrapping array API-compliant classes. (:issue:`8666`, :pull:`8668`)
By `Tom Nicholas <https://github.com/TomNicholas>`_.

Expand Down
3 changes: 2 additions & 1 deletion xarray/core/variable.py
Original file line number Diff line number Diff line change
Expand Up @@ -1476,7 +1476,8 @@ def set_dims(self, dims, shape=None):
tmp_shape = tuple(dims_map[d] for d in expanded_dims)
expanded_data = duck_array_ops.broadcast_to(self.data, tmp_shape)
else:
expanded_data = self.data[(None,) * (len(expanded_dims) - self.ndim)]
indexer = (None,) * (len(expanded_dims) - self.ndim) + (...,)
expanded_data = self.data[indexer]

expanded_var = Variable(
expanded_dims, expanded_data, self._attrs, self._encoding, fastpath=True
Expand Down
16 changes: 16 additions & 0 deletions xarray/tests/test_array_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,22 @@ def test_broadcast(arrays: tuple[xr.DataArray, xr.DataArray]) -> None:
assert_equal(a, e)


def test_broadcast_during_arithmetic(arrays: tuple[xr.DataArray, xr.DataArray]) -> None:
np_arr, xp_arr = arrays
np_arr2 = xr.DataArray(np.array([1.0, 2.0]), dims="x")
xp_arr2 = xr.DataArray(xp.asarray([1.0, 2.0]), dims="x")

expected = np_arr * np_arr2
actual = xp_arr * xp_arr2
assert isinstance(actual.data, Array)
assert_equal(actual, expected)

expected = np_arr2 * np_arr
actual = xp_arr2 * xp_arr
assert isinstance(actual.data, Array)
assert_equal(actual, expected)


def test_concat(arrays: tuple[xr.DataArray, xr.DataArray]) -> None:
np_arr, xp_arr = arrays
expected = xr.concat((np_arr, np_arr), dim="x")
Expand Down

0 comments on commit 8704501

Please sign in to comment.