Skip to content

Commit 23d5da4

Browse files
committed
BUG: Bug in Panel.apply with a multi-index as an axis (GH7469)
1 parent be2db88 commit 23d5da4

File tree

3 files changed

+13
-2
lines changed

3 files changed

+13
-2
lines changed

doc/source/v0.14.1.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ Bug Fixes
163163

164164

165165

166-
166+
- Bug in ``Panel.apply`` with a multi-index as an axis (:issue:`7469`)
167167

168168

169169
- Bug in ``DatetimeIndex.insert`` doesn't preserve ``name`` and ``tz`` (:issue:`7299`)

pandas/tests/test_panel.py

+10
Original file line numberDiff line numberDiff line change
@@ -1180,6 +1180,16 @@ def test_apply_slabs(self):
11801180
expected = Panel(dict([ (ax,f(self.panel.loc[:,ax])) for ax in self.panel.major_axis ]))
11811181
assert_panel_equal(result,expected)
11821182

1183+
# with multi-indexes
1184+
# GH7469
1185+
index = MultiIndex.from_tuples([('one', 'a'), ('one', 'b'), ('two', 'a'), ('two', 'b')])
1186+
dfa = DataFrame(np.array(np.arange(12, dtype='int64')).reshape(4,3), columns=list("ABC"), index=index)
1187+
dfb = DataFrame(np.array(np.arange(10, 22, dtype='int64')).reshape(4,3), columns=list("ABC"), index=index)
1188+
p = Panel({'f':dfa, 'g':dfb})
1189+
result = p.apply(lambda x: x.sum(), axis=0)
1190+
expected = p.sum(0)
1191+
assert_frame_equal(result,expected)
1192+
11831193
def test_reindex(self):
11841194
ref = self.panel['ItemB']
11851195

pandas/tools/util.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from pandas.core.index import Index
44
import numpy as np
55
from pandas import algos
6+
from pandas.core import common as com
67

78

89
def match(needles, haystack):
@@ -32,7 +33,7 @@ def cartesian_product(X):
3233

3334
b = cumprodX[-1] / cumprodX
3435

35-
return [np.tile(np.repeat(np.asarray(x), b[i]),
36+
return [np.tile(np.repeat(np.asarray(com._values_from_object(x)), b[i]),
3637
np.product(a[i]))
3738
for i, x in enumerate(X)]
3839

0 commit comments

Comments
 (0)