Skip to content

Make the sel error more descriptive when method is unset - #6774

Merged
mathause merged 3 commits into
pydata:mainfrom
jsignell:sel-error-message
Jul 12, 2022
Merged

Make the sel error more descriptive when method is unset#6774
mathause merged 3 commits into
pydata:mainfrom
jsignell:sel-error-message

Conversation

@jsignell

Copy link
Copy Markdown
Member
  • Tests added

This came up in the tutorial and I was wondering if the error could be made a little clearer. Not sure if the error message should hint that a user might want to use method.

importnumpyasnpimportpandasaspdimportxarrayasxrarr=xr.DataArray(
data=np.arange(48).reshape(4, 2, 6),
dims=("u", "v", "time"),
coords={
"u": [-3.2, 2.1, 5.3, 6.5],
"v": [-1, 2.6],
"time": pd.date_range("2009-01-05", periods=6, freq="M"),
},
)
arr.sel(u=5, time="2009-04-28") # I removed `method="nearest"`

Before this PR:

...
KeyError: 5.0

After this PR:

...
KeyError: "not all values found in index 'u'. Did you mean to set `method=`?"

@dcheriandcherian left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @jsignell This is a nice improvement!

Comment threadxarray/core/indexes.py Outdated
Comment threadxarray/tests/test_dataarray.py Outdated
jsignelland others added 2 commits July 12, 2022 14:47
Co-authored-by: Deepak Cherian <dcherian@users.noreply.github.com>
@mathause
mathause merged commit 4aae7fd into pydata:mainJul 12, 2022
@mathause

Copy link
Copy Markdown
Collaborator

Thanks @jsignell!

@jsignell
jsignell deleted the sel-error-message branch July 13, 2022 14:49
dcherian added a commit to keewis/xarray that referenced this pull request Jul 22, 2022
* main: (313 commits)
Update whats-new
Release notes for v2022.06.0 (pydata#6815)
Drop multi-indexes when assigning to a multi-indexed variable (pydata#6798)
Support NumPy array API (experimental) (pydata#6804)
Add cumsum to DatasetGroupBy (pydata#6525)
Refactor groupby binary ops code. (pydata#6789)
Update DataArray.rename + docu (pydata#6665)
Switch to T_DataArray and T_Dataset in concat (pydata#6784)
Fix typos found by codespell (pydata#6794)
Update groupby attrs tests (pydata#6787)
Update map_blocks to use chunksizes property. (pydata#6776)
Fix `DataArrayRolling.__iter__` with `center=True` (pydata#6744)
[test-upstream] Update flox repo URL (pydata#6780)
Move _infer_meta_data and _parse_size to utils (pydata#6779)
Make the `sel` error more descriptive when `method` is unset (pydata#6774)
Move Rolling tests to their own testing module (pydata#6777)
[pre-commit.ci] pre-commit autoupdate (pydata#6773)
move da and ds fixtures to conftest.py (pydata#6730)
Bump EnricoMi/publish-unit-test-result-action from 1 to 2 (pydata#6770)
Type shape methods (pydata#6767)
...
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants

@jsignell@mathause@dcherian