syntax.us Let the syntax do the talking
Blog Contact Posts Questions Tags Hire Me

Question:
In Python NumPy, how do I load a CSV file?

I just encountered a use-case asking me to load some Forex data into a Python NumPy Array.

The Forex data resides in a CSV file which looks like this:
dan@hp ~/cjb4/fx $
dan@hp ~/cjb4/fx $
dan@hp ~/cjb4/fx $ head ibf5min.csv
aud_usd,2009-05-01 00:00:00,0.729
aud_usd,2009-05-01 00:05:00,0.7288
aud_usd,2009-05-01 00:10:00,0.729
aud_usd,2009-05-01 00:15:00,0.7287
aud_usd,2009-05-01 00:20:00,0.7287
aud_usd,2009-05-01 00:25:00,0.729
aud_usd,2009-05-01 00:30:00,0.729
aud_usd,2009-05-01 00:35:00,0.7294
aud_usd,2009-05-01 00:40:00,0.7293
aud_usd,2009-05-01 00:45:00,0.7289
dan@hp ~/cjb4/fx $
dan@hp ~/cjb4/fx $
dan@hp ~/cjb4/fx $ tail ibf5min.csv
usd_jpy,2014-11-28 21:15:00,118.7132
usd_jpy,2014-11-28 21:20:00,118.7136
usd_jpy,2014-11-28 21:25:00,118.7057
usd_jpy,2014-11-28 21:30:00,118.6778
usd_jpy,2014-11-28 21:35:00,118.6547
usd_jpy,2014-11-28 21:40:00,118.6669
usd_jpy,2014-11-28 21:45:00,118.67
usd_jpy,2014-11-28 21:50:00,118.6069
usd_jpy,2014-11-28 21:55:00,118.6076
usd_jpy,2014-11-28 22:00:00,118.6338
dan@hp ~/cjb4/fx $
dan@hp ~/cjb4/fx $
dan@hp ~/cjb4/fx $
dan@hp ~/cjb4/fx $
dan@hp ~/cjb4/fx $ du -sh ibf5min.csv
81M	ibf5min.csv
dan@hp ~/cjb4/fx $
dan@hp ~/cjb4/fx $ wc -l ibf5min.csv
2401224 ibf5min.csv
dan@hp ~/cjb4/fx $
The syntax I used to load the data into a Python NumPy Array is displayed below:
# numpy_loadtxt.py
# Fill ibf5min from ibf5min.csv
import numpy as np
mydtype = [('pair','S7'),('ydate','S19'),('cp','f8')]
ibf5min = np.loadtxt('ibf5min.csv',dtype=mydtype,delimiter=',')
print(ibf5min[0])
print(ibf5min[-1])
Here is a screendump from my run of the script:
dan@hp ~/cjb4/fx $
dan@hp ~/cjb4/fx $ python numpy_loadtxt.py
('aud_usd', '2009-05-01 00:00:00', 0.729)
('usd_jpy', '2014-11-28 22:00:00', 118.6338)
dan@hp ~/cjb4/fx $
dan@hp ~/cjb4/fx $
I should note that the api documentation for NumPy loadtxt() is here:

http://docs.scipy.org/doc/numpy/reference/generated/numpy.loadtxt.html


syntax.us Let the syntax do the talking
Blog Contact Posts Questions Tags Hire Me