From 445736d939dc1ce8615767ccb79f84c8bfdad947 Mon Sep 17 00:00:00 2001 From: Tom Augspurger Date: Sat, 10 Nov 2018 06:30:57 -0600 Subject: [PATCH] unicode, bytes --- pandas/core/arrays/base.py | 15 +++++++++++++++ pandas/tests/extension/base/printing.py | 12 ++++++++++++ 2 files changed, 27 insertions(+) diff --git a/pandas/core/arrays/base.py b/pandas/core/arrays/base.py index 7bdb54bec02fb..cfcf2e98c2bc6 100644 --- a/pandas/core/arrays/base.py +++ b/pandas/core/arrays/base.py @@ -9,6 +9,8 @@ import operator +from pandas import compat +from pandas.core.config import get_option from pandas.core.dtypes.generic import ABCSeries, ABCIndexClass from pandas.errors import AbstractMethodError from pandas.compat.numpy import function as nv @@ -662,6 +664,19 @@ def copy(self, deep=False): # ------------------------------------------------------------------------ # Printing # ------------------------------------------------------------------------ + def __unicode__(self): + result = str(self) + if compat.PY2: + encoding = get_option("display.encoding") + result = result.decode(encoding) + return result + + def __bytes__(self): + result = str(self) + if compat.PY3: + encoding = get_option("display.encoding") + result = result.encode(encoding) + return result def __repr__(self): from pandas.io.formats.printing import format_object_summary diff --git a/pandas/tests/extension/base/printing.py b/pandas/tests/extension/base/printing.py index 3d224c2b06c0b..512ef23f4c247 100644 --- a/pandas/tests/extension/base/printing.py +++ b/pandas/tests/extension/base/printing.py @@ -1,6 +1,7 @@ import io import pandas as pd +from pandas import compat import pytest from .base import BaseExtensionTests @@ -23,6 +24,17 @@ def test_array_repr(self, data, size): if size == 'big': assert '...' in result + def test_array_repr_bytes(self, data): + result = bytes(data) + if compat.PY2: + assert isinstance(result, str) + else: + assert isinstance(result, bytes) + + def test_array_repr_unicode(self, data): + result = compat.u(data) + assert isinstance(result, compat.text_type) + def test_series_repr(self, data): ser = pd.Series(data) assert data.dtype.name in repr(ser)