Skip to content

Commit 3fb5b6c

Browse files
fix: support more than one initial letters in variable and constraint naming (#397)
1 parent e5cc814 commit 3fb5b6c

File tree

3 files changed

+22
-4
lines changed

3 files changed

+22
-4
lines changed

doc/release_notes.rst

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
Release Notes
22
=============
33

4-
.. Upcoming Version
5-
.. ----------------
4+
Upcoming Version
5+
----------------
6+
7+
* Solution files that following a different naming scheme of variables and constraints using more than on initial letter in the prefix (e.g. `col123`, `row456`) are now supported.
68

79
Version 0.4.3
810
--------------
911

10-
* When creating slices for variables and constraints (important for the `solve` function), the slicing is now fixed in case now dimension to slice is available.
12+
* When creating slices for variables and constraints (important for the `solve` function), the slicing is now fixed in case no dimension to slice is available.
1113
* Added a pandas priority attribute. With this change, the operation with pandas objects is now prioritizing linopy objects over pandas objects. This is useful when the using linopy objects in arithmetic operations with pandas objects, e.g. `a * x` where `a` is a pandas Series/DataFrame and `x` is a linopy variable.
1214
* The method :meth:`model.to_file <linopy.model.Model.to_file>` now includes a progress argument to enable or disable the progress bar while writing.
1315

linopy/common.py

+13
Original file line numberDiff line numberDiff line change
@@ -547,6 +547,19 @@ def _remap(array, mapping):
547547
return mapping[array.ravel()].reshape(array.shape)
548548

549549

550+
def count_initial_letters(word: str):
551+
"""
552+
Count the number of initial letters in a word.
553+
"""
554+
count = 0
555+
for char in word:
556+
if char.isalpha():
557+
count += 1
558+
else:
559+
break
560+
return count
561+
562+
550563
def replace_by_map(ds, mapping):
551564
"""
552565
Replace values in a DataArray by a one-dimensional mapping.

linopy/solvers.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import pandas as pd
2222
from pandas.core.series import Series
2323

24+
from linopy.common import count_initial_letters
2425
from linopy.constants import (
2526
Result,
2627
Solution,
@@ -138,7 +139,9 @@ def set_int_index(series: Series) -> Series:
138139
"""
139140
Convert string index to int index.
140141
"""
141-
series.index = series.index.str[1:].astype(int)
142+
if not series.empty:
143+
cutoff = count_initial_letters(str(series.index[0]))
144+
series.index = series.index.str[cutoff:].astype(int)
142145
return series
143146

144147

0 commit comments

Comments
 (0)