1

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 "=="?
Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
Max Parzen
  • 103
  • 1
  • 6

1 Answers1

2

.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"})
euronion
  • 1,142
  • 6
  • 14
  • Amazing! This is pypsa syntax but I could then also iterate through aka. select multiple values: ```vars_link.sel({"Link-ext": nodes + " discharger"})``` which would build something like: ```vars_link.sel({"Link-ext": ["1 discharger", "2 discharger"]})``` This resulted in: ``` array([24, 25, 26, 27, 28]) Coordinates: * Link-ext (Link-ext) object 'BE0 0 battery discharger' ... 'BE0 4 battery... Attributes: binary: False ``` – Max Parzen Dec 07 '22 at 10:28
  • 1
    Yes! As long it is a valid Python dictionary that you pass to `.sel(...)` you can choose any method you like to generate the dict. You can as value not only have a single string or list, but also e.g. slices. – euronion Dec 07 '22 at 10:42