I am trying to convert an object type column to float. Not working

117 Views Asked by At
TSRdfr["Return"] = pd.to_numeric(TSRdfr.Return, errors='coerce')

This is not converting the data type of Return from Object to float64. I tried removing errors ='coerce' to see what's happening.

I am getting an error saying:

Unable to parse NaN at position 0

when I dont use errors = 'coerce'.

The Return numbers are accessed from Refinitiv Eikon API. I am assuming they are too large to convert to float64. Any suggestions??

2

There are 2 best solutions below

3
bubble On

Try the following:

import pandas as pd
import re
repat = re.compile('\d+(?:\.\d+)?')

# sample data frame 
df = pd.DataFrame({'Return': ['some data 123.123', 'tick 0.12']})

df.loc[:, 'Converted'] = df.Return.apply(lambda x: float(repat.findall(x)[0]))

Hope that helps

2
Kenan On

If your number is too large you can use the decimal library

import decimal
decimal.getcontext().prec = 100
df[col] = df[col].map(lambda x: decimal.Decimal(x))