256 Kilobytes

Answers in Scripting and Automation | By Some Guy

Published

8,402 views, 0 RAMs, and 4 comments

Tags: Python, Scripting, Errors, Programming, ValueError

Profile Photo - Some GuySome GuyBasic Subscriber

I need help with a Python error message I keep getting. Does anyone have experience with the specific error message "ValueError: cannot convert float nan to integer" and know how to fix it?

Download more RAM. 🐏 ⨉ 0Posted by Some Guy 2 years ago 🕓 Posted at 13 January, 2019 06:04 AM PST

This is a debug account.

Profile Photo - Hash BrownHash BrownInternet ActivistEnglandStaff

Hey,

So NaN values (not a number) are normally used when values are likely to be so large they don't fit into a standard double.

for example if we did this, shit would break because x would be too big to be contained by a double.

x = 10e200

y = x * x

This would leave x to be equal to 10e4000 which just fucks a lot of shit up.

If you tried to print y you could see "#inf" which stands for infinite number.

To convert your NaN to a interger the easiest way is with NumPy

And doing something like this will fix things for you:

import numpy

if numpy.isnan(x):
    y = numpy.nan_to_num(x)

Hope this helps

Users Who Have Downloaded More RAM:
August R. Garcia (1 year ago)
🐏 ⨉ 1
Posted by Hash Brown 2 years ago 🕓 Posted at 20 February, 2019 04:20 AM PST

"THAT DOG IS GETTING RAPED" - Terry A. Davis

Profile Photo - August R. GarciaAugust R. GarciaLARPing as a Sysadmi...Portland, ORSite Owner

If there are some scenarios where the value is actually not intended to be a number, this can also be solved with a try-catch block:

try: 
    # Do a thing that will raise a value error if x is NaN
    my_function(x)
except ValueError as e:
    # Do something else, or use "pass" to do nothing
    pass

Download more RAM. 🐏 ⨉ 0Posted by August R. Garcia 1 year ago 🕓 Posted at 03 December, 2019 13:33 PM PST

Sir, I can do you a nice SEO.

Profile Photo - Defaulthaviernick

You can avoid this with a mask method. Note first that in python NaN is defined as the number which is not equal to itself:

>float('nan') == float('nan')       False

It might be worth avoiding use of np.NaN altogether. NaN literally means "not a number", and it cannot be converted to an integer.  In general, Python prefers raising an exception to returning NaN, so things like sqrt(-1) and log(0.0) will generally raise instead of returning NaN. However, you may get this value back from some other library.  From v0.24, you actually can. Pandas introduces Nullable Integer Data Types which allows integers to coexist with NaNs.  Also, even at the lastest versions of pandas if the column is object type you would have to convert into float first, something like:

df['column_name'].astype(np.float).astype("Int32")

NB: You have to go through numpy float first and then to nullable Int32, for some reason.

Download more RAM. 🐏 ⨉ 0Posted by haviernick 1 month ago 🕓 Posted at 14 March, 2021 22:58 PM PDT
Profile Photo - Defaulthaviernick

You can avoid this with a mask method. Note first that in python NaN is defined as the number which is not equal to itself:

>float('nan') == float('nan')       False

It might be worth avoiding use of np.NaN altogether. NaN literally means "not a number", and it cannot be converted to an integer.  In general, Python prefers raising an exception to returning NaN, so things like sqrt(-1) and log(0.0) will generally raise instead of returning NaN. However, you may get this value back from some other library.  From v0.24, you actually can. Pandas introduces Nullable Integer Data Types which allows integers to coexist with NaNs.  Also, even at the lastest versions of pandas if the column is object type you would have to convert into float first, something like:

df['column_name'].astype(np.float).astype("Int32")

NB: You have to go through numpy float first and then to nullable Int32, for some reason.

Download more RAM. 🐏 ⨉ 0Posted by haviernick 1 month ago 🕓 Posted at 14 March, 2021 22:58 PM PDT

Post a New Comment

Do you like having a good time?

Register an Account

You can also login to an existing account or reset your password. All use of this site is subject to the terms of service and privacy policy.

Read Quality Articles

Read some quality articles. If you can manage to not get banned for like five minutes, you can even post your own articles.

View Articles →

Argue with People on the Internet

Use your account to explain why people are wrong on the Internet forum.

View Forum →

Vandalize the Wiki

Or don't. I'm not your dad.

View Wiki →

Ask and/or Answer Questions

If someone asks a terrible question, post a LMGTFY link.

View Answers →

Make Some Money

Hire freelancers and/or advertise your goods and/or services. Hire people directly. We're not a middleman or your dad. Manage your own business transactions.

Register an Account

Answers— Read More

Find more related content below!