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
Jasper
  • 65
  • 2
  • 6

1 Answers1

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]
chrisaycock
  • 36,470
  • 14
  • 88
  • 125
  • 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)]) – Jasper Jun 14 '14 at 21:55
  • 1
    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]) – Jasper Jun 14 '14 at 22:02