diff --git a/doc/source/release.rst b/doc/source/release.rst index ddf0ecfc52d61..74b68938d62eb 100644 --- a/doc/source/release.rst +++ b/doc/source/release.rst @@ -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 =========== diff --git a/pandas/compat/__init__.py b/pandas/compat/__init__.py index eaf2928e4482c..6070c0e9c5379 100644 --- a/pandas/compat/__init__.py +++ b/pandas/compat/__init__.py @@ -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 diff --git a/pandas/core/common.py b/pandas/core/common.py index a4206fe26172c..06ca3be455f2a 100644 --- a/pandas/core/common.py +++ b/pandas/core/common.py @@ -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 @@ -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")): diff --git a/pandas/tests/test_common.py b/pandas/tests/test_common.py index ca119a8e263bf..abed2818cb864 100644 --- a/pandas/tests/test_common.py +++ b/pandas/tests/test_common.py @@ -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 @@ -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)