0
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??

GileBrt
  • 1,830
  • 3
  • 20
  • 28
user3021495
  • 97
  • 10
  • Could you provide more info about `TSRdfr`? What is content of it? – bubble Apr 05 '19 at 01:44
  • @bubble I have a dataframe TSRdfr which I have accessed from Eikon API on Python. Index Instrument Total Return 1. RIO.L. 15.990065 2. AAP.A. 22.543209 and so on .... – user3021495 Apr 05 '19 at 02:49
  • Total Return is of the type Object – user3021495 Apr 05 '19 at 02:49
  • I have a dataframe TSRdfr which I have accessed from Eikon API on Python. Index Instrument Total Return 1. RIO.L. 15.990065 2. AAP.A. 22.543209 and so on .... The data type of Total Return is Object which I need to convert to float64. I am using the syntax below. TSRdfr["Total Return"] = pd.to_numeric(TSRdfr["Total Return"], errors='coerce') – user3021495 Apr 05 '19 at 02:50

2 Answers2

0

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

bubble
  • 1,634
  • 12
  • 17
  • import re repat = re.compile('\d+(?:\.\d+)?') TSR_df_06_09.loc[:, 'Converted'] = TSR_df_06_09.Return.apply(lambda x: float(repat.findall(x)[0])) – user3021495 Apr 05 '19 at 07:10
  • AttributeError: 'DataFrame' object has no attribute 'Return' – user3021495 Apr 05 '19 at 07:10
  • This means that `TSR_df_06_09` hasn't column named `Return`. What are column names of `TSR_df_06_09`; print `TSR_df_06_09.columns` to see all columns of the data frame. – bubble Apr 07 '19 at 23:42
0

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))
Kenan
  • 13,156
  • 8
  • 43
  • 50