Skip to content

Commit 52712e2

Browse files
committed
Merge pull request #4456 from cpcloud/fix-py3-bytes-repr
BUG/OFMT: fix repring of python3 bytes objects
2 parents 218ba11 + 0862f02 commit 52712e2

File tree

4 files changed

+27
-7
lines changed

4 files changed

+27
-7
lines changed

doc/source/release.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,8 @@ pandas 0.13
119119
called using the top level matplotlib API (:issue:`4408`)
120120
- Fixed a bug where calling ``Series.astype(str)`` would truncate the string
121121
(:issue:`4405`, :issue:`4437`)
122+
- Fixed a py3 compat issue where bytes were being repr'd as tuples
123+
(:issue:`4455`)
122124

123125
pandas 0.12
124126
===========

pandas/compat/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,10 @@ def u(s):
185185
def u(s):
186186
return unicode(s, "unicode_escape")
187187

188+
189+
string_and_binary_types = string_types + (binary_type,)
190+
191+
188192
try:
189193
# callable reintroduced in later versions of Python
190194
callable = callable

pandas/core/common.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1612,9 +1612,9 @@ def is_list_like(arg):
16121612
def _is_sequence(x):
16131613
try:
16141614
iter(x)
1615-
len(x) # it has a length
1616-
return not isinstance(x, compat.string_types) and True
1617-
except Exception:
1615+
len(x) # it has a length
1616+
return not isinstance(x, compat.string_and_binary_types)
1617+
except (TypeError, AttributeError):
16181618
return False
16191619

16201620
_ensure_float64 = algos.ensure_float64
@@ -2053,8 +2053,7 @@ def as_escaped_unicode(thing,escape_chars=escape_chars):
20532053

20542054
return compat.text_type(result)
20552055

2056-
if (compat.PY3 and hasattr(thing, '__next__')) or \
2057-
hasattr(thing, 'next'):
2056+
if (compat.PY3 and hasattr(thing, '__next__')) or hasattr(thing, 'next'):
20582057
return compat.text_type(thing)
20592058
elif (isinstance(thing, dict) and
20602059
_nest_lvl < get_option("display.pprint_nest_depth")):

pandas/tests/test_common.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
from datetime import datetime
2-
import sys
32
import re
43

54
import nose
5+
from nose.tools import assert_equal
66
import unittest
77

88
from pandas import Series, DataFrame, date_range, DatetimeIndex, Timestamp
9-
from pandas.compat import range, long, lrange, lmap, u, map
9+
from pandas.compat import range, long, lrange, lmap, u
1010
from pandas.core.common import notnull, isnull
1111
import pandas.core.common as com
1212
import pandas.util.testing as tm
@@ -147,6 +147,21 @@ def test_all_not_none():
147147
assert(not com._all_not_none(None, None, None, None))
148148

149149

150+
def test_repr_binary_type():
151+
import string
152+
letters = string.ascii_letters
153+
btype = compat.binary_type
154+
try:
155+
raw = btype(letters, encoding=cf.get_option('display.encoding'))
156+
except TypeError:
157+
raw = btype(letters)
158+
b = compat.text_type(compat.bytes_to_str(raw))
159+
res = com.pprint_thing(b, quote_strings=True)
160+
assert_equal(res, repr(b))
161+
res = com.pprint_thing(b, quote_strings=False)
162+
assert_equal(res, b)
163+
164+
150165
def test_rands():
151166
r = com.rands(10)
152167
assert(len(r) == 10)

0 commit comments

Comments
 (0)