PERF: checking is_monotonic_increasing/decreasing before sorting on an index · Issue #11080 · pandas-dev/pandas (original) (raw)

@jreback

We don't keep the sortedness state in an index per-se, but it is rather cheap to check

In [8]: df = DataFrame(np.random.randn(1000000,2),columns=list('AB'))

In [9]: %timeit df.sort_index()
10 loops, best of 3: 37.1 ms per loop

In [10]: %timeit -n 1 -r 1 df.index.is_monotonic_increasing
1 loops, best of 1: 2.01 ms per loop

In [11]: %timeit -n 1 -r 1 df.index.is_monotonic_increasin^C
KeyboardInterrupt

In [11]: %timeit df.set_index('A').sort_index()
10 loops, best of 3: 175 ms per loop

In [12]: %timeit -n 1 -r 1 df.set_index('A').index.is_monotonic_increasing
1 loops, best of 1: 9.54 ms per loop