arup-group / genet

Manipulate MATSim networks via a Python API.
MIT License
45 stars 9 forks source link

Bugfix: Multi-indexing fails in a very specific case #249

Closed KasiaKoz closed 1 week ago

KasiaKoz commented 2 weeks ago

Fixes: #248, in a wild turn of events, my projected 3 part PR series has grown to 4. I found this bug while working on #245. The fix is inversely proportional to the amount of time it took to diagnose and recreate the issue in a unit test..

I had to mock the method which gives the available multi-index for an edge in the graph because it would not give me the same behaviour as I was seeing with a larger dataset. The problem happens when this method outputs an available multi-index that is sandwiched by multi-indices already being used on the graph. In this small example it would always give me the largest index being used +1, which did not recreate the error.

Stripped from all the context, the problem was setting a dataframe with an output of a groupby (of the same dataframe) which has inherited indexing from the groupby operation. This did not match the indexing of the original dataframe, so when trying to set it back in, it resulted in loss of data.

KasiaKoz commented 1 week ago

I also added an extra assertion, which I hope makes things clearer