`sel` fails confusingly or silently when a dimension name matches an optional argument
See original GitHub issueGiven that many Xarray methods accept dict
or keyword arguments, this may effect other methods. That said, here is a minimal example with sel
.
Minimal example
If I want to select based on a dimension named method
, I cannot because Xarray thinks method
is the method
argument to sel
:
>>> da1 = xr.DataArray(range(3), dims=['method'], coords={'method': range(3)})
>>> da1
<xarray.DataArray (method: 3)>
array([0, 1, 2])
Coordinates:
* method (method) int64 0 1 2
>>> da1.sel(method=0)
...
TypeError: ``method`` must be a string
And if the method
dimension has string labels, this fails silently:
>>> da2 = xr.DataArray(range(3), dims=['method'], coords={'method': list('abc')})
>>> da2.sel(method='a')
<xarray.DataArray (method: 3)>
array([0, 1, 2])
Coordinates:
* method (method) <U1 'a' 'b' 'c'
Expected Output
I think raising a ValueError
and providing a clarifying error message is the right call, but maybe the maintainers have a different opinion. At the very least, it seems like Xarray could note that the DataArray
instance has a dimension that matches one of the function arguments and ask the user to use dict
-like arguments if required.
I imagine a general error handling function could be written to check this for any function and DataArray
pair.
Issue Analytics
- State:
- Created 4 years ago
- Comments:10 (7 by maintainers)
Top GitHub Comments
Yes add another kwarg
+1. Though I prefer raising a more informative over raising a warning.