pulp
pulp copied to clipboard
Representation in `pd.DataFrame` display of `pulp.LpAffineExtression` variables
Details for the issue
What did you do?
Define a df pd.DataFrame with some values being pulp.LpAffineExtression variables.
What did you expect to see?
When displaying the df in a jupyter notebook it expect to see pulp.LpAffineExtression as the usual __repr__() of pulp.LpAffineExtression instances. Which is a mathematical linear combination.
What did you see instead?
When displaying the df in a jupyter notebook it shows the pulp.LpAffineExtression as dicts, instead of the usual __repr__() of pulp.LpAffineExtression instances. I assume this happens because pulp.LpAffineExtression inherit from a dict class.
Useful extra information
The info below often helps, please fill it out if you're able to. :)
What operating system are you using?
- [x] Mac OS: ( _version:12.4 )
I'm using python version:
- [x] Other: 3.9
I installed PuLP via:
- [x] pypi (python -m pip install pulp)
Did you also
- [x] Tried out the latest github version: https://github.com/coin-or/pulp
- [x] Searched for an existing similar issue: https://github.com/coin-or/pulp/issues?utf8=%E2%9C%93&q=is%3Aissue%20
can you show an example? I'm not sure I understand what you expect to see? Also, if you feel like doing a PR, I can check it. In any case, we do not have a pandas dependency so I'm not sure if we can test this.
Sure, this is a minimal working example to show it:
import pulp
import pandas as pd
from IPython.display import display
x = pulp.LpVariable.dicts("x", indices = range(3))
var_sum = pulp.lpSum(x.values())
print("Pulp representation")
print(var_sum)
print(var_sum.__repr__())
print("Pandas representation")
print(pd.Series([var_sum]))
print(pd.Series([var_sum]).to_frame())
display(pd.Series([var_sum]).to_frame())
As you can see, when I print/repr the affine expression it is displayed as a linear combination.
When the same variable is displayed in a pandas object, both pd.Series and pd.DataFrame it is shown as if it were a dict.
Probably it is because pulp.LpAffineExpression inherits from dict, and this is what pandas checks to select how to display it. But I have no idea how to solve it. The only hack I found is to use
df.applymap(lambda x: x.__repr__())