Repository: PyPSA/linopy

Issue:
Fix print out of empty LinearExpression

Discussion:

Patch:
diff --git a/doc/release_notes.rst b/doc/release_notes.rst
index bd4b5ee..778cfd6 100644
--- a/doc/release_notes.rst
+++ b/doc/release_notes.rst
@@ -1,8 +1,10 @@
 Release Notes
 =============
 
-.. Upcoming Release
-.. ----------------
+Upcoming Release
+----------------
+
+* Fix representation of empty variables and linear expressions.
 
 
 Version 0.1.3
diff --git a/linopy/expressions.py b/linopy/expressions.py
index bfa4439..ae0be11 100644
--- a/linopy/expressions.py
+++ b/linopy/expressions.py
@@ -226,6 +226,9 @@ class LinearExpression:
         truncate = nexprs > max_print
         coords = self.unravel_coords(to_print)
 
+        if not self.size:
+            return f"{header}\nNone"
+
         # loop over all values to print
         coord_strings = []
         expr_strings = []
diff --git a/linopy/variables.py b/linopy/variables.py
index d5ef25e..1620be9 100644
--- a/linopy/variables.py
+++ b/linopy/variables.py
@@ -229,6 +229,9 @@ class Variable:
             labels = np.ravel(self.labels.values)
             coords = [[c.item() for c in self.coords.values()]]
 
+        if not self.size:
+            return f"{header}\nNone"
+
         coord_strings = []
         var_strings = []
         bound_strings = []


Test patch:
diff --git a/test/test_repr.py b/test/test_repr.py
index c7b1cff..a4f7e64 100644
--- a/test/test_repr.py
+++ b/test/test_repr.py
@@ -131,6 +131,12 @@ def test_constraint_repr():
         repr(con)
 
 
+def test_empty_repr():
+    repr(u.loc[[]])
+    repr(lu.sel(dim_0=[]))
+    repr(lu.sel(dim_0=[]) >= 0)
+
+
 def test_print_options():
     for o in [v, lv, cv_, cv]:
         default_repr = repr(o)