Skip to content

Commit 9d2fd87

Browse files
TomAugspurgerKrzysztof Chomski
authored and
Krzysztof Chomski
committed
API: Change str for CategoricalDtype to category (pandas-dev#17783)
1 parent 5a8aca7 commit 9d2fd87

File tree

4 files changed

+15
-17
lines changed

4 files changed

+15
-17
lines changed

doc/source/whatsnew/v0.21.0.txt

+4-5
Original file line numberDiff line numberDiff line change
@@ -157,11 +157,10 @@ The values have been correctly interpreted as integers.
157157

158158
The ``.dtype`` property of a ``Categorical``, ``CategoricalIndex`` or a
159159
``Series`` with categorical type will now return an instance of
160-
``CategoricalDtype``. For the most part, this is backwards compatible, though
161-
the string repr has changed. If you were previously using ``str(s.dtype) ==
162-
'category'`` to detect categorical data, switch to
163-
:func:`pandas.api.types.is_categorical_dtype`, which is compatible with the old
164-
and new ``CategoricalDtype``.
160+
``CategoricalDtype``. This change should be backwards compatible, though the
161+
repr has changed. ``str(CategoricalDtype())`` is still the string
162+
``'category'``, but the preferred way to detect categorical data is to use
163+
:func:`pandas.api.types.is_categorical_dtype`.
165164

166165
See the :ref:`CategoricalDtype docs <categorical.categoricaldtype>` for more.
167166

pandas/core/dtypes/dtypes.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ def __eq__(self, other):
220220
# both unordered; this could probably be optimized / cached
221221
return hash(self) == hash(other)
222222

223-
def __unicode__(self):
223+
def __repr__(self):
224224
tpl = u'CategoricalDtype(categories={}ordered={})'
225225
if self.categories is None:
226226
data = u"None, "

pandas/tests/dtypes/test_dtypes.py

+8
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# -*- coding: utf-8 -*-
2+
import re
23
import pytest
34

45
from itertools import product
@@ -649,3 +650,10 @@ def test_from_categorical_dtype_both(self):
649650
result = CategoricalDtype._from_categorical_dtype(
650651
c1, categories=[1, 2], ordered=False)
651652
assert result == CategoricalDtype([1, 2], ordered=False)
653+
654+
def test_str_vs_repr(self):
655+
c1 = CategoricalDtype(['a', 'b'])
656+
assert str(c1) == 'category'
657+
# Py2 will have unicode prefixes
658+
pat = r"CategoricalDtype\(categories=\[.*\], ordered=False\)"
659+
assert re.match(pat, repr(c1))

pandas/tests/series/test_analytics.py

+2-11
Original file line numberDiff line numberDiff line change
@@ -1784,7 +1784,8 @@ class TestNLargestNSmallest(object):
17841784
# not supported on some archs
17851785
# Series([3., 2, 1, 2, 5], dtype='complex256'),
17861786
Series([3., 2, 1, 2, 5], dtype='complex128'),
1787-
Series(list('abcde'))])
1787+
Series(list('abcde')),
1788+
Series(list('abcde'), dtype='category')])
17881789
def test_error(self, r):
17891790
dt = r.dtype
17901791
msg = ("Cannot use method 'n(larg|small)est' with "
@@ -1795,16 +1796,6 @@ def test_error(self, r):
17951796
with tm.assert_raises_regex(TypeError, msg):
17961797
method(arg)
17971798

1798-
def test_error_categorical_dtype(self):
1799-
# same as test_error, but regex hard to escape properly
1800-
msg = ("Cannot use method 'n(larg|small)est' with dtype "
1801-
"CategoricalDtype.+")
1802-
with tm.assert_raises_regex(TypeError, msg):
1803-
Series(list('ab'), dtype='category').nlargest(2)
1804-
1805-
with tm.assert_raises_regex(TypeError, msg):
1806-
Series(list('ab'), dtype='category').nsmallest(2)
1807-
18081799
@pytest.mark.parametrize(
18091800
"s",
18101801
[v for k, v in s_main_dtypes().iteritems()])

0 commit comments

Comments
 (0)