def w_opt(W):

    def filterer(x):
        v = x.range.values
tp = v[0]
lower, upper = tp
return lower <= x[column_name].sum() <= upper
return not W.groupby(level=0, axis=0).filter(filterer).empty
c_ = {'type': 'eq', 'fun': w_opt}
SciPy optimization with grouped bounds