Skip to content

Commit 3c01ced

Browse files
authored
DOC: Clarify error message in open_dataarray (#9637)
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.
1 parent 0c1d02e commit 3c01ced

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

xarray/backends/api.py

+9-5
Original file line numberDiff line numberDiff line change
@@ -810,11 +810,15 @@ def open_dataarray(
810810
)
811811

812812
if len(dataset.data_vars) != 1:
813-
raise ValueError(
814-
"Given file dataset contains more than one data "
815-
"variable. Please read with xarray.open_dataset and "
816-
"then select the variable you want."
817-
)
813+
if len(dataset.data_vars) == 0:
814+
msg = "Given file dataset contains no data variables."
815+
else:
816+
msg = (
817+
"Given file dataset contains more than one data "
818+
"variable. Please read with xarray.open_dataset and "
819+
"then select the variable you want."
820+
)
821+
raise ValueError(msg)
818822
else:
819823
(data_array,) = dataset.data_vars.values()
820824

0 commit comments

Comments
 (0)