Skip to content

Can not create constraints when a coordinate is named "sign" #359

@tom-welfonder

Description

@tom-welfonder

When I name a coordinate "sign", an error is raised when I try to create an constraint.

Here a short example to reproduce the issue:

import pandas as pd
import linopy

coords = [pd.Index(range(10), name="sign")]

m = linopy.Model()
x = m.add_variables(lower=0, coords=coords, name="x")
x>=0

Error message:

File ~/.env/lib/python3.10/site-packages/linopy/constraints.py:295, in __repr__(self)
    293         else:
    294             line = f"{print_coord(coord)}: None"
--> 295         lines.append(line)
    296 lines = align_lines_by_delimiter(lines, list(SIGNS_pretty.values()))
    298 shape_str = ", ".join(f"{d}: {s}" for d, s in zip(dims, dim_sizes))

KeyError: 0

When I add at least one additional coordinate to coords, a different error is raised:

File ~/.env/lib/python3.10/site-packages/linopy/constraints.py:295, in __repr__(self)
    293         else:
    294             line = f"{print_coord(coord)}: None"
--> 295         lines.append(line)
    296 lines = align_lines_by_delimiter(lines, list(SIGNS_pretty.values()))
    298 shape_str = ", ".join(f"{d}: {s}" for d, s in zip(dims, dim_sizes))

IndexError: too many indices for array: array is 1-dimensional, but 2 were indexed

I was able to reproduce some (different) errors for the names: "labels", "coeffs", "vars", "rhs". I found these in constraint.py in line 671:

dataset_attrs = ["labels", "coeffs", "vars", "sign", "rhs"]

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions