Back to Community
Convert Float to NaN Error

Anyone have a quick solution for this:

ValueError: cannot convert float NaN to integer

1 response

The error "cannot convert float NaN to integer" is raised by various numpy and pandas methods when trying to cast a NaN value to an integer. NaN can only be represented with a float.

Here are some things to look for (without seeing the code). Since the culprit seems to be a NaN, ask yourself where NaNs may originate in the code. The most common place is in data. Any pipeline output could potentially contain some NaNs. If that output is used in another method, eg order_optimal_portfolio. it may try to convert a NaN to an integer and, voila, one gets that error.

So, a good practice is to clean up ones data immediately after fetching it. Both the data.history and pipeline_output methods return pandas dataframes. Apply either dropna ( https://pandas.pydata.org/pandas-docs/version/0.18/generated/pandas.DataFrame.dropna.html) or fillna (https://pandas.pydata.org/pandas-docs/version/0.18/generated/pandas.DataFrame.fillna.html) . Something like this:

# pipeline_output returns the constructed dataframe.  
context.output = pipeline_output('my_pipe')  
context.output.dropna(inplace=True)

This may delete securities one wants to leave so ultimately a more 'surgical' approach may be the way to go. However, if the above removes the error, it at least highlights the problem is with NaN values in the data. Ultimately either drop the securities with NaN values in their factors or change the factor data to a non-NaN value.

If you could post your code others may be able to help troubleshoot.

Hope that helps.

Disclaimer

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by Quantopian. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. No information contained herein should be regarded as a suggestion to engage in or refrain from any investment-related course of action as none of Quantopian nor any of its affiliates is undertaking to provide investment advice, act as an adviser to any plan or entity subject to the Employee Retirement Income Security Act of 1974, as amended, individual retirement account or individual retirement annuity, or give advice in a fiduciary capacity with respect to the materials presented herein. If you are an individual retirement or other investor, contact your financial advisor or other fiduciary unrelated to Quantopian about whether any given investment idea, strategy, product or service described herein may be appropriate for your circumstances. All investments involve risk, including loss of principal. Quantopian makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances.