Forum Discussion

FICO's avatar
FICO
Community Manager
5 years ago

Storing python xp.constraint objects in numpy arrays or pandas Series

We would like to store a list of xpress xp.constraint objects in a pandas Series. We have found that the xp.constraint objects cannot be stored in numpy arrays or pandas Series. Here is example code:

import xpress as xp
import numpy as np
import pandas as pd
 
v = xp.var("v")
ct = xp.constraint(v >= 0)
ctl = [ct]
try:
    a = np.array(ctl)
except Exception as e:
    print("Exception for np array is ", e, " type is ", type(e))
try:
    s = pd.Series(ctl)
except Exception as e:
    print("Exception for series is ", e, " type is ", type(e))

This code prints:

Exception for np array is  Incorrect member name of type xpress.constraint  type is  <class 'xpress.InterfaceError'>
Exception for series is  Incorrect member name of type xpress.constraint  type is  <class 'xpress.InterfaceError'>

I was able to figure out that you have overridden the `__getattr__()` method to raise a different exception than the python standard of `AttributeError`. Both `numpy` and `pandas` are looking for that exception when trying to see if the underlying object has certain methods.

Can you change the implementation of `xp.constraint` to allow the above code to work so that we can store a list of `xp.constraint` objects in pandas data structures?

2 Replies

  • FICO's avatar
    FICO
    Community Manager

    I cannot replicate that behavior, the code you submitted runs fine on two platforms: Mac (pandas 1.2.0 and numpy 1.19.5) and Linux (pandas 1.2.2 and numpy 1.20.1) using xpress 8.12. Can you provide more details on the pandas and numpy (and xpress) versions you are using?

    Note that as of version 8.9 we introduced a dtype for numpy arrays of constraints, and it's called xpress.npconstraint. So the declaration of the numpy array should be

    a = np.array(ctl, dtype=xp.npconstraint)

    This could then be added as a pandas series, which accepts numpy arrays.

  • FICO's avatar
    FICO
    Community Manager

    I am using Xpress 8.11.3 conda package. Seems like it is fixed in 8.12 . So that explains it.

    Unfortunately, that numpy array with a dtype of `xp.npconstraint` can't be put in a pandas `Series` in 8.11.3:

    import xpress as xp
    import numpy as np
    import pandas as pd
     
    v = xp.var("v")
    ct = xp.constraint(v >= 0)
    ctl = [ct]
     
    na = np.array(ctl, dtype=xp.npconstraint)
    print("na is ", na)
    s2 = pd.Series(na)
    print("s2 is ", s2)

    This produces:

      File "/home/ubuntu/anaconda3/envs/optiwrap37/lib/python3.7/site-packages/pandas/core/series.py", line 307, in __init__
        "Cannot construct a Series from an ndarray with "
    ValueError: Cannot construct a Series from an ndarray with compound dtype.  Use DataFrame instead.

    Using `DataFrame` doesn't work either.