Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6c27568

Browse files
committed
BUG: GH10332 where Panel.apply does not handle result with ndim=0 correctly
1 parent 814dbe8 commit 6c27568

File tree

3 files changed

+23
-6
lines changed

3 files changed

+23
-6
lines changed

doc/source/whatsnew/v0.16.2.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,3 +169,5 @@ Bug Fixes
169169
- Bug where MySQL interface could not handle numeric table/column names (:issue:`10255`)
170170

171171
- Bug in ``read_csv`` with a ``date_parser`` that returned a ``datetime64`` array of other time resolution than ``[ns]`` (:issue:`10245`)
172+
173+
- Bug in ``Panel.apply`` when the result has ndim = 0 (:issue:`10332`)

pandas/core/panel.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1093,14 +1093,10 @@ def _construct_return_type(self, result, axes=None):
10931093
# need to assume they are the same
10941094
if ndim is None:
10951095
if isinstance(result,dict):
1096-
ndim = getattr(list(compat.itervalues(result))[0],'ndim',None)
1097-
1098-
# a saclar result
1099-
if ndim is None:
1100-
ndim = 0
1096+
ndim = getattr(list(compat.itervalues(result))[0],'ndim',0)
11011097

11021098
# have a dict, so top-level is +1 dim
1103-
else:
1099+
if ndim != 0:
11041100
ndim += 1
11051101

11061102
# scalar

pandas/tests/test_panel.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1233,6 +1233,25 @@ def test_apply_slabs(self):
12331233
expected = p.sum(0)
12341234
assert_frame_equal(result,expected)
12351235

1236+
def test_apply_no_or_zero_ndim(self):
1237+
# GH10332
1238+
self.panel = Panel(np.random.rand(5, 5, 5))
1239+
1240+
result_int = self.panel.apply(lambda df: 0, axis=[1, 2])
1241+
result_float = self.panel.apply(lambda df: 0.0, axis=[1, 2])
1242+
result_int64 = self.panel.apply(lambda df: np.int64(0), axis=[1, 2])
1243+
result_float64 = self.panel.apply(lambda df: np.float64(0.0),
1244+
axis=[1, 2])
1245+
1246+
expected_int = expected_int64 = Series([0] * 5)
1247+
expected_float = expected_float64 = Series([0.0] * 5)
1248+
1249+
assert_series_equal(result_int, expected_int)
1250+
assert_series_equal(result_int64, expected_int64)
1251+
assert_series_equal(result_float, expected_float)
1252+
assert_series_equal(result_float64, expected_float64)
1253+
1254+
12361255
def test_reindex(self):
12371256
ref = self.panel['ItemB']
12381257

0 commit comments

Comments
 (0)