Suppose we have the following two Series:
s1 = pd.Series([1, 1, 1], index=['B', 'A', 'C'])
s2 = pd.Series([2, 3, 1], index=['B', 'C', 'A'])
Note that the index of s1
and s2
are the same three labels but in different orders.
Multiplying the two Series s1 * s2
gives:
A 1
B 2
C 3
where the resulting index is sorted alphabetically.
This comes as a surprise to me because I would expect to get:
B 2
A 1
C 3
preserving the index order of the multiplicand s1
(or maybe that of the multiplier s2
).
I get similar results using DataFrames as well as other arithmetic operations (+
, -
, and /
) and their corresponding methods, like .mul()
.
I figured that reindexing s2
is an easy solution, for example:
s1 * s2.reindex_like(s1)
Nonetheless, sorting misaligned indices in alphabetic order seems arbitrary to me. I'm not able to find a good description of this pandas behaviour online, can somebody explain?