0

I have a data set that contains 20 years of monthly averages in a numpy array with dimensions (1x240). I'm trying to write a function to spit out the yearly averages. I've managed to do it (I believe), using a for loop, but when I stick the exact same code into a function, it only gives me the first of what should be 20 values.

def yearlymean_gm(gm_data):
    data= np.load(gm_data)
        for i in range (0,20):
        average= data[i*12:i*12+12].sum()/12
        print average
        return average

gm_data is the name of the file.

When I simply manually enter

data= np.load(gm_data)
for i in range (0,20):
    average= data[i*12:i*12+12].sum()/12
    print average
    return average

it successfully reads out the 20 values. I'm pretty sure I just don't quite understand how for loops work in the context of functions. Any explanation (and a fix, if possible), would be awesome.

Secondly, I would love to have these values fed into an numpy array. I tried

def yearlymean_gm(gm_data):
    data= np.load(gm_data)
    average = np.zeroes(20)  
    for i in range (0,20):
        average[i]= data[i*12:i*12+12].sum()/12
        print average
        return average

but this gives me a long, wacky, list. Help on this would be cool too. Thanks!

8
  • 1
    I'm sure this is a duplicate but I can't find one. You have your return inside your for loop body so it only completes one iteration. Then the function is over so the rest of your loop is kaput. Commented Aug 17, 2016 at 17:10
  • Something's off with the indentation in your examples. Also, the second block of code contains a return but it's not in a function. Commented Aug 17, 2016 at 17:10
  • 1
    @Two-BitAlchemist I'm sure there's plenty but they never have a title that makes it easy to track down :) Indentation is invalid though, so I'm not sure we can confirm that this is the cause. Commented Aug 17, 2016 at 17:12
  • 1
    An aggregate dataset via pandas might give you the results your looking for (and wouldn't require iterative logic). I can provide additional details if you'd like - just let me know. Commented Aug 17, 2016 at 17:14
  • 1
    @Sevyns he's on the right track for what he wants to calculate... he just needs to unindent his return statement Commented Aug 17, 2016 at 17:15

2 Answers 2

5

Why not avoid the "for" loop altogether?

def yearlymean_gm(gm_data):
    data = np.load(gm_data)
    data = data.reshape((12, 20))
    print data.mean(axis=1)
    return data.mean(axis=1)
Sign up to request clarification or add additional context in comments.

Comments

1

here's what you need...

def yearlymean_gm(gm_data):
    data= np.load(gm_data)
    average = np.zeroes(20)  
    for i in range (0,20):
        average[i]= data[i*12:i*12+12].sum()/12
        print average
    return average  #don't return until the loop has completed

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.