Repository: pandas-dev/pandas

Issue:
API: CategoricalDtype str, repr
I think that `str(cat.dtype)` should be changed back to always being 'category'. I've seen several places where people use `str(thing.dtype) == 'category'` as a way to check for Categoricals. Even subtle things like [this arrow PR](https://github.com/apache/arrow/pull/1161/files#diff-4b312e1fbf7b4c070f9788be9db33b20R198) would break.

So instead of

```
In [6]: str(pd.Categorical([1, 2, 3]).dtype)
Out[6]: 'CategoricalDtype(categories=[1, 2, 3], ordered=False)'
```

it would be

```
Out[6]: 'category'
```

We can leave `__repr__` to be unambiguous.



Discussion:

Resolving patch:
diff --git a/pandas/core/dtypes/dtypes.py b/pandas/core/dtypes/dtypes.py
--- a/pandas/core/dtypes/dtypes.py
+++ b/pandas/core/dtypes/dtypes.py
@@ -220,7 +220,7 @@ def __eq__(self, other):
             # both unordered; this could probably be optimized / cached
             return hash(self) == hash(other)
 
-    def __unicode__(self):
+    def __repr__(self):
         tpl = u'CategoricalDtype(categories={}ordered={})'
         if self.categories is None:
             data = u"None, "


Test patch:
diff --git a/pandas/tests/dtypes/test_dtypes.py b/pandas/tests/dtypes/test_dtypes.py
--- a/pandas/tests/dtypes/test_dtypes.py
+++ b/pandas/tests/dtypes/test_dtypes.py
@@ -1,4 +1,5 @@
 # -*- coding: utf-8 -*-
+import re
 import pytest
 
 from itertools import product
@@ -649,3 +650,10 @@ def test_from_categorical_dtype_both(self):
         result = CategoricalDtype._from_categorical_dtype(
             c1, categories=[1, 2], ordered=False)
         assert result == CategoricalDtype([1, 2], ordered=False)
+
+    def test_str_vs_repr(self):
+        c1 = CategoricalDtype(['a', 'b'])
+        assert str(c1) == 'category'
+        # Py2 will have unicode prefixes
+        pat = r"CategoricalDtype\(categories=\[.*\], ordered=False\)"
+        assert re.match(pat, repr(c1))
diff --git a/pandas/tests/series/test_analytics.py b/pandas/tests/series/test_analytics.py
--- a/pandas/tests/series/test_analytics.py
+++ b/pandas/tests/series/test_analytics.py
@@ -1784,7 +1784,8 @@ class TestNLargestNSmallest(object):
               # not supported on some archs
               # Series([3., 2, 1, 2, 5], dtype='complex256'),
               Series([3., 2, 1, 2, 5], dtype='complex128'),
-              Series(list('abcde'))])
+              Series(list('abcde')),
+              Series(list('abcde'), dtype='category')])
     def test_error(self, r):
         dt = r.dtype
         msg = ("Cannot use method 'n(larg|small)est' with "
@@ -1795,16 +1796,6 @@ def test_error(self, r):
             with tm.assert_raises_regex(TypeError, msg):
                 method(arg)
 
-    def test_error_categorical_dtype(self):
-        # same as test_error, but regex hard to escape properly
-        msg = ("Cannot use method 'n(larg|small)est' with dtype "
-               "CategoricalDtype.+")
-        with tm.assert_raises_regex(TypeError, msg):
-            Series(list('ab'), dtype='category').nlargest(2)
-
-        with tm.assert_raises_regex(TypeError, msg):
-            Series(list('ab'), dtype='category').nsmallest(2)
-
     @pytest.mark.parametrize(
         "s",
         [v for k, v in s_main_dtypes().iteritems()])