0

I dont understand why my column allHoldingsFund['ratioBest'] is not converting into numeric?

Initially i replaced empty rows with the number 0 but int 0 and floats numbers did not make the column allHoldingsFund['ratioBest'] easily convertible to numeric (got error TypeError: len() of unsized object). Now I left empty rows as they are and forced the floats to convert to numeric but it did not work- allHoldingsFund['ratioBest'] is is still an object. See below

allHoldingsFund['ratioBest']

Out[170]: 
100                          
144                          
187         0.520704845814978
264                          
386                          
494                          
597                          
635         2.790492957746479

allHoldingsFund['ratioBest']=pd.to_numeric(allHoldingsFund['ratioBest'], errors='coerce')

Name: ratioBest, Length: 1664, dtype: object

Any help is appreciated

Thanks

SBad
  • 1,245
  • 5
  • 23
  • 36

1 Answers1

1

I get your problem - values are not scalar, but numpy.ndarrays:

allHoldingsFund = pd.DataFrame({'ratioBest':[np.array(4.12), '']})
print (allHoldingsFund)
        ratioBest
0      4.12
1    

Solution is convert to list and then to 1d array:

arr = np.array(allHoldingsFund['ratioBest'].tolist())
allHoldingsFund['ratioBest'] = (pd.to_numeric(arr, errors='coerce'))
print (allHoldingsFund)
   ratioBest
0       4.12
1        NaN

print (allHoldingsFund['ratioBest'].dtype)
float64
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252