Forum Discussion
FICO
5 years agoCommunity Manager
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
- FICOCommunity 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.
- FICOCommunity 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.
Related Content
Related Content
How to apply termination conditions to each objective function in multi-objective model
Hello everyone, I am implementing a multi-objective model in Python, and I would like to set a time limit for each objective function. It would be best if I could set different time limits for each,...1 year agoPlace FICO® Xpress Optimization ForumFICO® Xpress Optimization Forum4 unread comments4 unread3Views0likes4CommentsVariable object mapping issue in Python Callbacks with Presolve enabled
Hi, I am implementing a Branch-and-Cut algorithm using the Xpress Python interface. I have structured my code as a class where I create variables (using xp.var) and store them in a dictionary attrib...9 months agoPlace FICO® Xpress Optimization ForumFICO® Xpress Optimization Forum6 unread comments6 unread6Views0likes6Comments