Ale Ale - 1 year ago 135
Python Question

Pandas selecting with unaligned indexes

I have 2 series.
The first one contains a list of numbers with an index counting 0..8.

A = pd.Series([2,3,4,6,5,4,7,6,5], name=['A'], index=[0,1,2,3,4,5,6,7,8])


The second one only contains True values, but the index of the series is a subset of the first one.

B = pd.Series([1, 1, 1, 1, 1], name=['B'], index=[0,2,4,7,8], dtype=bool)


I'd like to use B as boolean vector to get the A-values for the corresponding indexes, like:

A[B]

[...]
IndexingError: Unalignable boolean Series key provided


Unfortunately this raises an error.

Do I need to align them first?

Answer Source

Does

A[B.index.values]

work for your version of pandas? (I see we have different versions because now the Series name has to be hashable, so your code gave me an error)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download