REF: clearer Categorical/CategoricalIndex construction
State: closed

Construction ATM of ``Categorical`` and ``CategoricalIndex`` each uses their own ways to construct the dtype. This makes the construction look more complex than it need to.

By collecting the dtype construction in a shared function, a lot of things become more simple. An additional advantage is that we can now have a higher confidence in dtype being the same for the same inputs, easing reasoning about the construction phase.

The above is all internal changes, so no whatsnew note is supplied.

A very minor issue was discovered, where an error message for CategoricalDtype made it look like the problem was in ``CategoricalIndex``, which is confusing, especially if we're not constructing a ``CategoricalIndex``.

```python
>>> pd.api.type.CategoricalDtype('category')
TypeError: CategoricalIndex(...) must be called with a collection of some kind, 'category' was passed  # master
TypeError: Parameter 'categories' must be list-like, was 'category'  # this PR
```

Otherwise the API is unchanged, but the code paths are now much simpler IMO.

Labels:
name: Refactor
description: Internal refactoring of code
name: Categorical
description: Categorical Data Type

Comments:
Hello @topper-123! Thanks for updating the PR.













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

##### Comment last updated on January 03, 2019 at 22:12 Hours UTC
# [Codecov](https://codecov.io/gh/pandas-dev/pandas/pull/24419?src=pr&el=h1) Report
> Merging [#24419](https://codecov.io/gh/pandas-dev/pandas/pull/24419?src=pr&el=desc) into [master](https://codecov.io/gh/pandas-dev/pandas/commit/159772d0c9d49977d4b7f871c62c26e06890143a?src=pr&el=desc) will **increase** coverage by `<.01%`.
> The diff coverage is `100%`.

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

```diff
@@            Coverage Diff             @@
##           master   #24419      +/-   ##
==========================================
+ Coverage    92.3%    92.3%   +<.01%     
==========================================
  Files         163      163              
  Lines       51950    51948       -2     
==========================================
- Hits        47953    47952       -1     
+ Misses       3997     3996       -1
```

| Flag | Coverage Δ | |
|---|---|---|
| #multiple | `90.71% <100%> (-0.01%)` | :arrow_down: |
| #single | `43% <81.25%> (ø)` | :arrow_up: |

| [Impacted Files](https://codecov.io/gh/pandas-dev/pandas/pull/24419?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [pandas/core/dtypes/dtypes.py](https://codecov.io/gh/pandas-dev/pandas/pull/24419/diff?src=pr&el=tree#diff-cGFuZGFzL2NvcmUvZHR5cGVzL2R0eXBlcy5weQ==) | `95.38% <100%> (+0.04%)` | :arrow_up: |
| [pandas/core/indexes/category.py](https://codecov.io/gh/pandas-dev/pandas/pull/24419/diff?src=pr&el=tree#diff-cGFuZGFzL2NvcmUvaW5kZXhlcy9jYXRlZ29yeS5weQ==) | `98.61% <100%> (-0.04%)` | :arrow_down: |
| [pandas/core/arrays/categorical.py](https://codecov.io/gh/pandas-dev/pandas/pull/24419/diff?src=pr&el=tree#diff-cGFuZGFzL2NvcmUvYXJyYXlzL2NhdGVnb3JpY2FsLnB5) | `95.33% <100%> (+0.02%)` | :arrow_up: |
| [pandas/util/testing.py](https://codecov.io/gh/pandas-dev/pandas/pull/24419/diff?src=pr&el=tree#diff-cGFuZGFzL3V0aWwvdGVzdGluZy5weQ==) | `87.84% <0%> (+0.09%)` | :arrow_up: |

------

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

# [Codecov](https://codecov.io/gh/pandas-dev/pandas/pull/24419?src=pr&el=h1) Report
> Merging [#24419](https://codecov.io/gh/pandas-dev/pandas/pull/24419?src=pr&el=desc) into [master](https://codecov.io/gh/pandas-dev/pandas/commit/c9a0405892f33528e32ffadefcf12a8ee42ba382?src=pr&el=desc) will **increase** coverage by `<.01%`.
> The diff coverage is `100%`.

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

```diff
@@            Coverage Diff             @@
##           master   #24419      +/-   ##
==========================================
+ Coverage   92.38%   92.38%   +<.01%     
==========================================
  Files         166      166              
  Lines       52485    52485              
==========================================
+ Hits        48489    48490       +1     
+ Misses       3996     3995       -1
```

| Flag | Coverage Δ | |
|---|---|---|
| #multiple | `90.81% <100%> (ø)` | :arrow_up: |
| #single | `43.05% <81.81%> (ø)` | :arrow_up: |

| [Impacted Files](https://codecov.io/gh/pandas-dev/pandas/pull/24419?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [pandas/core/dtypes/dtypes.py](https://codecov.io/gh/pandas-dev/pandas/pull/24419/diff?src=pr&el=tree#diff-cGFuZGFzL2NvcmUvZHR5cGVzL2R0eXBlcy5weQ==) | `95.55% <100%> (+0.21%)` | :arrow_up: |
| [pandas/core/indexes/category.py](https://codecov.io/gh/pandas-dev/pandas/pull/24419/diff?src=pr&el=tree#diff-cGFuZGFzL2NvcmUvaW5kZXhlcy9jYXRlZ29yeS5weQ==) | `98.61% <100%> (-0.04%)` | :arrow_down: |
| [pandas/core/arrays/categorical.py](https://codecov.io/gh/pandas-dev/pandas/pull/24419/diff?src=pr&el=tree#diff-cGFuZGFzL2NvcmUvYXJyYXlzL2NhdGVnb3JpY2FsLnB5) | `95.41% <100%> (-0.06%)` | :arrow_down: |
| [pandas/util/testing.py](https://codecov.io/gh/pandas-dev/pandas/pull/24419/diff?src=pr&el=tree#diff-cGFuZGFzL3V0aWwvdGVzdGluZy5weQ==) | `88.09% <0%> (+0.09%)` | :arrow_up: |

------

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

I've adjusted for doc string comments. Will look into the tests part tonight.
Tests have been added.
Wrt. performance. Have some problems getting asv working. Will look into that tomorrow.


Most time checks are ok, but I get this:

```
       before           after         ratio
     [b49136eb]       [935c8c16]
     <master>         <cateorical_refactor>
+        6.25±0μs       10.9±0.8μs     1.75  categoricals.CategoricalSlicing.time_getitem_slice('monotonic_decr')
+        6.25±0μs       10.9±0.6μs     1.75  categoricals.CategoricalSlicing.time_getitem_slice('monotonic_incr')
```

This is for timing slicing a Categoricals with length of 3 million, so I've doubtful if this even matters?

thanks @topper-123 can you merge master
@TomAugspurger @jbrockmendel any comments
LGTM at a glance.
Comments from @jschendel have been addressed.
thanks!