I have monthly mean data for 25 years, and I want to take the mean of each month over all the years, is my for loop correct?

Viewed 67

I have 1D monthly data ranging from 1992 - 2016. I want to take say, the means of all the January values from 1992 - 2016 in order to isolate the seasonal variation. Is my for loop doing the right thing?

test_mean = []

for months in range(lsds_detrend_mnths.size):
    mean = np.mean(lsds_detrend_mnths[months::months+1])
    test_mean.append(mean)

Am I using the array slicing properly?

Or in order to take the average of every month for all years, do I do this for loop?

1 Answers

If you data starts on January you should use this to get mean for January:

mean = np.mean(lsds_detrend_mnths[::12])

This will be mean for January.And to get mean for other months you can use this loop:

for i in range(12):
    test_mean.append(np.mean(lsds_detrend_mnths[i::12]))

Just remember what slice notation means:

a[start:stop:step] # start through not past stop, by step

Related