开发者

How to reference numpy array objects?

I have numpy array:

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

where date ob开发者_运维问答ject 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?


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))])
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜