Skip to content

Commit

Permalink
ENH: pass list of array to create MultiIndex, close #831
Browse files Browse the repository at this point in the history
  • Loading branch information
wesm committed May 7, 2012
1 parent 41b3f9c commit 4c31c83
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 3 deletions.
3 changes: 3 additions & 0 deletions RELEASE.rst
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,16 @@ pandas 0.8.0
etc. (#825 and others)
- Add support for indexes (dates or otherwise) with duplicates and common
sense indexing/selection functionality
- Series/DataFrame.update methods, in-place variant of combine_first (#961)

**Improvements to existing features**

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

**API Changes**

Expand Down
5 changes: 4 additions & 1 deletion pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -415,11 +415,14 @@ def _init_ndarray(self, values, index, columns, dtype=None,

if index is None:
index = _default_index(N)
else:
index = _ensure_index(index)

if columns is None:
columns = _default_index(K)
else:
columns = _ensure_index(columns)

columns = _ensure_index(columns)
block = make_block(values.T, columns, columns)
return BlockManager([block], [columns, index])

Expand Down
5 changes: 5 additions & 0 deletions pandas/core/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -2242,6 +2242,11 @@ def _ensure_index(index_like):
return index_like
if hasattr(index_like, 'name'):
return Index(index_like, name=index_like.name)

if isinstance(index_like, list):
if len(index_like) and isinstance(index_like[0], (list, np.ndarray)):
return MultiIndex.from_arrays(index_like)

return Index(index_like)

def _validate_join_method(method):
Expand Down
5 changes: 3 additions & 2 deletions pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,9 @@ def __new__(cls, data=None, index=None, dtype=None, name=None,
if dtype is not None:
dtype = np.dtype(dtype)

if index is not None:
index = _ensure_index(index)

subarr = _sanitize_array(data, index, dtype, copy,
raise_cast_failure=True)

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

if index is None:
index = _default_index(len(subarr))
else:
index = _ensure_index(index)

# Change the class of the array to be the subclass type.
if index.is_all_dates:
Expand Down
21 changes: 21 additions & 0 deletions pandas/tests/test_multilevel.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,27 @@ def test_append(self):
result = a['A'].append(b['A'])
tm.assert_series_equal(result, self.frame['A'])

def test_dataframe_constructor(self):
multi = DataFrame(np.random.randn(4, 4),
index=[np.array(['a', 'a', 'b', 'b']),
np.array(['x', 'y', 'x', 'y'])])
self.assert_(isinstance(multi.index, MultiIndex))
self.assert_(not isinstance(multi.columns, MultiIndex))

multi = DataFrame(np.random.randn(4, 4),
columns=[['a', 'a', 'b', 'b'],
['x', 'y', 'x', 'y']])
self.assert_(isinstance(multi.columns, MultiIndex))

def test_series_constructor(self):
multi = Series(1., index=[np.array(['a', 'a', 'b', 'b']),
np.array(['x', 'y', 'x', 'y'])])
self.assert_(isinstance(multi.index, MultiIndex))

multi = Series(1., index=[['a', 'a', 'b', 'b'],
['x', 'y', 'x', 'y']])
self.assert_(isinstance(multi.index, MultiIndex))

def test_reindex_level(self):
# axis=0
month_sums = self.ymd.sum(level='month')
Expand Down

0 comments on commit 4c31c83

Please sign in to comment.