Pandas DatetimeIndex slicing: Fix KeyError with sort_index

Pandas DatetimeIndex slicing: Fix KeyError with sort_index

Slice an unsorted datetime index in Python Pandas, and you’ll likely get:

KeyError: ‘Value based partial slicing on non-monotonic DatetimeIndexes with non-existing keys is not allowed.’

Why? If the dates are unsorted, where should Pandas start/stop?

Solution: sort_index!