Skip to content

Commit

Permalink
Merge branch 'v1.1.x'
Browse files Browse the repository at this point in the history
  • Loading branch information
simonbowly committed Feb 10, 2025
2 parents 37f4a4a + f291dd6 commit aa46653
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
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

0 comments on commit aa46653

Please sign in to comment.