Skip to content

Commit 7b93f72

Browse files
committed
BUG: don't invoke function twice in DataFrame.apply
1 parent 0d2e1a7 commit 7b93f72

File tree

4 files changed

+22
-20
lines changed

4 files changed

+22
-20
lines changed

pandas/core/frame.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -2267,7 +2267,7 @@ def _apply_standard(self, func, axis, ignore_failures=False):
22672267
dummy = Series(np.nan, index=self._get_axis(axis),
22682268
dtype=values.dtype)
22692269
result = lib.reduce(values, func, axis=axis, dummy=dummy)
2270-
return Series(result, index=agg_axis)
2270+
return Series(result, index=self._get_agg_axis(axis))
22712271
except Exception:
22722272
pass
22732273

pandas/src/reduce.pyx

+14-15
Original file line numberDiff line numberDiff line change
@@ -50,29 +50,28 @@ cdef class Reducer:
5050
ndarray arr, result, chunk
5151
Py_ssize_t i
5252
flatiter it
53+
object res
5354

5455
arr = self.arr
5556
chunk = self.dummy
56-
57-
test = self.f(chunk)
58-
try:
59-
assert(not isinstance(test, np.ndarray))
60-
if hasattr(test, 'dtype'):
61-
result = np.empty(self.nresults, dtype=test.dtype)
62-
else:
63-
result = np.empty(self.nresults, dtype='O')
64-
result[0] = test
65-
except Exception:
66-
raise ValueError('function does not reduce')
67-
6857
it = <flatiter> PyArray_IterNew(result)
6958

7059
dummy_buf = chunk.data
7160
chunk.data = arr.data
7261
try:
73-
for i in range(self.nresults):
74-
PyArray_SETITEM(result, PyArray_ITER_DATA(it),
75-
self.f(self.dummy))
62+
for i in range(1, self.nresults):
63+
res = self.f(self.dummy)
64+
if i == 0:
65+
try:
66+
assert(not isinstance(res, np.ndarray))
67+
if hasattr(res, 'dtype'):
68+
result = np.empty(self.nresults, dtype=res.dtype)
69+
else:
70+
result = np.empty(self.nresults, dtype='O')
71+
except Exception:
72+
raise ValueError('function does not reduce')
73+
74+
PyArray_SETITEM(result, PyArray_ITER_DATA(it), res)
7675
chunk.data = chunk.data + self.increment
7776
PyArray_ITER_NEXT(it)
7877
finally:

pandas/tools/pivot.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@ def pivot_table(data, values=None, rows=None, cols=None, aggfunc=np.mean,
7979

8080
return table
8181

82+
DataFrame.pivot_table = pivot_table
83+
8284
def _convert_by(by):
8385
if by is None:
8486
by = []
@@ -88,8 +90,6 @@ def _convert_by(by):
8890
by = list(by)
8991
return by
9092

91-
def pprint_table(table):
92-
pass
9393

9494
if __name__ == '__main__':
9595
def _sample(values, n):

pandas/tools/tests/test_pivot.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ def test_pivot_table(self):
2727
cols= 'C'
2828
table = pivot_table(self.data, values='D', rows=rows, cols=cols)
2929

30+
# this works
31+
pivot_table(self.data, values='D', rows=rows)
32+
3033
if len(rows) > 1:
3134
self.assertEqual(table.index.names, rows)
3235
else:
@@ -49,9 +52,9 @@ def test_pivot_table_multiple(self):
4952

5053
def test_pivot_multi_values(self):
5154
result = pivot_table(self.data, values=['D', 'E'],
52-
rows='A', cols=['B', 'C'])
55+
rows='A', cols=['B', 'C'], fill_value=0)
5356
expected = pivot_table(self.data.drop(['F'], axis=1),
54-
rows='A', cols=['B', 'C'])
57+
rows='A', cols=['B', 'C'], fill_value=0)
5558
assert_frame_equal(result, expected)
5659

5760
if __name__ == '__main__':

0 commit comments

Comments
 (0)