Repository: PyPSA/linopy

Issue:
dimension error when multiplying variable/expression with numpy float
### Version Checks (indicate both or one)

- [x] I have confirmed this bug exists on the lastest [release](https://github.com/pypsa/linopy/releases) of Linopy.

- [ ] I have confirmed this bug exists on the current [`master`](https://github.com/pypsa/linopy/tree/master) branch of Linopy.


### Issue Description

When multiplying a variable or expression with a scalar numpy float, I receive a `ValueError: different number of dimensions on data and dims: 0 vs 1`.



### Reproducible Example

```python
import pandas as pd
from linopy import Model
import numpy as np

m = Model()
b = np.float64(5.0)
index = pd.Index(["a", "b", "c"], name="index")
m.add_variables(lower=0, name="var", coords=[index])
expr = m.variables["var"] * b

# workaround
expr = m.variables["var"] * b.item()
```

### Expected Behavior

No ValueError in `expr = m.variables["var"] * b`

### Installed Versions

<details>
Bottleneck==1.5.0
click==8.2.1
cloudpickle==3.1.1
colorama==0.4.6
dask==2025.7.0
deprecation==2.1.0
fsspec==2025.7.0
linopy==0.5.5
locket==1.0.0
numexpr==2.11.0
numpy==2.3.2
packaging==25.0
pandas==2.3.1
partd==1.4.2
polars==1.31.0
python-dateutil==2.9.0.post0
pytz==2025.2
PyYAML==6.0.2
scipy==1.16.1
setuptools==78.1.1
six==1.17.0
toolz==1.0.0
tqdm==4.67.1
tzdata==2025.2
wheel==0.45.1
xarray==2025.7.1
</details>


Discussion hints:
FabianHofmann: thanks @RobbieKiwi 

Solution patch:
diff --git a/doc/release_notes.rst b/doc/release_notes.rst
index 77de18d..ca6b95d 100644
--- a/doc/release_notes.rst
+++ b/doc/release_notes.rst
@@ -4,6 +4,7 @@ Release Notes
 .. Upcoming Version
 .. ----------------
 .. * Improved constraint equality check in `linopy.testing.assert_conequal` to less strict optionally
+.. * Minor bugfix for multiplying variables with numpy type constants
 
 Version 0.5.6
 --------------
diff --git a/linopy/common.py b/linopy/common.py
index a8bec09..0a08f64 100644
--- a/linopy/common.py
+++ b/linopy/common.py
@@ -254,7 +254,9 @@ def as_dataarray(
         arr = pandas_to_dataarray(arr, coords=coords, dims=dims, **kwargs)
     elif isinstance(arr, np.ndarray):
         arr = numpy_to_dataarray(arr, coords=coords, dims=dims, **kwargs)
-    elif isinstance(arr, np.number | int | float | str | bool | list):
+    elif isinstance(arr, np.number):
+        arr = DataArray(float(arr), coords=coords, dims=dims, **kwargs)
+    elif isinstance(arr, int | float | str | bool | list):
         arr = DataArray(arr, coords=coords, dims=dims, **kwargs)
 
     elif not isinstance(arr, DataArray):


Test patch:
diff --git a/test/test_common.py b/test/test_common.py
index 33d0771..8505948 100644
--- a/test/test_common.py
+++ b/test/test_common.py
@@ -369,6 +369,14 @@ def test_as_dataarray_with_number() -> None:
     assert list(da.coords["dim1"].values) == ["a"]
 
 
+def test_as_dataarray_with_np_number() -> None:
+    num = np.float64(1)
+    da = as_dataarray(num, dims=["dim1"], coords=[["a"]])
+    assert isinstance(da, DataArray)
+    assert da.dims == ("dim1",)
+    assert list(da.coords["dim1"].values) == ["a"]
+
+
 def test_as_dataarray_with_number_default_dims_coords() -> None:
     num = 1
     da = as_dataarray(num)