Skip to content

Commit 282bf26

Browse files
committed
Merge pull request #5077 from jtratner/fix-mi-repr
BUG: Make Index, Int64Index and MI repr evalable
2 parents 24774a1 + 763abbd commit 282bf26

File tree

4 files changed

+25
-13
lines changed

4 files changed

+25
-13
lines changed

pandas/core/base.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -175,4 +175,4 @@ def __unicode__(self):
175175
Invoked by unicode(df) in py2 only. Yields a Unicode String in both py2/py3.
176176
"""
177177
prepr = com.pprint_thing(self, escape_chars=('\t', '\r', '\n'),quote_strings=True)
178-
return '%s(%s, dtype=%s)' % (type(self).__name__, prepr, self.dtype)
178+
return "%s(%s, dtype='%s')" % (type(self).__name__, prepr, self.dtype)

pandas/core/index.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -2044,7 +2044,7 @@ def __repr__(self):
20442044
attrs.append(('sortorder', default_pprint(self.sortorder)))
20452045

20462046
space = ' ' * (len(self.__class__.__name__) + 1)
2047-
prepr = (u("\n%s") % space).join([u("%s=%s") % (k, v)
2047+
prepr = (u(",\n%s") % space).join([u("%s=%s") % (k, v)
20482048
for k, v in attrs])
20492049
res = u("%s(%s)") % (self.__class__.__name__, prepr)
20502050

pandas/tests/test_format.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1456,7 +1456,7 @@ def test_to_html_with_classes(self):
14561456
<table border="1" class="dataframe sortable draggable">
14571457
<tbody>
14581458
<tr>
1459-
<td>Index([], dtype=object)</td>
1459+
<td>Index([], dtype='object')</td>
14601460
<td>Empty DataFrame</td>
14611461
</tr>
14621462
</tbody>

pandas/tests/test_index.py

+22-10
Original file line numberDiff line numberDiff line change
@@ -36,21 +36,23 @@ class TestIndex(unittest.TestCase):
3636
_multiprocess_can_split_ = True
3737

3838
def setUp(self):
39-
self.unicodeIndex = tm.makeUnicodeIndex(100)
40-
self.strIndex = tm.makeStringIndex(100)
41-
self.dateIndex = tm.makeDateIndex(100)
42-
self.intIndex = tm.makeIntIndex(100)
43-
self.floatIndex = tm.makeFloatIndex(100)
44-
self.empty = Index([])
45-
self.tuples = Index(lzip(['foo', 'bar', 'baz'], [1, 2, 3]))
39+
self.indices = dict(
40+
unicodeIndex = tm.makeUnicodeIndex(100),
41+
strIndex = tm.makeStringIndex(100),
42+
dateIndex = tm.makeDateIndex(100),
43+
intIndex = tm.makeIntIndex(100),
44+
floatIndex = tm.makeFloatIndex(100),
45+
empty = Index([]),
46+
tuples = Index(lzip(['foo', 'bar', 'baz'], [1, 2, 3])),
47+
)
48+
for name, ind in self.indices.items():
49+
setattr(self, name, ind)
4650

4751
def test_wrong_number_names(self):
4852
def testit(ind):
4953
ind.names = ["apple", "banana", "carrot"]
5054

51-
indices = (self.dateIndex, self.unicodeIndex, self.strIndex,
52-
self.intIndex, self.floatIndex, self.empty, self.tuples)
53-
for ind in indices:
55+
for ind in self.indices.values():
5456
assertRaisesRegexp(ValueError, "^Length", testit, ind)
5557

5658
def test_set_name_methods(self):
@@ -700,6 +702,10 @@ def test_hash_error(self):
700702
type(self.float).__name__):
701703
hash(self.float)
702704

705+
def test_repr_roundtrip(self):
706+
for ind in (self.mixed, self.float):
707+
tm.assert_index_equal(eval(repr(ind)), ind)
708+
703709
def check_is_index(self, i):
704710
self.assert_(isinstance(i, Index) and not isinstance(i, Float64Index))
705711

@@ -1167,6 +1173,9 @@ def test_repr_summary(self):
11671173
self.assertTrue(len(r) < 100)
11681174
self.assertTrue("..." in r)
11691175

1176+
def test_repr_roundtrip(self):
1177+
tm.assert_index_equal(eval(repr(self.index)), self.index)
1178+
11701179
def test_unicode_string_with_unicode(self):
11711180
idx = Index(lrange(1000))
11721181

@@ -2291,6 +2300,9 @@ def test_repr_with_unicode_data(self):
22912300
index = pd.DataFrame(d).set_index(["a", "b"]).index
22922301
self.assertFalse("\\u" in repr(index)) # we don't want unicode-escaped
22932302

2303+
def test_repr_roundtrip(self):
2304+
tm.assert_index_equal(eval(repr(self.index)), self.index)
2305+
22942306
def test_unicode_string_with_unicode(self):
22952307
d = {"a": [u("\u05d0"), 2, 3], "b": [4, 5, 6], "c": [7, 8, 9]}
22962308
idx = pd.DataFrame(d).set_index(["a", "b"]).index

0 commit comments

Comments
 (0)