Skip to content

Commit 1aa4ed4

Browse files
committed
Merge pull request #4929 from jtratner/fix-repr-on-FrozenNDArray-and-FrozenList
BUG: Fix FrozenNDArray & FrozenList string methods
2 parents 4cabd91 + cde7d28 commit 1aa4ed4

File tree

4 files changed

+39
-13
lines changed

4 files changed

+39
-13
lines changed

doc/source/release.rst

+1
Original file line numberDiff line numberDiff line change
@@ -446,6 +446,7 @@ Bug Fixes
446446
the ``by`` argument was passed (:issue:`4112`, :issue:`4113`).
447447
- Fixed a bug in ``convert_objects`` for > 2 ndims (:issue:`4937`)
448448
- Fixed a bug in DataFrame/Panel cache insertion and subsequent indexing (:issue:`4939`)
449+
- Fixed string methods for ``FrozenNDArray`` and ``FrozenList`` (:issue:`4929`)
449450

450451
pandas 0.12.0
451452
-------------

pandas/core/base.py

+14-2
Original file line numberDiff line numberDiff line change
@@ -122,9 +122,12 @@ def _disabled(self, *args, **kwargs):
122122

123123
def __unicode__(self):
124124
from pandas.core.common import pprint_thing
125+
return pprint_thing(self, quote_strings=True,
126+
escape_chars=('\t', '\r', '\n'))
127+
128+
def __repr__(self):
125129
return "%s(%s)" % (self.__class__.__name__,
126-
pprint_thing(self, quote_strings=True,
127-
escape_chars=('\t', '\r', '\n')))
130+
str(self))
128131

129132
__setitem__ = __setslice__ = __delitem__ = __delslice__ = _disabled
130133
pop = append = extend = remove = sort = insert = _disabled
@@ -154,3 +157,12 @@ def values(self):
154157
"""returns *copy* of underlying array"""
155158
arr = self.view(np.ndarray).copy()
156159
return arr
160+
161+
def __unicode__(self):
162+
"""
163+
Return a string representation for this object.
164+
165+
Invoked by unicode(df) in py2 only. Yields a Unicode String in both py2/py3.
166+
"""
167+
prepr = com.pprint_thing(self, escape_chars=('\t', '\r', '\n'),quote_strings=True)
168+
return '%s(%s, dtype=%s)' % (type(self).__name__, prepr, self.dtype)

pandas/core/index.py

-9
Original file line numberDiff line numberDiff line change
@@ -228,15 +228,6 @@ def copy(self, names=None, name=None, dtype=None, deep=False):
228228
new_index = new_index.astype(dtype)
229229
return new_index
230230

231-
def __unicode__(self):
232-
"""
233-
Return a string representation for a particular Index
234-
235-
Invoked by unicode(df) in py2 only. Yields a Unicode String in both py2/py3.
236-
"""
237-
prepr = com.pprint_thing(self, escape_chars=('\t', '\r', '\n'),quote_strings=True)
238-
return '%s(%s, dtype=%s)' % (type(self).__name__, prepr, self.dtype)
239-
240231
def to_series(self):
241232
"""
242233
return a series with both index and values equal to the index keys

pandas/tests/test_base.py

+24-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,30 @@
11
import re
22
import unittest
33
import numpy as np
4+
import pandas.compat as compat
5+
from pandas.compat import u
46
from pandas.core.base import FrozenList, FrozenNDArray
57
from pandas.util.testing import assertRaisesRegexp, assert_isinstance
68

79

10+
class CheckStringMixin(object):
11+
def test_string_methods_dont_fail(self):
12+
repr(self.container)
13+
str(self.container)
14+
bytes(self.container)
15+
if not compat.PY3:
16+
unicode(self.container)
17+
18+
def test_tricky_container(self):
19+
if not hasattr(self, 'unicode_container'):
20+
raise nose.SkipTest('Need unicode_container to test with this')
21+
repr(self.unicode_container)
22+
str(self.unicode_container)
23+
bytes(self.unicode_container)
24+
if not compat.PY3:
25+
unicode(self.unicode_container)
26+
27+
828
class CheckImmutable(object):
929
mutable_regex = re.compile('does not support mutable operations')
1030

@@ -43,8 +63,9 @@ def check_result(self, result, expected, klass=None):
4363
self.assertEqual(result, expected)
4464

4565

46-
class TestFrozenList(CheckImmutable, unittest.TestCase):
66+
class TestFrozenList(CheckImmutable, CheckStringMixin, unittest.TestCase):
4767
mutable_methods = ('extend', 'pop', 'remove', 'insert')
68+
unicode_container = FrozenList([u("\u05d0"), u("\u05d1"), "c"])
4869

4970
def setUp(self):
5071
self.lst = [1, 2, 3, 4, 5]
@@ -68,8 +89,9 @@ def test_inplace(self):
6889
self.check_result(r, self.lst)
6990

7091

71-
class TestFrozenNDArray(CheckImmutable, unittest.TestCase):
92+
class TestFrozenNDArray(CheckImmutable, CheckStringMixin, unittest.TestCase):
7293
mutable_methods = ('put', 'itemset', 'fill')
94+
unicode_container = FrozenNDArray([u("\u05d0"), u("\u05d1"), "c"])
7395

7496
def setUp(self):
7597
self.lst = [3, 5, 7, -2]

0 commit comments

Comments
 (0)