From 763abbd3a1c77a2c54875774082748c6f85c1323 Mon Sep 17 00:00:00 2001 From: Jeffrey Tratner Date: Tue, 1 Oct 2013 20:40:40 -0400 Subject: [PATCH] BUG: Make Index, Int64Index and MI repr evalable Only talking about Index, Int64Index and MultiIndex. Tseries indices (like PeriodIndex and DatetimeIndex) are more complicated and could be covered separately. --- pandas/core/base.py | 2 +- pandas/core/index.py | 2 +- pandas/tests/test_format.py | 2 +- pandas/tests/test_index.py | 32 ++++++++++++++++++++++---------- 4 files changed, 25 insertions(+), 13 deletions(-) diff --git a/pandas/core/base.py b/pandas/core/base.py index f390592a6f6c4..2acc045156720 100644 --- a/pandas/core/base.py +++ b/pandas/core/base.py @@ -175,4 +175,4 @@ def __unicode__(self): 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) + return "%s(%s, dtype='%s')" % (type(self).__name__, prepr, self.dtype) diff --git a/pandas/core/index.py b/pandas/core/index.py index 465a0439c6eb3..98f190360bc33 100644 --- a/pandas/core/index.py +++ b/pandas/core/index.py @@ -2044,7 +2044,7 @@ def __repr__(self): attrs.append(('sortorder', default_pprint(self.sortorder))) space = ' ' * (len(self.__class__.__name__) + 1) - prepr = (u("\n%s") % space).join([u("%s=%s") % (k, v) + prepr = (u(",\n%s") % space).join([u("%s=%s") % (k, v) for k, v in attrs]) res = u("%s(%s)") % (self.__class__.__name__, prepr) diff --git a/pandas/tests/test_format.py b/pandas/tests/test_format.py index 55f70e9e4fe28..d9bf8adb71298 100644 --- a/pandas/tests/test_format.py +++ b/pandas/tests/test_format.py @@ -1456,7 +1456,7 @@ def test_to_html_with_classes(self): - + diff --git a/pandas/tests/test_index.py b/pandas/tests/test_index.py index 11538ae8b3ab8..cd26016acba5c 100644 --- a/pandas/tests/test_index.py +++ b/pandas/tests/test_index.py @@ -36,21 +36,23 @@ class TestIndex(unittest.TestCase): _multiprocess_can_split_ = True def setUp(self): - self.unicodeIndex = tm.makeUnicodeIndex(100) - self.strIndex = tm.makeStringIndex(100) - self.dateIndex = tm.makeDateIndex(100) - self.intIndex = tm.makeIntIndex(100) - self.floatIndex = tm.makeFloatIndex(100) - self.empty = Index([]) - self.tuples = Index(lzip(['foo', 'bar', 'baz'], [1, 2, 3])) + self.indices = dict( + unicodeIndex = tm.makeUnicodeIndex(100), + strIndex = tm.makeStringIndex(100), + dateIndex = tm.makeDateIndex(100), + intIndex = tm.makeIntIndex(100), + floatIndex = tm.makeFloatIndex(100), + empty = Index([]), + tuples = Index(lzip(['foo', 'bar', 'baz'], [1, 2, 3])), + ) + for name, ind in self.indices.items(): + setattr(self, name, ind) def test_wrong_number_names(self): def testit(ind): ind.names = ["apple", "banana", "carrot"] - indices = (self.dateIndex, self.unicodeIndex, self.strIndex, - self.intIndex, self.floatIndex, self.empty, self.tuples) - for ind in indices: + for ind in self.indices.values(): assertRaisesRegexp(ValueError, "^Length", testit, ind) def test_set_name_methods(self): @@ -700,6 +702,10 @@ def test_hash_error(self): type(self.float).__name__): hash(self.float) + def test_repr_roundtrip(self): + for ind in (self.mixed, self.float): + tm.assert_index_equal(eval(repr(ind)), ind) + def check_is_index(self, i): self.assert_(isinstance(i, Index) and not isinstance(i, Float64Index)) @@ -1167,6 +1173,9 @@ def test_repr_summary(self): self.assertTrue(len(r) < 100) self.assertTrue("..." in r) + def test_repr_roundtrip(self): + tm.assert_index_equal(eval(repr(self.index)), self.index) + def test_unicode_string_with_unicode(self): idx = Index(lrange(1000)) @@ -2291,6 +2300,9 @@ def test_repr_with_unicode_data(self): index = pd.DataFrame(d).set_index(["a", "b"]).index self.assertFalse("\\u" in repr(index)) # we don't want unicode-escaped + def test_repr_roundtrip(self): + tm.assert_index_equal(eval(repr(self.index)), self.index) + def test_unicode_string_with_unicode(self): d = {"a": [u("\u05d0"), 2, 3], "b": [4, 5, 6], "c": [7, 8, 9]} idx = pd.DataFrame(d).set_index(["a", "b"]).index
Index([], dtype=object)Index([], dtype='object') Empty DataFrame