Repository: pandas-dev/pandas

Issue:
UnicodeEncodeError in index.summary()
>  idx=pd.Index([u'\u05d3\u05e4\u05e0\u05d4',u'\u05d3\u05e4\u05e0\u05d4'])
> idx.summary()

---

UnicodeEncodeError                        Traceback (most recent call last)
<ipython-input-135-26bf122d0c51> in <module>()
      1 idx=pd.Index([u'\u05d3\u05e4\u05e0\u05d4',u'\u05d3\u05e4\u05e0\u05d4'])
----> 2 idx.summary()
      3 

/usr/local/lib/python2.7/dist-packages/pandas-0.9.0.dev_37f4ded-py2.7-linux-x86_64.egg/pandas/core/index.pyc in summary(self, name)
    197     def summary(self, name=None):
    198         if len(self) > 0:
--> 199             index_summary = ', %s to %s' % (str(self[0]), str(self[-1]))
    200         else:
    201             index_summary = ''

UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-3: ordinal not in range(128)

UnicodeEncodeError in index.summary()
>  idx=pd.Index([u'\u05d3\u05e4\u05e0\u05d4',u'\u05d3\u05e4\u05e0\u05d4'])
> idx.summary()

---

UnicodeEncodeError                        Traceback (most recent call last)
<ipython-input-135-26bf122d0c51> in <module>()
      1 idx=pd.Index([u'\u05d3\u05e4\u05e0\u05d4',u'\u05d3\u05e4\u05e0\u05d4'])
----> 2 idx.summary()
      3 

/usr/local/lib/python2.7/dist-packages/pandas-0.9.0.dev_37f4ded-py2.7-linux-x86_64.egg/pandas/core/index.pyc in summary(self, name)
    197     def summary(self, name=None):
    198         if len(self) > 0:
--> 199             index_summary = ', %s to %s' % (str(self[0]), str(self[-1]))
    200         else:
    201             index_summary = ''

UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-3: ordinal not in range(128)



Discussion:

Resolving patch:
diff --git a/pandas/core/index.py b/pandas/core/index.py
--- a/pandas/core/index.py
+++ b/pandas/core/index.py
@@ -196,7 +196,7 @@ def _has_complex_internals(self):
 
     def summary(self, name=None):
         if len(self) > 0:
-            index_summary = ', %s to %s' % (str(self[0]), str(self[-1]))
+            index_summary = ', %s to %s' % (unicode(self[0]), unicode(self[-1]))
         else:
             index_summary = ''
 


Test patch:
diff --git a/pandas/tests/test_format.py b/pandas/tests/test_format.py
--- a/pandas/tests/test_format.py
+++ b/pandas/tests/test_format.py
@@ -246,7 +246,7 @@ def test_repr_corner(self):
 
     def test_frame_info_encoding(self):
         index = ['\'Til There Was You (1997)',
-                 '\xc1 k\xf6ldum klaka (Cold Fever) (1994)']
+                 'ldum klaka (Cold Fever) (1994)']
         fmt.set_printoptions(max_rows=1)
         df = DataFrame(columns=['a', 'b', 'c'], index=index)
         repr(df)
diff --git a/pandas/tests/test_index.py b/pandas/tests/test_index.py
--- a/pandas/tests/test_index.py
+++ b/pandas/tests/test_index.py
@@ -27,6 +27,7 @@
 class TestIndex(unittest.TestCase):
 
     def setUp(self):
+        self.unicodeIndex = tm.makeUnicodeIndex(100)
         self.strIndex = tm.makeStringIndex(100)
         self.dateIndex = tm.makeDateIndex(100)
         self.intIndex = tm.makeIntIndex(100)
@@ -374,6 +375,7 @@ def test_take(self):
     def _check_method_works(self, method):
         method(self.empty)
         method(self.dateIndex)
+        method(self.unicodeIndex)
         method(self.strIndex)
         method(self.intIndex)
         method(self.tuples)
diff --git a/pandas/util/testing.py b/pandas/util/testing.py
--- a/pandas/util/testing.py
+++ b/pandas/util/testing.py
@@ -36,6 +36,10 @@ def rands(n):
     choices = string.ascii_letters + string.digits
     return ''.join([random.choice(choices) for _ in xrange(n)])
 
+def randu(n):
+    choices = u"".join(map(unichr,range(1488,1488+26))) + string.digits
+    return ''.join([random.choice(choices) for _ in xrange(n)])
+
 #-------------------------------------------------------------------------------
 # Console debugging tools
 
@@ -183,6 +187,9 @@ def getCols(k):
 def makeStringIndex(k):
     return Index([rands(10) for _ in xrange(k)])
 
+def makeUnicodeIndex(k):
+    return Index([randu(10) for _ in xrange(k)])
+
 def makeIntIndex(k):
     return Index(range(k))