How to select coordiante values in xarray that are indexed with a hyphen string?

207 views Asked by At

Using PyPSA with the Linopy backend, I getting and xarray object that looks like the following:

Variable 'Link-p_nom':
----------------------

Variable labels:
array([ 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25,
       26, 27, 28])
Coordinates:
  * Link-ext  (Link-ext) object 'BE0 0 H2 Electrolysis' ... 'BE0 4 battery di...
Attributes:
    binary:   False

What I want to do is to filter out e.g. one value or a list of values.

I tried this as it is typically recommended in xarray: vars_link.sel(Link-ext="BE0 0 H2 Electrolysis")

but it gives me the following error:

   vars_link.sel(Link-ext="BE0 0 H2 Electrolysis")
                  ^^^^^^^^^
SyntaxError: expression cannot contain assignment, perhaps you meant "=="?
1

There are 1 answers

2
euronion On BEST ANSWER

.sel in general supports indexing using a dictonary which you can specify in multiple ways. The =-notation is one way. If you specify it using e.g. curly brackets {...} you can also use dimension names including special characters like hyphens:

vars_link.sel({"Link-ext":"BE0 0 H2 Electrolysis"})