Skip to content

Commit 6fdf766

Browse files
fix: ensure compat with pandas item selection and numpy >= 2.0 (#434)
* fix: ensure compat with pandas item selection and numpy >= 2.0 * revert unneeded condition
1 parent 4c5aabe commit 6fdf766

File tree

3 files changed

+11
-0
lines changed

3 files changed

+11
-0
lines changed

doc/release_notes.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ Release Notes
44
Upcoming Version
55
----------------
66

7+
**Bug Fixes**
8+
9+
* Fix the multiplication with of zero dimensional numpy arrays with linopy objects. This is mainly affecting operations where single numerical items from pandas objects are selected and used for multiplication.
10+
711
Version 0.5.1
812
--------------
913

linopy/common.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,10 @@ def numpy_to_dataarray(
205205
DataArray:
206206
The converted DataArray.
207207
"""
208+
# fallback case for zero dim arrays
209+
if arr.ndim == 0:
210+
return DataArray(arr.item(), coords=coords, dims=dims, **kwargs)
211+
208212
ndim = max(arr.ndim, 0 if coords is None else len(coords))
209213
if isinstance(dims, (Iterable, Sequence)):
210214
dims = list(dims)

test/test_linear_expression.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,9 @@ def test_linear_expression_with_multiplication(x: Variable) -> None:
199199
expr = np.array([1, 2]) * x
200200
assert isinstance(expr, LinearExpression)
201201

202+
expr = np.array(1) * x
203+
assert isinstance(expr, LinearExpression)
204+
202205
expr = xr.DataArray(np.array([[1, 2], [2, 3]])) * x
203206
assert isinstance(expr, LinearExpression)
204207

0 commit comments

Comments
 (0)