Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
API: Change str for CategoricalDtype to category
Better compatibility with older versions
  • Loading branch information
TomAugspurger committed Oct 4, 2017
commit 5d477cd5758f11aa85c376ca364b0b4f9feb7666
9 changes: 4 additions & 5 deletions doc/source/whatsnew/v0.21.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -157,11 +157,10 @@ The values have been correctly interpreted as integers.

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

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

Expand Down
2 changes: 1 addition & 1 deletion pandas/core/dtypes/dtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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, "
Expand Down
6 changes: 6 additions & 0 deletions pandas/tests/dtypes/test_dtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -649,3 +649,9 @@ 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'
expected = "CategoricalDtype(categories=['a', 'b'], ordered=False)"
assert repr(c1) == expected