Skip to content

PERF: checking is_monotonic_increasing/decreasing before sorting on an index #11080

Closed
@jreback

Description

@jreback

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

  • is_monotonic_increasing or is_monotonic_decreasing on a reg-index
  • MultiIndex should check is_lexsorted (this might be done already)
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

Metadata

Metadata

Assignees

No one assigned

    Labels

    IndexingRelated to indexing on series/frames, not to indexes themselvesPerformanceMemory or execution speed performance

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions