For a deeper look into our Eikon Data API, look into:

Overview |  Quickstart |  Documentation |  Downloads |  Tutorials |  Articles

question

Upvotes
Accepted
7 2 1 3

Trying to convert Total Return in a dataframe from Object type to float64 type. Not working.

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')

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"

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

I know this question is not specific to Eikon API, but some help will be appreciated.

eikoneikon-data-apiworkspaceworkspace-data-apirefinitiv-dataplatform-eikonpython
icon clock
10 |1500

Up to 2 attachments (including images) can be used with a maximum of 5.0 MiB each and 10.0 MiB total.

1 Answer

Upvotes
Accepted
39.2k 75 11 27

It's hard to say what's going on without having a copy of the dataframe or knowing exactly how it was created. Your assumption that the total return number is too large is definitely incorrect. Numeric data is returned by Eikon Data APIs as float64. Here's an example.

>>> df, err = ek.get_data(["RIO.L","AAP.A"],["TR.TotalReturn"])
>>> type(df['Total Return'][0])
<class 'numpy.float64'>
More specifically, when get_data method constructs the dataframe and populates it with the values extracted from JSON retrieved from the Web service delivering the data, the method applies pandas to_numeric function to each column of the dataframe before returning the result. If your dataframe is constructed using get_data method there's no point in applying to_numeric function again.
What struck me in your depiction of your dataframe is that the index appears to start at 1 and not at 0. I cannot help but wonder how this happened and whether this is related to to_numeric method applied to 'Total Return' column returning "Unable to parse NaN at position 0".
icon clock
10 |1500

Up to 2 attachments (including images) can be used with a maximum of 5.0 MiB each and 10.0 MiB total.

Click below to post an Idea Post Idea