Skip to content

Commit 33fbcbb

Browse files
committed
Merge pull request #5089 from jreback/mi_constructor
TST: Tests for multi-index construction of an all-nan frame (GH4078)
2 parents 8dbbf56 + 6418d2f commit 33fbcbb

File tree

3 files changed

+28
-7
lines changed

3 files changed

+28
-7
lines changed

doc/source/release.rst

+1
Original file line numberDiff line numberDiff line change
@@ -524,6 +524,7 @@ Bug Fixes
524524
- Bug in setting with ``ix/loc`` and a mixed int/string index (:issue:`4544`)
525525
- Make sure series-series boolean comparions are label based (:issue:`4947`)
526526
- Bug in multi-level indexing with a Timestamp partial indexer (:issue:`4294`)
527+
- Tests/fix for multi-index construction of an all-nan frame (:isue:`4078`)
527528

528529
pandas 0.12.0
529530
-------------

pandas/core/internals.py

+10-3
Original file line numberDiff line numberDiff line change
@@ -3530,9 +3530,16 @@ def _shape_compat(x):
35303530
if ref_items.is_unique:
35313531
items = ref_items[ref_items.isin(names)]
35323532
else:
3533-
items = _ensure_index([n for n in names if n in ref_items])
3534-
if len(items) != len(stacked):
3535-
raise Exception("invalid names passed _stack_arrays")
3533+
# a mi
3534+
if isinstance(ref_items, MultiIndex):
3535+
names = MultiIndex.from_tuples(names)
3536+
items = ref_items[ref_items.isin(names)]
3537+
3538+
# plain old dups
3539+
else:
3540+
items = _ensure_index([n for n in names if n in ref_items])
3541+
if len(items) != len(stacked):
3542+
raise ValueError("invalid names passed _stack_arrays")
35363543

35373544
return items, stacked, placement
35383545

pandas/tests/test_frame.py

+17-4
Original file line numberDiff line numberDiff line change
@@ -2263,10 +2263,6 @@ def test_constructor_overflow_int64(self):
22632263
df_crawls = DataFrame(data)
22642264
self.assert_(df_crawls['uid'].dtype == object)
22652265

2266-
def test_is_mixed_type(self):
2267-
self.assert_(not self.frame._is_mixed_type)
2268-
self.assert_(self.mixed_frame._is_mixed_type)
2269-
22702266
def test_constructor_ordereddict(self):
22712267
import random
22722268
nitems = 100
@@ -2319,6 +2315,19 @@ def test_constructor_dict(self):
23192315
frame = DataFrame({'A': [], 'B': []}, columns=['A', 'B'])
23202316
self.assert_(frame.index.equals(Index([])))
23212317

2318+
def test_constructor_multi_index(self):
2319+
# GH 4078
2320+
# construction error with mi and all-nan frame
2321+
tuples = [(2, 3), (3, 3), (3, 3)]
2322+
mi = MultiIndex.from_tuples(tuples)
2323+
df = DataFrame(index=mi,columns=mi)
2324+
self.assert_(pd.isnull(df).values.ravel().all())
2325+
2326+
tuples = [(3, 3), (2, 3), (3, 3)]
2327+
mi = MultiIndex.from_tuples(tuples)
2328+
df = DataFrame(index=mi,columns=mi)
2329+
self.assert_(pd.isnull(df).values.ravel().all())
2330+
23222331
def test_constructor_error_msgs(self):
23232332
msg = "Mixing dicts with non-Series may lead to ambiguous ordering."
23242333
# mix dict and array, wrong size
@@ -9489,6 +9498,10 @@ def test_get_X_columns(self):
94899498
self.assert_(np.array_equal(df._get_numeric_data().columns,
94909499
['a', 'b', 'e']))
94919500

9501+
def test_is_mixed_type(self):
9502+
self.assert_(not self.frame._is_mixed_type)
9503+
self.assert_(self.mixed_frame._is_mixed_type)
9504+
94929505
def test_get_numeric_data(self):
94939506
intname = np.dtype(np.int_).name
94949507
floatname = np.dtype(np.float_).name

0 commit comments

Comments
 (0)