Skip to content
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

Fix integer-valued attributes bug #99

Merged
merged 1 commit into from
Feb 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/gurobipy_pandas/accessors.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ def _convert_single_value(value):
"""Return a numeric casted value if possible, otherwise return a string.
If value is neither a string nor something we can cast to numeric, then
raise a TypeError."""
if isinstance(value, int):
return value

try:
return float(value)
except ValueError:
Expand Down
21 changes: 21 additions & 0 deletions tests/test_accessors.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,16 @@ def test_setattr_dataframe(self):
with self.assertRaises(TypeError):
x.gppd.Start = pd.DataFrame(index=index, data={"start": [1, 2, 3, 4, 5]})

def test_setattr_single_int(self):
index = pd.Index([0, 1])
x = gppd.add_vars(self.model, index)
x.gppd.Partition = 0

def test_setattr_series_int(self):
index = pd.Index([0, 1])
x = gppd.add_vars(self.model, index)
x.gppd.Partition = pd.Series(index=index, data=[0, 1])


class TestSeriesGetAttrSetAttr(GurobiModelTestCase):
def test_var_getattr_X(self):
Expand All @@ -225,6 +235,17 @@ def test_var_setattr_bounds(self):
self.assertEqual(result.loc[i + 5].lb, 1.0)
self.assertEqual(result.loc[i + 5].ub, i + 2)

def test_var_setattr_bounds_fractional(self):
index = pd.RangeIndex(5, 10)
x = gppd.add_vars(self.model, index, name="x")
lb = 1.4
ub = pd.Series(index=index, data=[2.2, 3.3, 4.4, 5.5, 6.6])
result = x.gppd.set_attr("LB", lb).gppd.set_attr("UB", ub)
self.model.update()
for i in range(5):
self.assertEqual(result.loc[i + 5].lb, 1.4)
self.assertLess(abs(result.loc[i + 5].ub - (i + 2) * 1.1), 1e-6)

def test_var_setattr_vtype(self):
index = pd.RangeIndex(5, 10)
x = gppd.add_vars(self.model, index, name="x")
Expand Down