Skip to content

Commit f1746e9

Browse files
committed
- linopy/variables.py: ffill, bfill, sanitize — labels were cast back to int64 via astype(int), now use DEFAULT_LABEL_DTYPE. Also Variables.to_dataframe arange for
map_labels. - linopy/constraints.py: Constraints.to_dataframe arange for map_labels. - linopy/common.py: save_join outer-join fallback was casting to int64.
1 parent d0a8c74 commit f1746e9

File tree

3 files changed

+18
-7
lines changed

3 files changed

+18
-7
lines changed

linopy/common.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -460,7 +460,7 @@ def save_join(*dataarrays: DataArray, integer_dtype: bool = False) -> Dataset:
460460
)
461461
arrs = xr_align(*dataarrays, join="outer")
462462
if integer_dtype:
463-
arrs = tuple([ds.fillna(-1).astype(int) for ds in arrs])
463+
arrs = tuple([ds.fillna(-1).astype(DEFAULT_LABEL_DTYPE) for ds in arrs])
464464
return Dataset({ds.name: ds for ds in arrs})
465465

466466

linopy/constraints.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
)
5757
from linopy.config import options
5858
from linopy.constants import (
59+
DEFAULT_LABEL_DTYPE,
5960
EQUAL,
6061
GREATER_EQUAL,
6162
HELPER_DIMS,
@@ -1071,7 +1072,10 @@ def flat(self) -> pd.DataFrame:
10711072
return pd.DataFrame(columns=["coeffs", "vars", "labels", "key"])
10721073
df = pd.concat(dfs, ignore_index=True)
10731074
unique_labels = df.labels.unique()
1074-
map_labels = pd.Series(np.arange(len(unique_labels)), index=unique_labels)
1075+
map_labels = pd.Series(
1076+
np.arange(len(unique_labels), dtype=DEFAULT_LABEL_DTYPE),
1077+
index=unique_labels,
1078+
)
10751079
df["key"] = df.labels.map(map_labels)
10761080
return df
10771081

linopy/variables.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
to_polars,
5353
)
5454
from linopy.config import options
55-
from linopy.constants import HELPER_DIMS, TERM_DIM
55+
from linopy.constants import DEFAULT_LABEL_DTYPE, HELPER_DIMS, TERM_DIM
5656
from linopy.solver_capabilities import SolverFeature, solver_supports
5757
from linopy.types import (
5858
ConstantLike,
@@ -1066,7 +1066,9 @@ def ffill(self, dim: str, limit: None = None) -> Variable:
10661066
.map(DataArray.ffill, dim=dim, limit=limit)
10671067
.fillna(self._fill_value)
10681068
)
1069-
return self.assign_multiindex_safe(labels=data.labels.astype(int))
1069+
return self.assign_multiindex_safe(
1070+
labels=data.labels.astype(DEFAULT_LABEL_DTYPE)
1071+
)
10701072

10711073
def bfill(self, dim: str, limit: None = None) -> Variable:
10721074
"""
@@ -1093,7 +1095,7 @@ def bfill(self, dim: str, limit: None = None) -> Variable:
10931095
.map(DataArray.bfill, dim=dim, limit=limit)
10941096
.fillna(self._fill_value)
10951097
)
1096-
return self.assign(labels=data.labels.astype(int))
1098+
return self.assign(labels=data.labels.astype(DEFAULT_LABEL_DTYPE))
10971099

10981100
def sanitize(self) -> Variable:
10991101
"""
@@ -1104,7 +1106,9 @@ def sanitize(self) -> Variable:
11041106
linopy.Variable
11051107
"""
11061108
if issubdtype(self.labels.dtype, floating):
1107-
return self.assign(labels=self.labels.fillna(-1).astype(int))
1109+
return self.assign(
1110+
labels=self.labels.fillna(-1).astype(DEFAULT_LABEL_DTYPE)
1111+
)
11081112
return self
11091113

11101114
def equals(self, other: Variable) -> bool:
@@ -1525,7 +1529,10 @@ def flat(self) -> pd.DataFrame:
15251529
"""
15261530
df = pd.concat([self[k].flat for k in self], ignore_index=True)
15271531
unique_labels = df.labels.unique()
1528-
map_labels = pd.Series(np.arange(len(unique_labels)), index=unique_labels)
1532+
map_labels = pd.Series(
1533+
np.arange(len(unique_labels), dtype=DEFAULT_LABEL_DTYPE),
1534+
index=unique_labels,
1535+
)
15291536
df["key"] = df.labels.map(map_labels)
15301537
return df
15311538

0 commit comments

Comments
 (0)