Skip to content

Commit 61ad751

Browse files
author
Robbie Muir
committed
minor changes
1 parent 3807adb commit 61ad751

File tree

4 files changed

+39
-1
lines changed

4 files changed

+39
-1
lines changed

doc/release_notes.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ Release Notes
33

44
.. Upcoming Version
55
6+
* Fix warning when multiplying variables with pd.Series containing time-zone aware index
67
* Fix compatibility for xpress versions below 9.6 (regression)
78
* Performance: Up to 50x faster ``repr()`` for variables/constraints via O(log n) label lookup and direct numpy indexing
89
* Performance: Up to 46x faster ``ncons`` property by replacing ``.flat.labels.unique()`` with direct counting

linopy/common.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import numpy as np
1919
import pandas as pd
2020
import polars as pl
21+
import xarray as xr
2122
from numpy import arange, signedinteger
2223
from xarray import DataArray, Dataset, apply_ufunc, broadcast
2324
from xarray import align as xr_align
@@ -124,6 +125,17 @@ def get_from_iterable(lst: DimsLike | None, index: int) -> Any | None:
124125
return lst[index] if 0 <= index < len(lst) else None
125126

126127

128+
def try_to_convert_to_pd_datetime_index(
129+
coord: xr.DataArray | Sequence | pd.Index | Any,
130+
) -> pd.DatetimeIndex | xr.DataArray | Sequence | pd.Index | Any:
131+
try:
132+
if isinstance(coord, xr.DataArray):
133+
return coord.to_index()
134+
return pd.DatetimeIndex(coord) # type: ignore
135+
except Exception:
136+
return coord
137+
138+
127139
def pandas_to_dataarray(
128140
arr: pd.DataFrame | pd.Series,
129141
coords: CoordsLike | None = None,
@@ -164,7 +176,10 @@ def pandas_to_dataarray(
164176
shared_dims = set(pandas_coords.keys()) & set(coords.keys())
165177
non_aligned = []
166178
for dim in shared_dims:
179+
pd_coord = pandas_coords[dim]
167180
coord = coords[dim]
181+
if isinstance(pd_coord, pd.DatetimeIndex):
182+
coord = try_to_convert_to_pd_datetime_index(coord)
168183
if not isinstance(coord, pd.Index):
169184
coord = pd.Index(coord)
170185
if not pandas_coords[dim].equals(coord):

test/test_constraint.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
import polars as pl
1313
import pytest
1414
import xarray as xr
15-
import xarray.core
1615
from xarray.testing import assert_equal
1716

1817
import linopy

test/test_variables.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
This module aims at testing the correct behavior of the Variables class.
44
"""
55

6+
import warnings
7+
from datetime import UTC, datetime
8+
69
import numpy as np
710
import pandas as pd
811
import pytest
@@ -122,3 +125,23 @@ def test_scalar_variable(m: Model) -> None:
122125
x = ScalarVariable(label=0, model=m)
123126
assert isinstance(x, ScalarVariable)
124127
assert x.__rmul__(x) is NotImplemented # type: ignore
128+
129+
130+
def test_timezone_alignment_with_multiplication() -> None:
131+
utc_index = pd.date_range(
132+
start=datetime(2025, 1, 1),
133+
freq="15min",
134+
periods=4,
135+
tz=UTC,
136+
name="time",
137+
)
138+
model = Model()
139+
series1 = pd.Series(index=utc_index, data=1.0)
140+
var1 = model.add_variables(coords=[utc_index], name="var1")
141+
142+
with warnings.catch_warnings():
143+
warnings.simplefilter("error")
144+
expr = var1 * series1
145+
index: pd.DatetimeIndex = expr.coords["time"].to_index() # type: ignore
146+
assert index.equals(utc_index)
147+
assert index.tzinfo is UTC

0 commit comments

Comments
 (0)