Reputation: 45
I am trying to read a column of float into a list for each of a set of CSV files, then appending to a two dimensional list, later converting that to a 2D array, but the array doesn't convert into a two dimensional array of float (paraphrased below). Where am I going wrong?
import numpy
symbols = ['CVX', 'COP', 'MMM', 'BP', 'HMC', 'TM']
AA_lst = []
nSyms = len(symbols)
shortest = 99999
for sym in symbols
fn = "PricesOf_" + sym + ".csv"
col = getCSVcolumn( fn, "Close" )
print( "type(col)=" + str(type(col)) ) # --> <class 'list'>
print( "type(col[0])=" + str(type(col[0])) ) # --> <class 'float'>
shortest = min(shortest,len(col))
AA_lst.append(col) # appended as a row of AA_lst
AA = numpy.array( AA_lst )
print( "type=(AA)=" + str(type(AA)) ) # --> <class 'numpy.ndarray'>
print( "type=(AA[0]=" + str(type(AA[0])) ) # --> <class 'list'>
#print( "type=(AA[0,0]=" + str(type(AA[0,0])) ) # --> Error, too many indices
# fix up dimensions (so rows are all the same length)
AA = numpy.resize( AA, (nSyms, shortest) )
print( "type=(AA)=" + str(type(AA)) ) # --> <class 'numpy.ndarray'>
print( "type=(AA[0]=" + str(type(AA[0])) ) # --> <class 'numpy.ndarray'>
print( "type=(AA[0,0]=" + str(type(AA[0,0])) ) # --> <class 'list'>
# desire something of the form: array([[1,2,3] [4,5,6] [7,8,9]])
# i.e. expecting type(AA[0,0] to be <class 'float'>, not <class 'list'>
Upvotes: 0
Views: 408
Reputation: 45
Problem solved by replacing numpy.resize(...) with:
AAA_lst = []
for row in AA_lst:
AAA_lst.append( row[:shortestUnshifted] )
and feeding AAA_lst to numpy.array()
Upvotes: 1