diff --git a/doc/source/release.rst b/doc/source/release.rst index 4e5178d8a554a..057802dc51af5 100644 --- a/doc/source/release.rst +++ b/doc/source/release.rst @@ -524,6 +524,7 @@ Bug Fixes - Bug in setting with ``ix/loc`` and a mixed int/string index (:issue:`4544`) - Make sure series-series boolean comparions are label based (:issue:`4947`) - Bug in multi-level indexing with a Timestamp partial indexer (:issue:`4294`) + - Tests/fix for multi-index construction of an all-nan frame (:isue:`4078`) pandas 0.12.0 ------------- diff --git a/pandas/core/internals.py b/pandas/core/internals.py index fd9aed58798fe..6fddc44d7552e 100644 --- a/pandas/core/internals.py +++ b/pandas/core/internals.py @@ -3530,9 +3530,16 @@ def _shape_compat(x): if ref_items.is_unique: items = ref_items[ref_items.isin(names)] else: - items = _ensure_index([n for n in names if n in ref_items]) - if len(items) != len(stacked): - raise Exception("invalid names passed _stack_arrays") + # a mi + if isinstance(ref_items, MultiIndex): + names = MultiIndex.from_tuples(names) + items = ref_items[ref_items.isin(names)] + + # plain old dups + else: + items = _ensure_index([n for n in names if n in ref_items]) + if len(items) != len(stacked): + raise ValueError("invalid names passed _stack_arrays") return items, stacked, placement diff --git a/pandas/tests/test_frame.py b/pandas/tests/test_frame.py index e8d9f3a7fc7cc..1e4e988431f43 100644 --- a/pandas/tests/test_frame.py +++ b/pandas/tests/test_frame.py @@ -2263,10 +2263,6 @@ def test_constructor_overflow_int64(self): df_crawls = DataFrame(data) self.assert_(df_crawls['uid'].dtype == object) - def test_is_mixed_type(self): - self.assert_(not self.frame._is_mixed_type) - self.assert_(self.mixed_frame._is_mixed_type) - def test_constructor_ordereddict(self): import random nitems = 100 @@ -2319,6 +2315,19 @@ def test_constructor_dict(self): frame = DataFrame({'A': [], 'B': []}, columns=['A', 'B']) self.assert_(frame.index.equals(Index([]))) + def test_constructor_multi_index(self): + # GH 4078 + # construction error with mi and all-nan frame + tuples = [(2, 3), (3, 3), (3, 3)] + mi = MultiIndex.from_tuples(tuples) + df = DataFrame(index=mi,columns=mi) + self.assert_(pd.isnull(df).values.ravel().all()) + + tuples = [(3, 3), (2, 3), (3, 3)] + mi = MultiIndex.from_tuples(tuples) + df = DataFrame(index=mi,columns=mi) + self.assert_(pd.isnull(df).values.ravel().all()) + def test_constructor_error_msgs(self): msg = "Mixing dicts with non-Series may lead to ambiguous ordering." # mix dict and array, wrong size @@ -9489,6 +9498,10 @@ def test_get_X_columns(self): self.assert_(np.array_equal(df._get_numeric_data().columns, ['a', 'b', 'e'])) + def test_is_mixed_type(self): + self.assert_(not self.frame._is_mixed_type) + self.assert_(self.mixed_frame._is_mixed_type) + def test_get_numeric_data(self): intname = np.dtype(np.int_).name floatname = np.dtype(np.float_).name