Skip to content

Unusual behavior when using Pyomo and Pandas (?) #68

@kdheepak

Description

@kdheepak

Hi all,

I'm getting an extremely unusual behavior when using Pyomo with a Pandas Series object. Specifically, when I convert a Pandas Series object to a dictionary and pass it to a Param initialization, I get a error when I create a constraint rule.

import pandas as pd
from pyomo.environ import *

model = ConcreteModel()

model.Set1 = Set(initialize=[0, 1, 2, 3, 4, 5])

# model.Parameter1 = Param(model.Set1, initialize=pd.Series({0: 400.0, 1: 0.0, 2: 0.0, 3: 0.0, 4: 0.0, 5: 240.0}).to_dict())
model.Parameter1 = Param(model.Set1, initialize={0: 400.0, 1: 0.0, 2: 0.0, 3: 0.0, 4: 0.0, 5: 240.0})

model.Variable1 = Var(model.Set1, initialize=0)

def rule(m, l):
    return -m.Parameter1[l] <= m.Variable1[l]

model.Constraint1 = Constraint(model.Set1, rule=rule)

The above works fine. However, if I replace the initialize argument for Parameter1 with a Series object converted to a dictionary, I get the following error.

ERROR: Constructing component 'Constraint1' from data=None failed:
        ValueError: Constraint 'Constraint1[0]' does not have a proper value. Found 'True'
        Expecting a tuple or equation. Examples:
           summation(model.costs) == model.income
           (0, model.price[item], 50)
Traceback (most recent call last):
  File "test.py", line 21, in <module>
    model.Constraint1 = Constraint(model.Set1, rule=rule)
  File "/Users/$USER/anaconda/lib/python2.7/site-packages/pyomo/core/base/block.py", line 483, in __setattr__
    self.add_component(name, val)
  File "/Users/$USER/anaconda/lib/python2.7/site-packages/pyomo/core/base/block.py", line 849, in add_component
    val.construct(data)
  File "/Users/$USER/anaconda/lib/python2.7/site-packages/pyomo/core/base/constraint.py", line 752, in construct
    cdata = self._check_skip_add(ndx, tmp)
  File "/Users/$USER/anaconda/lib/python2.7/site-packages/pyomo/core/base/constraint.py", line 900, in _check_skip_add
    condata.set_value(expr)
  File "/Users/$USER/anaconda/lib/python2.7/site-packages/pyomo/core/base/constraint.py", line 442, in set_value
    raise ValueError(msg)
ValueError: Constraint 'Constraint1[0]' does not have a proper value. Found 'True'
Expecting a tuple or equation. Examples:
   summation(model.costs) == model.income
   (0, model.price[item], 50)

In the example above, I've commented out the line that converts a dictionary to a Series object and back to a dictionary. To reproduce this error, uncomment that line and comment the following line.

I'm using Pyomo 4.4.1 and Pandas 0.17.1

I've also verified that the two arguments are equivalent.

In [1]: import pandas as pd

In [2]: {0: 400.0, 1: 0.0, 2: 0.0, 3: 0.0, 4: 0.0, 5: 240.0} == pd.Series({0: 400.0, 1: 0.0, 2: 0.0, 3: 0.0, 4: 0.0, 5: 240.0}).to_dict()
Out[2]: True

Any idea what is going on? Most of the data I have is in Pandas Dataframes / Series. It is puzzling to me why this does not work.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions