API: Change str for CategoricalDtype to category
State: closed

Better compatibility with older versions

Closes #17782 

Labels:
name: Categorical
description: Categorical Data Type

Comments:
# [Codecov](https://codecov.io/gh/pandas-dev/pandas/pull/17783?src=pr&el=h1) Report
> Merging [#17783](https://codecov.io/gh/pandas-dev/pandas/pull/17783?src=pr&el=desc) into [master](https://codecov.io/gh/pandas-dev/pandas/commit/48d0460ab9acbee223bae1be699344f8fd232224?src=pr&el=desc) will **decrease** coverage by `0.01%`.
> The diff coverage is `n/a`.

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

```diff
@@            Coverage Diff             @@
##           master   #17783      +/-   ##
==========================================
- Coverage   91.24%   91.22%   -0.02%     
==========================================
  Files         163      163              
  Lines       49916    49910       -6     
==========================================
- Hits        45544    45530      -14     
- Misses       4372     4380       +8
```

| Flag | Coverage Δ | |
|---|---|---|
| #multiple | `89.02% <ø> (ø)` | :arrow_up: |
| #single | `40.23% <ø> (-0.08%)` | :arrow_down: |

| [Impacted Files](https://codecov.io/gh/pandas-dev/pandas/pull/17783?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [pandas/core/dtypes/dtypes.py](https://codecov.io/gh/pandas-dev/pandas/pull/17783?src=pr&el=tree#diff-cGFuZGFzL2NvcmUvZHR5cGVzL2R0eXBlcy5weQ==) | `95.14% <ø> (+0.19%)` | :arrow_up: |
| [pandas/io/gbq.py](https://codecov.io/gh/pandas-dev/pandas/pull/17783?src=pr&el=tree#diff-cGFuZGFzL2lvL2dicS5weQ==) | `25% <0%> (-58.34%)` | :arrow_down: |
| [pandas/core/frame.py](https://codecov.io/gh/pandas-dev/pandas/pull/17783?src=pr&el=tree#diff-cGFuZGFzL2NvcmUvZnJhbWUucHk=) | `97.73% <0%> (-0.1%)` | :arrow_down: |

------

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

I am not sure this is a great idea, what is the purpose here?
There's, lot's of code out there that expects `str(dtype)` to be `'category '`, and an established tradition in Python of `str(obj)` being simple, while `repr(obj)` is more complicated.

Why are you against it?
this loses informtion, no reason why for that. where does code == 'str(dtype)` live? IOW can you show me an example from a project?
The information is still there... It's only when you call str(dtype). I
linked to an example in the issue

- https://github.com/dask/fastparquet/pull/200/files
-
https://github.com/dask/dask/pull/2737/commits/f8e382cc3bcf07acfaa03386d6c550d0a2d8507f
-
https://github.com/apache/arrow/pull/1161/files#diff-4b312e1fbf7b4c070f9788be9db33b20R198

On Thu, Oct 5, 2017 at 7:17 AM, Jeff Reback <notifications@github.com>
wrote:

> this loses informtion, no reason why for that. where does code ==
> 'str(dtype)` live? IOW can you show me an example from a project?
>
> —
> You are receiving this because you authored the thread.
> Reply to this email directly, view it on GitHub
> <https://github.com/pandas-dev/pandas/pull/17783#issuecomment-334447223>,
> or mute the thread
> <https://github.com/notifications/unsubscribe-auth/ABQHIuzmNIBNpLZDD9nVR_lYyGuDyUYQks5spMjigaJpZM4PtwTu>
> .
>

the ``if str(dtype) == 'category'`` was never a way to do this. to be honest I am in favor of breaking this.
Ok, I'm +1 for this change, and so is Joris.
It maybe never really was meant to do that, but before is_categorical was consistently publicly exposed, I think it was a sensible thing to do. 
Personally I don't find undoing the loss of information when converting dtype to string worth it to potentially break people's code.
ok then, merge away.
Another reason that I think this was a good change, is because it is used in the 'dtypes' overview. Before this PR you got:

```
In [19]: df.dtypes
Out[19]:
cats    CategoricalDtype(categories=['a', 'b', 'c'], o...
vals                                                int64
dtype: object
```

which I think is not ideal (although that might have been solved in another way as well). 

But that did me think, we could think of some kind of 'shorter but still informative' repr like datetimetz has (although once you have more categories, it will always be truncated, which is not that nice in a default repr I think).