From 02cb4da8c757f2744c332ea37f4c4cb1cb5720d8 Mon Sep 17 00:00:00 2001 From: Phillip Cloud Date: Fri, 2 Aug 2013 21:14:10 -0400 Subject: [PATCH 1/3] BUG/OFMT: fix spurious tuple repring of bytes types in python 3 --- doc/source/release.rst | 2 ++ pandas/core/common.py | 6 +++--- pandas/tests/test_common.py | 19 +++++++++++++++++-- 3 files changed, 22 insertions(+), 5 deletions(-) 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/core/common.py b/pandas/core/common.py index a4206fe26172c..9f39a9a573f83 100644 --- a/pandas/core/common.py +++ b/pandas/core/common.py @@ -1613,7 +1613,8 @@ def _is_sequence(x): try: iter(x) len(x) # it has a length - return not isinstance(x, compat.string_types) and True + return not isinstance(x, compat.string_types + + (compat.binary_type,)) and True except Exception: return False @@ -2053,8 +2054,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) From 76299b5bd7a26d23e4e5f4d9a43084d333bf09f1 Mon Sep 17 00:00:00 2001 From: Phillip Cloud Date: Sat, 3 Aug 2013 12:16:40 -0400 Subject: [PATCH 2/3] CLN: clean up _is_sequence --- pandas/core/common.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/pandas/core/common.py b/pandas/core/common.py index 9f39a9a573f83..c10b5367f247c 100644 --- a/pandas/core/common.py +++ b/pandas/core/common.py @@ -1612,10 +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 + - (compat.binary_type,)) and True - except Exception: + len(x) # it has a length + return not isinstance(x, compat.string_types + (compat.binary_type,)) + except (TypeError, AttributeError): return False _ensure_float64 = algos.ensure_float64 From 0862f02f31a0e5be0b8306647fa711c223b35ca9 Mon Sep 17 00:00:00 2001 From: Phillip Cloud Date: Sat, 3 Aug 2013 13:50:29 -0400 Subject: [PATCH 3/3] ENH/CLN: add string and binary types compat object --- pandas/compat/__init__.py | 4 ++++ pandas/core/common.py | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) 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 c10b5367f247c..06ca3be455f2a 100644 --- a/pandas/core/common.py +++ b/pandas/core/common.py @@ -1613,7 +1613,7 @@ def _is_sequence(x): try: iter(x) len(x) # it has a length - return not isinstance(x, compat.string_types + (compat.binary_type,)) + return not isinstance(x, compat.string_and_binary_types) except (TypeError, AttributeError): return False