3

I have numpy array:

>>> data
    dtype([('date', '|O4'), ('value', '<f8')]

where date object is Python datetime.date object which consists of all days in one year: [2010-1-1, ..., 2010-12-31] and value object is value data for corresponding date.

How can I return value data only for, let's say, September?

1
  • Is the array in order by date? Commented Sep 20, 2011 at 0:17

1 Answer 1

4

You could use a boolean array to index data:

import numpy as np
import datetime as dt
dates=[dt.date(2010,1,1)+dt.timedelta(days=i) for i in range(365)]
values=range(365)
data=np.array(zip(dates,values),dtype=[('dates','object'),('value','<f8')])

(data['dates']>=dt.date(2010,9,1)) & (data['dates']<dt.date(2010,10,1)) is a boolean array of the same length as data, which is True for all dates in September:

print(data['value'][(data['dates']>=dt.date(2010,9,1)) &
           (data['dates']<dt.date(2010,10,1))])
Sign up to request clarification or add additional context in comments.

1 Comment

Excellent. I was hoping for something like that

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.