Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have python dict object with key as datetime.date object and values as tuple objects:

>>> data_dict
{datetime.date(2006, 1, 1): (5, 3),
 datetime.date(2006, 1, 2): (8, 8),
 datetime.date(2006, 1, 3): (8, 5),
 datetime.date(2006, 1, 4): (3, 3),
 datetime.date(2006, 1, 5): (3, 3),
 datetime.date(2006, 1, 6): (4, 3),
...

and I want to convert it to numpy array object in this format:

dtype([('date', '|O4'), ('high', '<i1'), ('low', '<i1')])

so that I could store it on disk and later work with it, and learn, in numpy, matplotlib...

As a matter of fact, I thought to use this format after looking at this matplotlib examples: http://matplotlib.sourceforge.net/users/recipes.html but can't find my way out how to get there.

share|improve this question
add comment

1 Answer

up vote 7 down vote accepted

The following will do it:

arr = np.array([(k,)+v for k,v in data_dict.iteritems()], \
         dtype=[('date', '|O4'), ('high', '<f8'), ('low', '<f8')])

If you then wish to use arr as a recarray, you could use:

arr = arr.view(np.recarray)

This will enable you to reference fields by name, e.g. arr.date.

share|improve this answer
    
Indeed. Thanks. Had no idea that array type can be declared like that. Cheers –  zetah Sep 19 '11 at 13:54
    
Small side note: sorted(data_dict.iteritems()) seems needed for dict –  zetah Sep 19 '11 at 14:06
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.