Skip to content

Commit

Permalink
unicode, bytes
Browse files Browse the repository at this point in the history
  • Loading branch information
TomAugspurger committed Nov 10, 2018
1 parent b312fe4 commit 445736d
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
15 changes: 15 additions & 0 deletions pandas/core/arrays/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions pandas/tests/extension/base/printing.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import io

import pandas as pd
from pandas import compat
import pytest

from .base import BaseExtensionTests
Expand All @@ -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)
Expand Down

0 comments on commit 445736d

Please sign in to comment.