pydata / pydata/xarray

Error when broadcast given int

Open
#7,459 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
Python
Stars
4.2k
Forks
1.4k
Avg merge
2d 15h
Merged PRs (30d)
14

Description

What happened?

Unhelpful error raised by xr.broadcast when supplied with an int.

What did you expect to happen?

The broadcast to succeed I think?

Minimal Complete Verifiable Example
In [1]: import xarray as xr

In [2]: da = xr.DataArray([5, 4], dims='x')

In [3]: xr.broadcast(da, 1)
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Cell In[3], line 1
----> 1 xr.broadcast(da, 1)

File ~/miniconda3/envs/xrdev3.9/lib/python3.9/site-packages/xarray/core/alignment.py:1049, in broadcast(exclude, *args)
   1047 if exclude is None:
   1048     exclude = set()
-> 1049 args = align(*args, join="outer", copy=False, exclude=exclude)
   1051 dims_map, common_coords = _get_broadcast_dims_map_common_coords(args, exclude)
   1052 result = [_broadcast_helper(arg, exclude, dims_map, common_coords) for arg in args]

File ~/miniconda3/envs/xrdev3.9/lib/python3.9/site-packages/xarray/core/alignment.py:772, in align(join, copy, indexes, exclude, fill_value, *objects)
    576 """
    577 Given any number of Dataset and/or DataArray objects, returns new
    578 objects with aligned indexes and dimension sizes.
   (...)
    762 
    763 """
    764 aligner = Aligner(
    765     objects,
    766     join=join,
   (...)
    770     fill_value=fill_value,
    771 )
--> 772 aligner.align()
    773 return aligner.results

File ~/miniconda3/envs/xrdev3.9/lib/python3.9/site-packages/xarray/core/alignment.py:556, in Aligner.align(self)
    553     self.results = (obj.copy(deep=self.copy),)
    554     return
--> 556 self.find_matching_indexes()
    557 self.find_matching_unindexed_dims()
    558 self.assert_no_index_conflict()

File ~/miniconda3/envs/xrdev3.9/lib/python3.9/site-packages/xarray/core/alignment.py:262, in Aligner.find_matching_indexes(self)
    259 objects_matching_indexes = []
    261 for obj in self.objects:
--> 262     obj_indexes, obj_index_vars = self._normalize_indexes(obj.xindexes)
    263     objects_matching_indexes.append(obj_indexes)
    264     for key, idx in obj_indexes.items():

AttributeError: 'int' object has no attribute 'xindexes'
MVCE confirmation
  • Minimal example — the example is as focused as reasonably possible to demonstrate the underlying issue in xarray.
  • Complete example — the example is self-contained, including all data and the text of any traceback.
  • Verifiable example — the example copy & pastes into an IPython prompt or Binder notebook, returning the result.
  • New issue — a search of GitHub Issues suggests this is not a duplicate.
Relevant log output

No response

Anything else we need to know?

This clearly has something to do with a change in the flexible indexes refactor, as it complains about .xindexes not being present. @benbovy

Environment

The main branch

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the MVCE with xr.broadcast(da, 1), then inspect xarray/core/alignment.py around broadcast(), align(), and Aligner.find_matching_indexes(), which the traceback identifies. Determine the intended handling of scalar integers and add coverage for this case; done means the example no longer raises the shown AttributeError and the behavior matches the project’s intended broadcast semantics.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
data
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.