I founds this error in multiple question but it is not fixed or explained
Ex. Reading in single str column with loadtxt
I have a problem with the following line (import numpy as np)
symbols = np.loadtxt('filename.csv',
dtype={'names': ('ticker', 'year', 'month','day'),
'formats': ('S10','i4','i2','i2')},
delimiter=',',skiprows=1, usecols=(0,))
with following data
ticker year month day
GPD.TO 2010 8 16
HAO.V 2010 8 16
RDS.V 2010 8 16
MD.V 2010 8 16
It gives me a
IndexError: list index out of range
When I run that line without usecols it works fine and returns all 4 columns fine.
I looked at many other questions on google and Stackoverflow but this isnt answered
If you want to use
usecols = (0,)withnp.loadtxtthen you must change thedtypeto match:with
filename.csv:yields
Alternatively, with
np.genfromtxtyou do not need to changedtype:works as well.