2

I have an array of daily date times over a period of 30 years. I am trying to filter this array by month and day using np.where so that I can find the average of the corresponding data entries for each day of the year.

However, I get the error:

AttributeError: 'list' object has no attribute 'month'

I can query the data correctly when I do:

print "dates[100].month: ", dates[100].month

Is there any way I can use the numpy where function to query datetime entries?

The code I have so far is:

clim = np.zeros((12,31))

m_arr = range(1,13)  # months array
d_arr = range(1,32)  # days array

for i in range(len(m_arr)):

for j in range(len(d_arr)):

    # finding indexes of the dates array

    tt, = np.where((dates.month == i) & (dates.day == j))
    data2 = np.zeros(len(tt))
    dates2 = np.zeros(len(tt))

    if len(tt) > 0:

    for num in range(len(tt)):

                site = tt[num]
            dates2[num] = dates[site]
        data2[num] = data[site]

        clim[i,j]=data2[~np.isnan(data2)].mean()      # some nan entries in data

1 Answer 1

1

Since dates is a list, it cannot be used as a datetime, even if its elements are datetime type. So instead of

dates.month == i

you need something like

[d.month == i for d in months]
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for looking at this. I tried: tt, = np.where([d.month == i+1 for d in dates]), and this works fine for the months, but I can't seem to get it to filter both months and days, e.g. I tried:tt, = np.where([(d.month == i+1 for d in dates) & (d.day == j+1 for d in dates)])
Ah, problem solved, thanks for putting me on the right track with it! This works: tt, = np.where([(d.month == i+1) & (d.day == j+1) for d in dates])

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.