Skip to content

Commit 09c34dd

Browse files
fix: _as_index() calls to_index() for DataArrays (#897)
* fix: _as_index() calls to_index() for DataArrays * test: add tests with tz-aware coords * chore: add release note entry --------- Co-authored-by: Peter Hron <peter.hron@second-foundation.eu>
1 parent f06738e commit 09c34dd

3 files changed

Lines changed: 23 additions & 4 deletions

File tree

‎doc/release_notes.rst‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ Version 0.9.1
8080
* ``Model.remove_variables`` no longer raises an ``IndexError`` on a quadratic objective. A quadratic term is dropped when any of its factors uses the removed variable. (`#883 <https://github.com/PyPSA/linopy/issues/883>`__, `#895 <https://github.com/PyPSA/linopy/pull/895>`__)
8181
* The Xpress interface no longer calls API functions deprecated in Xpress 9.8. (`#880 <https://github.com/PyPSA/linopy/pull/880>`__)
8282
* The ``docs`` extra no longer pins ``numpy<2``. (`#869 <https://github.com/PyPSA/linopy/pull/869>`__)
83-
83+
* Using timezone-aware ``DatetimeIndex`` coordinates in a model no longer raises a ``ValueError``. The index is now retrieved directly via ``DataArray.to_index()`` instead of rebuilding it with ``pd.Index()``, which dropped the timezone info and failed during alignment. (`#898 <https://github.com/PyPSA/linopy/issues/898>`__)
8484

8585
Version v0.9.0
8686
--------------

‎linopy/alignment.py‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -193,9 +193,11 @@ def _coords_to_dict(
193193

194194

195195
def _as_index(coord_values: Any) -> pd.Index:
196-
return (
197-
coord_values if isinstance(coord_values, pd.Index) else pd.Index(coord_values)
198-
)
196+
if isinstance(coord_values, DataArray):
197+
return coord_values.to_index()
198+
if isinstance(coord_values, pd.Index):
199+
return coord_values
200+
return pd.Index(coord_values)
199201

200202

201203
def _as_multiindex(coord_values: Any) -> pd.MultiIndex | None:

‎test/test_alignment.py‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -572,6 +572,17 @@ def test_invalid_entry_raises_typeerror(
572572
class TestAddVariablesCoords:
573573
"""End-to-end: each coords / dims form sets the variable's dimensions."""
574574

575+
@staticmethod
576+
def _datetime_coordinates(timezone: str | None) -> xr.Coordinates:
577+
time = pd.date_range(
578+
"2025-10-26 00:00",
579+
periods=5,
580+
freq="h",
581+
tz=timezone,
582+
name="time",
583+
)
584+
return xr.Coordinates({"time": time})
585+
575586
@pytest.mark.parametrize(
576587
"coords, dims, expected_dims",
577588
[
@@ -586,6 +597,9 @@ class TestAddVariablesCoords:
586597
([np.array([0, 1, 2])], None, ("dim_0",)),
587598
([pd.Index([0, 1, 2])], None, ("dim_0",)),
588599
([("origin", ["a", "b"]), ("dest", ["x", "y"])], None, ("origin", "dest")),
600+
(_datetime_coordinates(None), None, ("time",)),
601+
(_datetime_coordinates("UTC"), None, ("time",)),
602+
(_datetime_coordinates("Europe/Berlin"), None, ("time",)),
589603
],
590604
ids=[
591605
"tuple",
@@ -599,6 +613,9 @@ class TestAddVariablesCoords:
599613
"ndarray",
600614
"unnamed-index",
601615
"multiple-tuples",
616+
"xarray-datetime-naive",
617+
"xarray-datetime-utc",
618+
"xarray-datetime-dst",
602619
],
603620
)
604621
def test_coords_set_variable_dims(

0 commit comments

Comments
 (0)