PERF: improved performance of CategoricalIndex.is_monotonic*
State: closed

- [x] passes `git diff upstream/master -u -- "*.py" | flake8 --diff`
- [x] whatsnew entry

```python
>>> n = 1000000
>>> ci = pd.CategoricalIndex(list('a' * n + 'b' * n + 'c' * n))
>>> %t ci.is_monotonic_increasing
22 ms # v0.22 and master
227 ns  # this commit
```

There seem to be a few more like this, where ``CategoricalIndex`` should use ``self._engine`` but doesn't. 

@TomAugspurger?

Labels:
name: Performance
description: Memory or execution speed performance
name: Categorical
description: Categorical Data Type

Comments:
this hit the same code path; so check this
Not sure I follow, but these two versions do not follow the same code path, as the old version required creating a new ``Int64Index`` which is expensive.

``CategoricalIndex.is_monotonic`` is already tested in ``indexes/test_category.py::TestCategoricalIndex::test_is_monotonic``.
# [Codecov](https://codecov.io/gh/pandas-dev/pandas/pull/21025?src=pr&el=h1) Report
> Merging [#21025](https://codecov.io/gh/pandas-dev/pandas/pull/21025?src=pr&el=desc) into [master](https://codecov.io/gh/pandas-dev/pandas/commit/501f0418c00064f5041ef156ed5a2289a9064da3?src=pr&el=desc) will **not change** coverage.
> The diff coverage is `100%`.

[![Impacted file tree graph](https://codecov.io/gh/pandas-dev/pandas/pull/21025/graphs/tree.svg?width=650&src=pr&token=eZ4WkYLtcO&height=150)](https://codecov.io/gh/pandas-dev/pandas/pull/21025?src=pr&el=tree)

```diff
@@           Coverage Diff           @@
##           master   #21025   +/-   ##
=======================================
  Coverage   91.83%   91.83%           
=======================================
  Files         153      153           
  Lines       49495    49495           
=======================================
  Hits        45454    45454           
  Misses       4041     4041
```

| Flag | Coverage Δ | |
|---|---|---|
| #multiple | `90.23% <100%> (ø)` | :arrow_up: |
| #single | `41.88% <0%> (ø)` | :arrow_up: |

| [Impacted Files](https://codecov.io/gh/pandas-dev/pandas/pull/21025?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [pandas/core/indexes/category.py](https://codecov.io/gh/pandas-dev/pandas/pull/21025/diff?src=pr&el=tree#diff-cGFuZGFzL2NvcmUvaW5kZXhlcy9jYXRlZ29yeS5weQ==) | `97.03% <100%> (ø)` | :arrow_up: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/pandas-dev/pandas/pull/21025?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/pandas-dev/pandas/pull/21025?src=pr&el=footer). Last update [501f041...c815d62](https://codecov.io/gh/pandas-dev/pandas/pull/21025?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).

can you add additional tests using strings (and not just integers) in that same test. otherwise lgtm.
do we have sufficient asv's for this?
There were no asv's for this. However, if you run my code snippet above, there is a huge spike in RAM usage, when run in the old version. I've even gotten a few ``MemoryErrors``.

So my ASV is done using only ``N = 1000`` to limit memory usage. The result is here 60 microseconds (old version) vs 260 ns (new version).

Also, Series.is_monotonic* wasn't added untill 0.19. should that be put in a try/except clause, to avoid failing on older versions of pandas?
Hello @topper-123! Thanks for updating the PR.







Cheers ! There are no PEP8 issues in this Pull Request. :beers: 

##### Comment last updated on May 16, 2018 at 19:04 Hours UTC
thanks @topper-123