Skip to content

BUG: Fix FrozenNDArray & FrozenList string methods #4929

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/release.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
-------------
Expand Down
16 changes: 14 additions & 2 deletions pandas/core/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
9 changes: 0 additions & 9 deletions pandas/core/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
26 changes: 24 additions & 2 deletions pandas/tests/test_base.py
Original file line number Diff line number Diff line change
@@ -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')

Expand Down Expand Up @@ -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]
Expand All @@ -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]
Expand Down