Skip to content

BUG/OFMT: fix repring of python3 bytes objects #4456

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 3, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions doc/source/release.rst
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,8 @@ pandas 0.13
called using the top level matplotlib API (:issue:`4408`)
- Fixed a bug where calling ``Series.astype(str)`` would truncate the string
(:issue:`4405`, :issue:`4437`)
- Fixed a py3 compat issue where bytes were being repr'd as tuples
(:issue:`4455`)

pandas 0.12
===========
Expand Down
4 changes: 4 additions & 0 deletions pandas/compat/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,10 @@ def u(s):
def u(s):
return unicode(s, "unicode_escape")


string_and_binary_types = string_types + (binary_type,)


try:
# callable reintroduced in later versions of Python
callable = callable
Expand Down
9 changes: 4 additions & 5 deletions pandas/core/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -1612,9 +1612,9 @@ def is_list_like(arg):
def _is_sequence(x):
try:
iter(x)
len(x) # it has a length
return not isinstance(x, compat.string_types) and True
except Exception:
len(x) # it has a length
return not isinstance(x, compat.string_and_binary_types)
except (TypeError, AttributeError):
return False

_ensure_float64 = algos.ensure_float64
Expand Down Expand Up @@ -2053,8 +2053,7 @@ def as_escaped_unicode(thing,escape_chars=escape_chars):

return compat.text_type(result)

if (compat.PY3 and hasattr(thing, '__next__')) or \
hasattr(thing, 'next'):
if (compat.PY3 and hasattr(thing, '__next__')) or hasattr(thing, 'next'):
return compat.text_type(thing)
elif (isinstance(thing, dict) and
_nest_lvl < get_option("display.pprint_nest_depth")):
Expand Down
19 changes: 17 additions & 2 deletions pandas/tests/test_common.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
from datetime import datetime
import sys
import re

import nose
from nose.tools import assert_equal
import unittest

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


def test_repr_binary_type():
import string
letters = string.ascii_letters
btype = compat.binary_type
try:
raw = btype(letters, encoding=cf.get_option('display.encoding'))
except TypeError:
raw = btype(letters)
b = compat.text_type(compat.bytes_to_str(raw))
res = com.pprint_thing(b, quote_strings=True)
assert_equal(res, repr(b))
res = com.pprint_thing(b, quote_strings=False)
assert_equal(res, b)


def test_rands():
r = com.rands(10)
assert(len(r) == 10)
Expand Down