Skip to content

Commit 97aece1

Browse files
committed
CLN: CategoricalDtype repr now yields category
DISPLAY: show dtype when displaying Categorical series (for consistency)
1 parent 89c42a3 commit 97aece1

File tree

4 files changed

+40
-6
lines changed

4 files changed

+40
-6
lines changed

pandas/core/common.py

+32
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,38 @@ class CategoricalDtype(object):
128128
def __unicode__(self):
129129
return self.name
130130

131+
def __str__(self):
132+
"""
133+
Return a string representation for a particular Object
134+
135+
Invoked by str(df) in both py2/py3.
136+
Yields Bytestring in Py2, Unicode String in py3.
137+
"""
138+
139+
if compat.PY3:
140+
return self.__unicode__()
141+
return self.__bytes__()
142+
143+
def __bytes__(self):
144+
"""
145+
Return a string representation for a particular object.
146+
147+
Invoked by bytes(obj) in py3 only.
148+
Yields a bytestring in both py2/py3.
149+
"""
150+
from pandas.core.config import get_option
151+
152+
encoding = get_option("display.encoding")
153+
return self.__unicode__().encode(encoding, 'replace')
154+
155+
def __repr__(self):
156+
"""
157+
Return a string representation for a particular object.
158+
159+
Yields Bytestring in Py2, Unicode String in py3.
160+
"""
161+
return str(self)
162+
131163
def __hash__(self):
132164
# make myself hashable
133165
return hash(str(self))

pandas/core/format.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -167,8 +167,7 @@ def _get_footer(self):
167167
footer += 'Length: %d' % len(self.series)
168168

169169
# TODO: in tidy_repr, with freq index, no dtype is shown -> also include a guard here?
170-
# tidy_repr does not show a dtype for categorical, so do the same here
171-
if self.dtype and not com.is_categorical_dtype(self.series.dtype):
170+
if self.dtype:
172171
name = getattr(self.series.dtype, 'name', None)
173172
if name:
174173
if footer:

pandas/core/series.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -897,7 +897,10 @@ def _repr_footer(self):
897897
# Categorical
898898
if com.is_categorical_dtype(self.dtype):
899899
level_info = self.cat._repr_level_info()
900-
return u('%sLength: %d\n%s') % (namestr, len(self),level_info)
900+
return u('%sLength: %d, dtype: %s\n%s') % (namestr,
901+
len(self),
902+
str(self.dtype.name),
903+
level_info)
901904

902905
# reg series
903906
return u('%sLength: %d, dtype: %s') % (namestr,

pandas/tests/test_categorical.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -778,21 +778,21 @@ def test_describe(self):
778778
def test_repr(self):
779779
a = pd.Series(pd.Categorical([1,2,3,4], name="a"))
780780
exp = u("0 1\n1 2\n2 3\n3 4\n" +
781-
"Name: a\nLevels (4, int64): [1 < 2 < 3 < 4]")
781+
"Name: a, dtype: category\nLevels (4, int64): [1 < 2 < 3 < 4]")
782782

783783
self.assertEqual(exp, a.__unicode__())
784784

785785
a = pd.Series(pd.Categorical(["a","b"] *25, name="a"))
786786
exp = u("".join(["%s a\n%s b\n"%(i,i+1) for i in range(0,10,2)]) + "...\n" +
787787
"".join(["%s a\n%s b\n"%(i,i+1) for i in range(40,50,2)]) +
788-
"Name: a, Length: 50\n" +
788+
"Name: a, Length: 50, dtype: category\n" +
789789
"Levels (2, object): [a < b]")
790790
self.assertEqual(exp,a._tidy_repr())
791791

792792
levs = list("abcdefghijklmnopqrstuvwxyz")
793793
a = pd.Series(pd.Categorical(["a","b"], name="a", levels=levs))
794794
exp = u("0 a\n1 b\n" +
795-
"Name: a\n"
795+
"Name: a, dtype: category\n"
796796
"Levels (26, object): [a < b < c < d ... w < x < y < z]")
797797
self.assertEqual(exp,a.__unicode__())
798798

0 commit comments

Comments
 (0)