Skip to content

timezone-aware coords do not build after version 0.8.0 #898

Description

@peterhron

Version Checks (indicate both or one)

  • I have confirmed this bug exists on the lastest release of Linopy.

  • I have confirmed this bug exists on the current master branch of Linopy.

Issue Description

since version 0.8.0, a model fails to build if timezone-aware time coords are used. The regression happens in add_variables(), where a strict broadcast_to_coords() validation was introduced by #732.

The expected and actual indexes for validation are constructed differently:

expected_idx = pd.Index(coord_values) # this happens in alignment.py::_as_index() helper
actual_idx = arr.coords[dim].to_index()

For a timezone-aware xarray DataArray, the first conversion produces an object index of integer nanoseconds, while the second produces the original timezone-aware pandas DatetimeIndex. The equality check therefore fails even though both coordinates originate from the same input.

Reproducible Example

import linopy
import pandas as pd
import xarray as xr

time = pd.date_range("2026-01-01", periods=2, tz="Europe/Prague", name="time")
coords = xr.Coordinates({"time": time})

model = linopy.Model()
model.add_variables(coords=coords, name="x") # ValueError

Expected Behavior

validation should pass and model should be created with the timezone-aware coord. A minimal fix could be to adapt _as_index() helper in alignment.py to call to_index() for DataArrays, which retrieves the pandas index directly (as compared to the pd.Index constructor, which goes through numpy arrays, deleting the timezone in the process).

I propose this small change in #897.

Installed Versions

Details Python: 3.12.10 Linopy: 0.8.0 xarray: 2025.9.1 pandas: 2.3.3 NumPy: 2.3.5

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions