PERF: Avoid materializing values in Categorical.set_categories
State: closed

Mater:

```python
In [1]: import pandas as pd; import numpy as np

In [2]: arr = ['s%04d' % i for i in np.random.randint(0, 500000 // 10,
                                                      size=500000)];
s = pd.Series(arr).astype('category')

In [3]: %timeit s.cat.set_categories(s.cat.categories)
68.5 ms ± 846 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)
```

HEAD:

```python
In [1]: import pandas as pd; import numpy as np

In [2]: arr = ['s%04d' % i for i in np.random.randint(0, 500000 // 10,
                                                      size=500000)]
s = pd.Series(arr).astype('category')

In [3]: %timeit s.cat.set_categories(s.cat.categories)
7.43 ms ± 110 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
```

Closes https://github.com/pandas-dev/pandas/issues/17508

I'll rebase #16015 on top of this. Running an ASV now.

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

Comments:
Here's the ASV:

```
[100.00%] ··· Running categoricals.Categoricals2.time_set_categories                                                                                80.8±1ms       before           after         ratio
     [f11bbf2f]       [6d72836e]
-        80.8±1ms       15.4±0.3ms     0.19  categoricals.Categoricals2.time_set_categories

SOME BENCHMARKS HAVE CHANGED SIGNIFICANTLY.
```
Hello @TomAugspurger! Thanks for updating the PR.









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

##### Comment last updated on September 14, 2017 at 10:55 Hours UTC
# [Codecov](https://codecov.io/gh/pandas-dev/pandas/pull/17515?src=pr&el=h1) Report
> Merging [#17515](https://codecov.io/gh/pandas-dev/pandas/pull/17515?src=pr&el=desc) into [master](https://codecov.io/gh/pandas-dev/pandas/commit/97abd2c9c11aeee0e3d2c58a74d85fa75062ca1f?src=pr&el=desc) will **decrease** coverage by `0.04%`.
> The diff coverage is `100%`.

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

```diff
@@            Coverage Diff             @@
##           master   #17515      +/-   ##
==========================================
- Coverage   91.24%    91.2%   -0.05%     
==========================================
  Files         163      163              
  Lines       49582    49586       +4     
==========================================
- Hits        45242    45225      -17     
- Misses       4340     4361      +21
```

| Flag | Coverage Δ | |
|---|---|---|
| #multiple | `88.99% <100%> (-0.03%)` | :arrow_down: |
| #single | `40.2% <18.18%> (-0.06%)` | :arrow_down: |

| [Impacted Files](https://codecov.io/gh/pandas-dev/pandas/pull/17515?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [pandas/core/dtypes/concat.py](https://codecov.io/gh/pandas-dev/pandas/pull/17515?src=pr&el=tree#diff-cGFuZGFzL2NvcmUvZHR5cGVzL2NvbmNhdC5weQ==) | `98.26% <100%> (-0.03%)` | :arrow_down: |
| [pandas/core/categorical.py](https://codecov.io/gh/pandas-dev/pandas/pull/17515?src=pr&el=tree#diff-cGFuZGFzL2NvcmUvY2F0ZWdvcmljYWwucHk=) | `95.57% <100%> (+0.04%)` | :arrow_up: |
| [pandas/io/gbq.py](https://codecov.io/gh/pandas-dev/pandas/pull/17515?src=pr&el=tree#diff-cGFuZGFzL2lvL2dicS5weQ==) | `25% <0%> (-58.34%)` | :arrow_down: |
| [pandas/plotting/\_converter.py](https://codecov.io/gh/pandas-dev/pandas/pull/17515?src=pr&el=tree#diff-cGFuZGFzL3Bsb3R0aW5nL19jb252ZXJ0ZXIucHk=) | `63.23% <0%> (-1.82%)` | :arrow_down: |
| [pandas/core/frame.py](https://codecov.io/gh/pandas-dev/pandas/pull/17515?src=pr&el=tree#diff-cGFuZGFzL2NvcmUvZnJhbWUucHk=) | `97.77% <0%> (-0.1%)` | :arrow_down: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/pandas-dev/pandas/pull/17515?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/17515?src=pr&el=footer). Last update [97abd2c...d238e3e](https://codecov.io/gh/pandas-dev/pandas/pull/17515?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).

I don't think so. `union_categoricals` takes the union of the two
categories. This replaces the old categories with the new.

On Wed, Sep 13, 2017 at 6:24 PM, Jeff Reback <notifications@github.com>
wrote:

> *@jreback* commented on this pull request.
> ------------------------------
>
> In pandas/core/categorical.py
> <https://github.com/pandas-dev/pandas/pull/17515#discussion_r138764715>:
>
> > +def _recode_for_categories(codes, old_categories, new_categories):
> +    """
> +    Convert a set of codes for to a new set of categories
> +
> +    Parameters
> +    ----------
> +    codes : array
> +    old_categories, new_categories : Index
> +
> +    Returns
> +    -------
> +    new_codes : array
> +
> +    Examples
> +    --------
> +    >>> old_cat = pd.Index(['b', 'a', 'c'])
>
> isn't this just a special case of union_categoricals?
>
> —
> You are receiving this because you were mentioned.
> Reply to this email directly, view it on GitHub
> <https://github.com/pandas-dev/pandas/pull/17515#pullrequestreview-62604925>,
> or mute the thread
> <https://github.com/notifications/unsubscribe-auth/ABQHIr3MDXGGPEIv_2U2nqZ4qxzcvvvRks5siGQ9gaJpZM4PWjZF>
> .
>

thanks @TomAugspurger