Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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")

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

but it gives me the following error:

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

>Solution :

.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"})
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading