Skip to content

Commit

Permalink
DOC: Clarify error message in open_dataarray
Browse files Browse the repository at this point in the history
If the file is empty (or contains no variables matching any filtering done by the backend), use a different error message indicating that, rather than suggesting that the file has too many variables for this function.
  • Loading branch information
DWesl authored Oct 16, 2024
1 parent 0c1d02e commit b722aa1
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions xarray/backends/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -810,11 +810,15 @@ def open_dataarray(
)

if len(dataset.data_vars) != 1:
raise ValueError(
"Given file dataset contains more than one data "
"variable. Please read with xarray.open_dataset and "
"then select the variable you want."
)
if len(dataset.data_vars) == 0:
msg = "Given file dataset contains no data variables."
else:
msg = (
"Given file dataset contains more than one data "
"variable. Please read with xarray.open_dataset and "
"then select the variable you want."
)
raise ValueError(msg)
else:
(data_array,) = dataset.data_vars.values()

Expand Down

0 comments on commit b722aa1

Please sign in to comment.