0

I have a recursive function

def boost_test(frame, iteration, max_iteration):
    if iteration < max_iteration:
        print iteration, max_iteration
        # get predictions
        iteration += 1
        boost_test(frame, iteration, max_iteration)
    elif iteration == max_iteration:
        print 'return frame', iteration, max_iteration, frame.columns
        #print frame
        return frame

I can run it like this:

testF = pd.DataFrame([1,2])
testF.columns = ['hello']
n = boost_test(testF, 0, 5)

Here is the output: 0 5 1 5 2 5 3 5 4 5 return frame 5 5 Index([u'hello'], dtype='object')

So, it looks like everything is correct. But if it doesn't look like the function is actually returning a frame.

When I try n.head()

I get:

AttributeError: 'NoneType' object has no attribute 'head'

I'm at a loss for why it isn't returning the dataframe. Any help is much appreciated. Thanks!

cloud36
  • 1,026
  • 6
  • 21
  • 35
  • You need to `return` in all execution paths of the recursive function, so the `if` block should end with `return boost_test(frame, iteration, max_iteration)`. Otherwise, that section will return the default value of `None`. – PM 2Ring Feb 02 '17 at 06:09

1 Answers1

1
boost_test(frame, iteration, max_iteration)

Here, you make the recursive call, but do nothing with the result. You need to explicitly return the value for it to make its way back up the call stack.

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153