Skip to content

Commit 27e34a4

Browse files
committed
BUG: improper MultiIndex conversion issue. close #2200
1 parent bf80c0a commit 27e34a4

File tree

4 files changed

+30
-2
lines changed

4 files changed

+30
-2
lines changed

RELEASE.rst

+3
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,9 @@ pandas 0.9.1
9494
- Pass through timezone information when calling DataFrame.align (#2127)
9595
- Properly sort when joining on datetime64 values (#2196)
9696
- Fix indexing bug in which False/True were being coerced to 0/1 (#2199)
97+
- Many unicode formatting fixes (#2201)
98+
- Fix improper MultiIndex conversion issue when assigning
99+
e.g. DataFrame.index (#2200)
97100
98101
pandas 0.9.0
99102
============

pandas/core/index.py

+8-1
Original file line numberDiff line numberDiff line change
@@ -2438,8 +2438,15 @@ def _ensure_index(index_like):
24382438
return Index(index_like, name=index_like.name)
24392439

24402440
if isinstance(index_like, list):
2441-
if len(index_like) and isinstance(index_like[0], (list, np.ndarray)):
2441+
klasses = (list, np.ndarray)
2442+
all_arrays = all(isinstance(x, klasses) for x in index_like)
2443+
2444+
if len(index_like) > 0 and all_arrays:
24422445
return MultiIndex.from_arrays(index_like)
2446+
else:
2447+
# #2200 ?
2448+
index_like = [tuple(x) if isinstance(x, klasses) else x
2449+
for x in index_like]
24432450

24442451
return Index(index_like)
24452452

pandas/core/internals.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -506,10 +506,12 @@ def is_mixed_dtype(self):
506506

507507
def set_axis(self, axis, value):
508508
cur_axis = self.axes[axis]
509+
value = _ensure_index(value)
510+
509511
if len(value) != len(cur_axis):
510512
raise Exception('Length mismatch (%d vs %d)'
511513
% (len(value), len(cur_axis)))
512-
self.axes[axis] = _ensure_index(value)
514+
self.axes[axis] = value
513515

514516
if axis == 0:
515517
for block in self.blocks:

pandas/tests/test_multilevel.py

+16
Original file line numberDiff line numberDiff line change
@@ -1578,6 +1578,22 @@ def test_multiindex_na_repr(self):
15781578
idf = df3.set_index(['A' * 30, 'C' * 30])
15791579
repr(idf)
15801580

1581+
def test_assign_index_sequences(self):
1582+
# #2200
1583+
df = DataFrame({"a":[1,2,3],
1584+
"b":[4,5,6],
1585+
"c":[7,8,9]}).set_index(["a","b"])
1586+
l = list(df.index)
1587+
l[0]=("faz","boo")
1588+
df.index = l
1589+
repr(df)
1590+
1591+
# this travels an improper code path
1592+
l[0] = ["faz","boo"]
1593+
df.index = l
1594+
repr(df)
1595+
1596+
15811597
if __name__ == '__main__':
15821598

15831599
# unittest.main()

0 commit comments

Comments
 (0)