nebius/SWE-rebench-V2-PRs huggingface dataset:
Repository: PyPSA/linopy
Pull request #434 (2025-03-24 08:35:10)

fix: ensure compat with pandas item selection and numpy >= 2.0
Closes # (if applicable).

## Changes proposed in this Pull Request


## Checklist

- [x] Code changes are sufficiently documented; i.e. new functions contain docstrings and further explanations may be given in `doc`.
- [x] Unit tests for new features were added (if applicable).
- [x] A note for the release notes `doc/release_notes.rst` of the upcoming release is included.
- [x] I consent to the release of this PR's code under the MIT license.



Patch:
diff --git a/doc/release_notes.rst b/doc/release_notes.rst
index 263a530..6d8aaf9 100644
--- a/doc/release_notes.rst
+++ b/doc/release_notes.rst
@@ -4,6 +4,10 @@ Release Notes
 Upcoming Version
 ----------------
 
+**Bug Fixes**
+
+* 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.
+
 Version 0.5.1
 --------------
 
diff --git a/linopy/common.py b/linopy/common.py
index 336e06c..3e2449f 100644
--- a/linopy/common.py
+++ b/linopy/common.py
@@ -205,6 +205,10 @@ def numpy_to_dataarray(
         DataArray:
             The converted DataArray.
     """
+    # fallback case for zero dim arrays
+    if arr.ndim == 0:
+        return DataArray(arr.item(), coords=coords, dims=dims, **kwargs)
+
     ndim = max(arr.ndim, 0 if coords is None else len(coords))
     if isinstance(dims, (Iterable, Sequence)):
         dims = list(dims)


Test patch:
diff --git a/test/test_linear_expression.py b/test/test_linear_expression.py
index 7f81fcf..3ed9482 100644
--- a/test/test_linear_expression.py
+++ b/test/test_linear_expression.py
@@ -199,6 +199,9 @@ def test_linear_expression_with_multiplication(x: Variable) -> None:
     expr = np.array([1, 2]) * x
     assert isinstance(expr, LinearExpression)
 
+    expr = np.array(1) * x
+    assert isinstance(expr, LinearExpression)
+
     expr = xr.DataArray(np.array([[1, 2], [2, 3]])) * x
     assert isinstance(expr, LinearExpression)