Skip to content

Commit 4ef29e4

Browse files
committed
ENH: take a crack at #614
1 parent a99c057 commit 4ef29e4

File tree

5 files changed

+70
-10
lines changed

5 files changed

+70
-10
lines changed

pandas/core/frame.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2656,7 +2656,8 @@ def apply(self, func, axis=0, broadcast=False, raw=False,
26562656
is_reduction = not isinstance(f(_EMPTY_SERIES),
26572657
np.ndarray)
26582658
if is_reduction:
2659-
return Series(np.nan, index=self._get_agg_axis(axis))
2659+
return Series(np.nan,
2660+
index=self._get_agg_axis(axis))
26602661
else:
26612662
return self.copy()
26622663

@@ -2670,7 +2671,7 @@ def apply(self, func, axis=0, broadcast=False, raw=False,
26702671
def _apply_raw(self, func, axis):
26712672
try:
26722673
result = lib.reduce(self.values, func, axis=axis)
2673-
except Exception:
2674+
except Exception, e:
26742675
result = np.apply_along_axis(func, axis, self.values)
26752676

26762677
# TODO: mixed type case
@@ -2715,8 +2716,13 @@ def _apply_standard(self, func, axis, ignore_failures=False):
27152716
if len(successes) < len(res_index):
27162717
res_index = res_index.take(successes)
27172718
else:
2718-
for k, v in series_gen:
2719-
results[k] = func(v)
2719+
try:
2720+
for k, v in series_gen:
2721+
results[k] = func(v)
2722+
except Exception, e:
2723+
if hasattr(e, 'args'):
2724+
e.args = e.args + ('occurred at index %s' % str(k),)
2725+
raise
27202726

27212727
if len(results) > 0 and _is_sequence(results.values()[0]):
27222728
if not isinstance(results.values()[0], Series):
@@ -2729,7 +2735,7 @@ def _apply_standard(self, func, axis, ignore_failures=False):
27292735
if axis == 1:
27302736
result = result.T
27312737

2732-
return result
2738+
return result.convert_objects()
27332739
else:
27342740
return Series(results, index=res_index)
27352741

pandas/sparse/frame.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,10 @@ def _consolidate_inplace(self):
107107
# do nothing when DataFrame calls this method
108108
pass
109109

110+
def convert_objects(self):
111+
# XXX
112+
return self
113+
110114
@property
111115
def _constructor(self):
112116
def wrapper(data, index=None, columns=None):

pandas/src/reduce.pyx

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@ cdef class Reducer:
6767
PyArray_SETITEM(result, PyArray_ITER_DATA(it), res)
6868
chunk.data = chunk.data + self.increment
6969
PyArray_ITER_NEXT(it)
70+
except Exception, e:
71+
if hasattr(e, 'args'):
72+
e.args = e.args + (i,)
7073
finally:
7174
# so we don't free the wrong memory
7275
chunk.data = dummy_buf
@@ -80,10 +83,6 @@ cdef class Reducer:
8083
assert(not (isinstance(res, list) and len(res) == len(self.dummy)))
8184

8285
result = np.empty(self.nresults, dtype='O')
83-
# if hasattr(res, 'dtype'):
84-
# result = np.empty(self.nresults, dtype=res.dtype)
85-
# else:
86-
# result = np.empty(self.nresults, dtype='O')
8786
result[0] = res
8887
except Exception:
8988
raise ValueError('function does not reduce')

pandas/tests/test_frame.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3162,6 +3162,58 @@ def test_apply_differently_indexed(self):
31623162
columns=df.index).T
31633163
assert_frame_equal(result1, expected1)
31643164

3165+
def test_apply_modify_traceback(self):
3166+
data = DataFrame({'A' : ['foo', 'foo', 'foo', 'foo',
3167+
'bar', 'bar', 'bar', 'bar',
3168+
'foo', 'foo', 'foo'],
3169+
'B' : ['one', 'one', 'one', 'two',
3170+
'one', 'one', 'one', 'two',
3171+
'two', 'two', 'one'],
3172+
'C' : ['dull', 'dull', 'shiny', 'dull',
3173+
'dull', 'shiny', 'shiny', 'dull',
3174+
'shiny', 'shiny', 'shiny'],
3175+
'D' : np.random.randn(11),
3176+
'E' : np.random.randn(11),
3177+
'F' : np.random.randn(11)})
3178+
3179+
data['C'][4] = np.nan
3180+
3181+
def transform(row):
3182+
if row['C'].startswith('shin') and row['A'] == 'foo':
3183+
row['D'] = 7
3184+
return row
3185+
3186+
def transform2(row):
3187+
if (notnull(row['C']) and row['C'].startswith('shin')
3188+
and row['A'] == 'foo'):
3189+
row['D'] = 7
3190+
return row
3191+
3192+
try:
3193+
transformed = data.apply(transform, axis=1)
3194+
except Exception, e:
3195+
pass
3196+
3197+
self.assertEqual(len(e.args), 2)
3198+
self.assertEqual(e.args[1], 'occurred at index 4')
3199+
3200+
def test_apply_convert_objects(self):
3201+
data = DataFrame({'A' : ['foo', 'foo', 'foo', 'foo',
3202+
'bar', 'bar', 'bar', 'bar',
3203+
'foo', 'foo', 'foo'],
3204+
'B' : ['one', 'one', 'one', 'two',
3205+
'one', 'one', 'one', 'two',
3206+
'two', 'two', 'one'],
3207+
'C' : ['dull', 'dull', 'shiny', 'dull',
3208+
'dull', 'shiny', 'shiny', 'dull',
3209+
'shiny', 'shiny', 'shiny'],
3210+
'D' : np.random.randn(11),
3211+
'E' : np.random.randn(11),
3212+
'F' : np.random.randn(11)})
3213+
3214+
result = data.apply(lambda x: x, axis=1)
3215+
assert_frame_equal(result, data)
3216+
31653217
def test_applymap(self):
31663218
applied = self.frame.applymap(lambda x: x * 2)
31673219
assert_frame_equal(applied, self.frame * 2)

scripts/roll_median_leak.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
pid = os.getpid()
1515
proc = psutil.Process(pid)
1616

17-
1817
s = Series(np.random.randn(10000))
1918

2019
for _ in xrange(5):

0 commit comments

Comments
 (0)