Jasper
Jasper

Reputation: 65

Python numpy where function with datetime

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

Upvotes: 2

Views: 7314

Answers (1)

chrisaycock
chrisaycock

Reputation: 37930

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]

Upvotes: 1

Related Questions