From cde7d28d1e7c43e09ad7127e5efcd9b4265613b8 Mon Sep 17 00:00:00 2001 From: Jeffrey Tratner Date: Sat, 21 Sep 2013 21:24:28 -0400 Subject: [PATCH] BUG: Fix FrozenNDArray & FrozenList string methods Plus basic tests (i.e., "Hey! If you pass me unicode I don't fail - yay!") --- doc/source/release.rst | 1 + pandas/core/base.py | 16 ++++++++++++++-- pandas/core/index.py | 9 --------- pandas/tests/test_base.py | 26 ++++++++++++++++++++++++-- 4 files changed, 39 insertions(+), 13 deletions(-) diff --git a/doc/source/release.rst b/doc/source/release.rst index 555c79baf8584..75097ee50e8c1 100644 --- a/doc/source/release.rst +++ b/doc/source/release.rst @@ -446,6 +446,7 @@ Bug Fixes the ``by`` argument was passed (:issue:`4112`, :issue:`4113`). - Fixed a bug in ``convert_objects`` for > 2 ndims (:issue:`4937`) - Fixed a bug in DataFrame/Panel cache insertion and subsequent indexing (:issue:`4939`) + - Fixed string methods for ``FrozenNDArray`` and ``FrozenList`` (:issue:`4929`) pandas 0.12.0 ------------- diff --git a/pandas/core/base.py b/pandas/core/base.py index fb0d56113ede9..14070d8825393 100644 --- a/pandas/core/base.py +++ b/pandas/core/base.py @@ -122,9 +122,12 @@ def _disabled(self, *args, **kwargs): def __unicode__(self): from pandas.core.common import pprint_thing + return pprint_thing(self, quote_strings=True, + escape_chars=('\t', '\r', '\n')) + + def __repr__(self): return "%s(%s)" % (self.__class__.__name__, - pprint_thing(self, quote_strings=True, - escape_chars=('\t', '\r', '\n'))) + str(self)) __setitem__ = __setslice__ = __delitem__ = __delslice__ = _disabled pop = append = extend = remove = sort = insert = _disabled @@ -154,3 +157,12 @@ def values(self): """returns *copy* of underlying array""" arr = self.view(np.ndarray).copy() return arr + + def __unicode__(self): + """ + Return a string representation for this object. + + Invoked by unicode(df) in py2 only. Yields a Unicode String in both py2/py3. + """ + prepr = com.pprint_thing(self, escape_chars=('\t', '\r', '\n'),quote_strings=True) + return '%s(%s, dtype=%s)' % (type(self).__name__, prepr, self.dtype) diff --git a/pandas/core/index.py b/pandas/core/index.py index 1d3f181b731e8..f2a22580f16b4 100644 --- a/pandas/core/index.py +++ b/pandas/core/index.py @@ -228,15 +228,6 @@ def copy(self, names=None, name=None, dtype=None, deep=False): new_index = new_index.astype(dtype) return new_index - def __unicode__(self): - """ - Return a string representation for a particular Index - - Invoked by unicode(df) in py2 only. Yields a Unicode String in both py2/py3. - """ - prepr = com.pprint_thing(self, escape_chars=('\t', '\r', '\n'),quote_strings=True) - return '%s(%s, dtype=%s)' % (type(self).__name__, prepr, self.dtype) - def to_series(self): """ return a series with both index and values equal to the index keys diff --git a/pandas/tests/test_base.py b/pandas/tests/test_base.py index c6285bc95b855..5d5a269b90428 100644 --- a/pandas/tests/test_base.py +++ b/pandas/tests/test_base.py @@ -1,10 +1,30 @@ import re import unittest import numpy as np +import pandas.compat as compat +from pandas.compat import u from pandas.core.base import FrozenList, FrozenNDArray from pandas.util.testing import assertRaisesRegexp, assert_isinstance +class CheckStringMixin(object): + def test_string_methods_dont_fail(self): + repr(self.container) + str(self.container) + bytes(self.container) + if not compat.PY3: + unicode(self.container) + + def test_tricky_container(self): + if not hasattr(self, 'unicode_container'): + raise nose.SkipTest('Need unicode_container to test with this') + repr(self.unicode_container) + str(self.unicode_container) + bytes(self.unicode_container) + if not compat.PY3: + unicode(self.unicode_container) + + class CheckImmutable(object): mutable_regex = re.compile('does not support mutable operations') @@ -43,8 +63,9 @@ def check_result(self, result, expected, klass=None): self.assertEqual(result, expected) -class TestFrozenList(CheckImmutable, unittest.TestCase): +class TestFrozenList(CheckImmutable, CheckStringMixin, unittest.TestCase): mutable_methods = ('extend', 'pop', 'remove', 'insert') + unicode_container = FrozenList([u("\u05d0"), u("\u05d1"), "c"]) def setUp(self): self.lst = [1, 2, 3, 4, 5] @@ -68,8 +89,9 @@ def test_inplace(self): self.check_result(r, self.lst) -class TestFrozenNDArray(CheckImmutable, unittest.TestCase): +class TestFrozenNDArray(CheckImmutable, CheckStringMixin, unittest.TestCase): mutable_methods = ('put', 'itemset', 'fill') + unicode_container = FrozenNDArray([u("\u05d0"), u("\u05d1"), "c"]) def setUp(self): self.lst = [3, 5, 7, -2]