Skip to content

Commit

Permalink
improve error for wrong key in dataset
Browse files Browse the repository at this point in the history
  • Loading branch information
Illviljan committed Jan 12, 2025
1 parent fc55271 commit ffc645e
Showing 1 changed file with 5 additions and 0 deletions.
5 changes: 5 additions & 0 deletions xarray/core/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -1611,6 +1611,11 @@ def __getitem__(
return self._construct_dataarray(key)
except KeyError as e:
message = f"No variable named {key!r}. Variables on the dataset include {shorten_list_repr(list(self.variables.keys()), max_items=10)}"

best_guess = utils.did_you_mean(key, self.variables.keys())
if best_guess:
message += f" {best_guess}"

# If someone attempts `ds['foo' , 'bar']` instead of `ds[['foo', 'bar']]`
if isinstance(key, tuple):
message += f"\nHint: use a list to select multiple variables, for example `ds[{list(key)}]`"
Expand Down

0 comments on commit ffc645e

Please sign in to comment.