Decision Optimization

Decision Optimization

Delivers prescriptive analytics capabilities and decision intelligence to improve decision-making.


#Analytics
#DecisionOptimization
#DecisionOptimization
 View Only
  • 1.  Multiplication in Pandas Using DocPlex

    Posted 02/11/19 08:03 PM

    Originally posted by: JPRyan


    I have a very simple test Pandas DataFrame

     

        from docplex.mp.advmodel import AdvModel
        from pandas.core.series import Series
        from pandas.core.frame import DataFrame

     

        mdl = AdvModel(name='Test')
        target = Series(mdl.continuous_var_list(['Asset'], name='Target', lb=0, ub=1), index=['Asset'])
        frame = DataFrame(1, ['Asset'], ['Column'])
        print(frame)
        print(frame * target)

     

    but since I updated to pandas 0.24.1 from 0.23.4 this multiplication no longer works.  I get the error:

     

    DOcplexException: Multiply expects variable, expr or number, array([1.]) was passed (type is <class 'numpy.ndarray'>)

     

    When I revert to 0.23.4 it works again.  Any idea what is going on here?


    #CPLEXOptimizers
    #DecisionOptimization


  • 2.  Re: Multiplication in Pandas Using DocPlex

    Posted 02/27/19 08:20 AM

    Originally posted by: Viu-Long Kong


    Hi,

     

    Continous variables in docplex override the __rmul__ operator, and it this case, that operator gets a parameter it does not now what to do with. When evaluating frame * target,

    it is called with a [NaN] value (while we were expecting [1] or 1, from you frame.

    It looks like multiplication of a DataFrame by a Series is not well defined in pandas, leading to this.

    Ruling out docplex, if I execute:

    f1 = DataFrame(data=[2], index=['Asset'], columns=['Column'])
    s1 = Series([7], index=['Asset'])

    print(f1*s1)

    the result is:

           Asset  Column
    Asset    NaN     NaN
    

    I don't know what was your initial intent, but maybe you meant f1.apply(lambda col: col *s1) (or frame.apply(lambda col: col * target) in your example ?)

    print(f1.apply(lambda col: col* s1))

           Column
    Asset      14
    

    #CPLEXOptimizers
    #DecisionOptimization


  • 3.  Re: Multiplication in Pandas Using DocPlex

    Posted 02/28/19 06:19 PM

    Originally posted by: JPRyan


    Thanks for the response.  You are correct, I had the wrong multiplication in my toy problem

     

    The correct call in Pandas would be 

      frame.mul(target, axis=0)

     

    which solved the issue.  Many thanks.


    #CPLEXOptimizers
    #DecisionOptimization