Pandas .loc slicing on a non-unique index: sort_index() first

Pandas .loc slicing on a non-unique index: sort_index() first

Trying to use .loc to retrieve a slice in Python Pandas? If the index repeats, you need to sort it:

s = Series([10, 20, 30], index=list('aba'))
s.loc['a':'b']  # KeyError: "Cannot get left slice bound for non-unique label: 'a'"
s.sort_index().loc['a':'b']  # works!