diff --git a/doc/source/v0.14.1.txt b/doc/source/v0.14.1.txt index f65a96f1c38a6..6b2ae95510fa1 100644 --- a/doc/source/v0.14.1.txt +++ b/doc/source/v0.14.1.txt @@ -163,7 +163,7 @@ Bug Fixes - +- Bug in ``Panel.apply`` with a multi-index as an axis (:issue:`7469`) - Bug in ``DatetimeIndex.insert`` doesn't preserve ``name`` and ``tz`` (:issue:`7299`) diff --git a/pandas/tests/test_panel.py b/pandas/tests/test_panel.py index 34ab401eac283..d0baa4b1ecad3 100644 --- a/pandas/tests/test_panel.py +++ b/pandas/tests/test_panel.py @@ -1180,6 +1180,16 @@ def test_apply_slabs(self): expected = Panel(dict([ (ax,f(self.panel.loc[:,ax])) for ax in self.panel.major_axis ])) assert_panel_equal(result,expected) + # with multi-indexes + # GH7469 + index = MultiIndex.from_tuples([('one', 'a'), ('one', 'b'), ('two', 'a'), ('two', 'b')]) + dfa = DataFrame(np.array(np.arange(12, dtype='int64')).reshape(4,3), columns=list("ABC"), index=index) + dfb = DataFrame(np.array(np.arange(10, 22, dtype='int64')).reshape(4,3), columns=list("ABC"), index=index) + p = Panel({'f':dfa, 'g':dfb}) + result = p.apply(lambda x: x.sum(), axis=0) + expected = p.sum(0) + assert_frame_equal(result,expected) + def test_reindex(self): ref = self.panel['ItemB'] diff --git a/pandas/tools/util.py b/pandas/tools/util.py index 1d6ed3e11c81e..215a76b84452a 100644 --- a/pandas/tools/util.py +++ b/pandas/tools/util.py @@ -3,6 +3,7 @@ from pandas.core.index import Index import numpy as np from pandas import algos +from pandas.core import common as com def match(needles, haystack): @@ -32,7 +33,7 @@ def cartesian_product(X): b = cumprodX[-1] / cumprodX - return [np.tile(np.repeat(np.asarray(x), b[i]), + return [np.tile(np.repeat(np.asarray(com._values_from_object(x)), b[i]), np.product(a[i])) for i, x in enumerate(X)]