linopy icon indicating copy to clipboard operation
linopy copied to clipboard

Accessing Variable.nvars fails if <empty>

Open FBumann opened this issue 7 months ago • 0 comments

Version Checks (indicate both or one)

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

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

Issue Description

If Variables.nvars is accessed, an Exception is raised when the container is empty. I would expect a return of 0. This can arise when checking the number of binaries of a Model with no binaries

Reproducible Example

import linopy
m = linopy.Model()
m.nvars

Expected Behavior

Expected

Return 0

Currently:

"""
Traceback (most recent call last):
  File "/Applications/PyCharm.app/Contents/plugins/python-ce/helpers/pydev/pydevconsole.py", line 364, in runcode
    coro = func()
  File "<input>", line 3, in <module>
  File "/Users/felix/Downloads/PythonProject8/.venv/lib/python3.9/site-packages/linopy/model.py", line 826, in nvars
    return self.variables.nvars
  File "/Users/felix/Downloads/PythonProject8/.venv/lib/python3.9/site-packages/linopy/variables.py", line 1300, in nvars
    return len(self.flat.labels.unique())
  File "/Users/felix/Downloads/PythonProject8/.venv/lib/python3.9/site-packages/linopy/variables.py", line 1424, in flat
    df = pd.concat([self[k].flat for k in self], ignore_index=True)
  File "/Users/felix/Downloads/PythonProject8/.venv/lib/python3.9/site-packages/pandas/core/reshape/concat.py", line 382, in concat
    op = _Concatenator(
  File "/Users/felix/Downloads/PythonProject8/.venv/lib/python3.9/site-packages/pandas/core/reshape/concat.py", line 445, in __init__
    objs, keys = self._clean_keys_and_objs(objs, keys)
  File "/Users/felix/Downloads/PythonProject8/.venv/lib/python3.9/site-packages/pandas/core/reshape/concat.py", line 507, in _clean_keys_and_objs
    raise ValueError("No objects to concatenate")
ValueError: No objects to concatenate
"""

Installed Versions

Bottleneck==1.4.2 click==8.1.8 cloudpickle==3.1.1 dask==2024.8.0 deprecation==2.1.0 fsspec==2025.3.0 importlib_metadata==8.6.1 linopy==0.5.2 locket==1.0.0 numexpr==2.10.2 numpy==1.26.4 packaging==24.2 pandas==2.2.3 partd==1.4.2 polars==1.26.0 python-dateutil==2.9.0.post0 pytz==2025.2 PyYAML==6.0.2 scipy==1.13.1 six==1.17.0 toolz==1.0.0 tqdm==4.67.1 tzdata==2025.2 xarray==2024.7.0 zipp==3.21.0

FBumann avatar Mar 29 '25 12:03 FBumann