0
from numpy import genfromtxt
dataPoints = genfromtxt(temp.csv, dtype='datetime64[D],i8',delimiter=' ')

CSV File: temp.csv

2014-05-19 10
2014-05-20 11
2014-05-21 12

Output from print(dataPoints)

[ nan  10.]
[ nan  11.]
[ nan  12.]

Edit

[1969-12-31 00:00:00 1970-01-11 00:00:00]
[1969-12-31 00:00:00 1970-01-12 00:00:00]
[1969-12-31 00:00:00 1970-01-13 00:00:00]
[1969-12-31 00:00:00 1970-01-14 00:00:00]
[1969-12-31 00:00:00 1970-01-13 00:00:00]
3
  • perhaps because a date is not a number Commented May 27, 2014 at 22:02
  • @daouzli who said they all had to be numbers? Commented May 27, 2014 at 22:02
  • the dtype default value in the docs Commented May 27, 2014 at 22:04

1 Answer 1

4

Just tell NumPy what types to expect. E.g.:

>>> genfromtxt('temp.csv', dtype='datetime64[D],i8')
array([(datetime.date(2014, 5, 19), 10L),
       (datetime.date(2014, 5, 20), 11L), (datetime.date(2014, 5, 21), 12L)], 
      dtype=[('f0', '<M8[D]'), ('f1', '<i8')])
Sign up to request clarification or add additional context in comments.

5 Comments

As for "why" are you getting nan instead of the proper type... NumPy should figure out the types for each column on its own, but dates don't seem to be on their coercion list and it's probably getting confused by the fact that your string starts on a number.
after using your dtype I get the output in my edit above. This doesn't look right. Did I do something wrong?
Maybe a different version of NumPy? Anyway, do you want to operate with the dates, or just read them? If you're happy with a plain string you can just use "S10,i8" instead. No need for delimiter
Also, i8 may not be suitable for you. You may need to specify a larger integer
I would like to work with dates, so some kind of datetime would probably be ideal. How do I check the version of NumPy I'm using?

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.