Skip to content

Accessing Variable.nvars fails if <empty> #436

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
2 tasks done
FBumann opened this issue Mar 29, 2025 · 0 comments
Open
2 tasks done

Accessing Variable.nvars fails if <empty> #436

FBumann opened this issue Mar 29, 2025 · 0 comments
Labels
bug Something isn't working

Comments

@FBumann
Copy link
Contributor

FBumann commented Mar 29, 2025

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

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 FBumann added the bug Something isn't working label Mar 29, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant