question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

`sel` fails confusingly or silently when a dimension name matches an optional argument

See original GitHub issue

Given 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:open
  • Created 4 years ago
  • Comments:10 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
dcheriancommented, Sep 20, 2019

Yes add another kwarg

1reaction
dcheriancommented, Sep 20, 2019

+1. Though I prefer raising a more informative over raising a warning.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Named and Optional Arguments - C# Programming Guide
Named arguments enable you to specify an argument for a parameter by matching the argument with its name rather than with its position...
Read more >
CRFM Benchmarking
In order to predict electronic fail rates from cosmic particles, it is necessary to know the local cosmic ray flux. This paper reviews...
Read more >
4 Math: Problem-Solving in Functions and Algebra - CUNY
Students tend to think of mistakes in math as failures and as proof ... rectangles formed of these dimensions: 1 × 12, 2...
Read more >
ACT Prep Guide: - E.W. Grove School
The optional Writing test is a 40-minute assessment consisting of a single essay question. You will receive three different perspectives.
Read more >
Untitled
You type the command latex followed by the name of your document file (see ... This means that values such as parskip can...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found