Ref: https://github.com/pandas-dev/pandas/pull/53049#issuecomment-1534094939
The following does not raise:
index1 = pd.MultiIndex(levels=[['a', 'b']], codes=[[0, 1]])
index2 = pd.MultiIndex(levels=[['b', 'a']], codes=[[1, 0]])
tm.assert_index_equal(index1, index2)
In some sense, these indices are equal. However, levels
is sometimes used as an order in certain ops, and if this is not consistently set then inconsistencies can arise. See #53009 for an example.
It seems to me that because of this, we should not consider these indices equal.
Comment From: attack68
Is there a concept of ordering here. Pandas seems to have different behaviour (index slicing as an example) dependent upon whether the index is montonic or not, and also if it has unique values or not.
In Python two lists are equal if all their items and ordering are equal but two sets are equal if their elements are the same.
Im aware that these methods sometimes have parameters to loosen their assertions. It may be than a non-montonic index can be equal to another non-montonic index even if the values are the same but in a different order. On the other hand I wouldn't expect a monotic index to be compared to a non-montonic index if it had the same elements, becuase different indexing behaviour can result.
Comment From: phofl
Yeah agreed, this should raise if they are not in the same order
Comment From: rhshadrach
@attack68 - I'm not sure I follow your comment / question. But just to make sure, the two indices in the OP have the same order, it is only the order of the levels that differ.
``` index1 = pd.MultiIndex(levels=[['a', 'b']], codes=[[0, 1]]) index2 = pd.MultiIndex(levels=[['b', 'a']], codes=[[1, 0]])
print(index1)
MultiIndex([('a',),
('b',)],
)
print(index2)
MultiIndex([('a',),
('b',)],
)
print(index1.levels)
[['a', 'b']]
print(index2.levels)
[['b', 'a']]
```
Comment From: attack68
my bad. read too quickly, apols.