Skip to content

Commit 4c31c83

Browse files
committed
ENH: pass list of array to create MultiIndex, close #831
1 parent 41b3f9c commit 4c31c83

File tree

5 files changed

+36
-3
lines changed

5 files changed

+36
-3
lines changed

RELEASE.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,16 @@ pandas 0.8.0
3737
etc. (#825 and others)
3838
- Add support for indexes (dates or otherwise) with duplicates and common
3939
sense indexing/selection functionality
40+
- Series/DataFrame.update methods, in-place variant of combine_first (#961)
4041

4142
**Improvements to existing features**
4243

4344
- Switch to klib/khash-based hash tables in Index classes for better
4445
performance in many cases and lower memory footprint
4546
- Shipping some functions from scipy.stats to reduce dependency,
4647
e.g. Series.describe and DataFrame.describe (GH #1092)
48+
- Can create MultiIndex by passing list of lists or list of arrays to Series,
49+
DataFrame constructor, etc. (#831)
4750

4851
**API Changes**
4952

pandas/core/frame.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -415,11 +415,14 @@ def _init_ndarray(self, values, index, columns, dtype=None,
415415

416416
if index is None:
417417
index = _default_index(N)
418+
else:
419+
index = _ensure_index(index)
418420

419421
if columns is None:
420422
columns = _default_index(K)
423+
else:
424+
columns = _ensure_index(columns)
421425

422-
columns = _ensure_index(columns)
423426
block = make_block(values.T, columns, columns)
424427
return BlockManager([block], [columns, index])
425428

pandas/core/index.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2242,6 +2242,11 @@ def _ensure_index(index_like):
22422242
return index_like
22432243
if hasattr(index_like, 'name'):
22442244
return Index(index_like, name=index_like.name)
2245+
2246+
if isinstance(index_like, list):
2247+
if len(index_like) and isinstance(index_like[0], (list, np.ndarray)):
2248+
return MultiIndex.from_arrays(index_like)
2249+
22452250
return Index(index_like)
22462251

22472252
def _validate_join_method(method):

pandas/core/series.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,9 @@ def __new__(cls, data=None, index=None, dtype=None, name=None,
293293
if dtype is not None:
294294
dtype = np.dtype(dtype)
295295

296+
if index is not None:
297+
index = _ensure_index(index)
298+
296299
subarr = _sanitize_array(data, index, dtype, copy,
297300
raise_cast_failure=True)
298301

@@ -301,8 +304,6 @@ def __new__(cls, data=None, index=None, dtype=None, name=None,
301304

302305
if index is None:
303306
index = _default_index(len(subarr))
304-
else:
305-
index = _ensure_index(index)
306307

307308
# Change the class of the array to be the subclass type.
308309
if index.is_all_dates:

pandas/tests/test_multilevel.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,27 @@ def test_append(self):
5959
result = a['A'].append(b['A'])
6060
tm.assert_series_equal(result, self.frame['A'])
6161

62+
def test_dataframe_constructor(self):
63+
multi = DataFrame(np.random.randn(4, 4),
64+
index=[np.array(['a', 'a', 'b', 'b']),
65+
np.array(['x', 'y', 'x', 'y'])])
66+
self.assert_(isinstance(multi.index, MultiIndex))
67+
self.assert_(not isinstance(multi.columns, MultiIndex))
68+
69+
multi = DataFrame(np.random.randn(4, 4),
70+
columns=[['a', 'a', 'b', 'b'],
71+
['x', 'y', 'x', 'y']])
72+
self.assert_(isinstance(multi.columns, MultiIndex))
73+
74+
def test_series_constructor(self):
75+
multi = Series(1., index=[np.array(['a', 'a', 'b', 'b']),
76+
np.array(['x', 'y', 'x', 'y'])])
77+
self.assert_(isinstance(multi.index, MultiIndex))
78+
79+
multi = Series(1., index=[['a', 'a', 'b', 'b'],
80+
['x', 'y', 'x', 'y']])
81+
self.assert_(isinstance(multi.index, MultiIndex))
82+
6283
def test_reindex_level(self):
6384
# axis=0
6485
month_sums = self.ymd.sum(level='month')

0 commit comments

Comments
 (0)