## ValueError: cannot convert float NaN to integer

Sometimes in your data analytics project, you will be working with float data types and integers, but the value NaN may also appear, which will give you headaches if you don’t know how to fix the problem at hand.

A NaN is defined as “Not a Number” and represents missing values in the data. If you are familiar with SQL, it is a similar concept to NULLS.

## So how does this error occur in Python?

Let’s look at some logic below:

``````NaN =float('NaN')
print(type(NaN))
print(NaN)

Result:
<class 'float'>
nan``````

As can be seen, we have a variable called ‘NaN’, and it is a data type ‘Float’

One of the characteristics of NaN is that it is a special floating-point value and cannot be converted to any other type than float; thus, when you look at the example below, it shows us this exactly and why you would get the error message we are trying to solve.

``````NaN =float('NaN')
print(type(NaN))
print(NaN)

a= int(NaN)

print(a)

Result:

Traceback (most recent call last):
File "ValueError_cannot_convert_float_NaN_to_integer.py", line 5, in <module>
a= int(NaN)
ValueError: cannot convert float NaN to integer

``````

In the variable ‘a’ we are trying to make that an integer number from the NaN variable, which, as we know, is a floating-point value and cannot be converted to any other type than float.

## How do we fix this problem?

The easiest way to fix this is to change the ‘NaN’ actual value to an integer as per the below:

``````NaN =float(1)
print(type(NaN))
print(NaN)

a= int(NaN)

print(a)
print(type(a))

Result:
<class 'float'>
1.0
1
<class 'int'>``````

So, in summary, if you come across this error:

1. Check to see if you have any ‘Nan’ values in your data.
2. If you do replace them with an integer value, or a value that you need for your project, that should solve your problem.

## TypeError: ‘NoneType’ object is not iterable

Have you wondered why you cant iterate over a NoneType but never had it properly explained? Read on as here we will fully explain the concept and how you can fix it.

## So what is a NoneType anyway?

First of all, to understand a none type, you need to understand what none is.

None is essentially no value, so in other words, nothing is present. It differs from the integer zero, as the integer zero is an actual value and it exists.

If you were to put both of them side by side on a database table, one would be empty the other would have a value of zero.

Also, None is a type in itself:

``````a = None
print(a)
print(type(a))

Gives the output:

None
<class 'NoneType'>``````

Whereas zero is an integer datatype:

``````a = 0
print(a)
print(type(a))

Giving output:
0
<class 'int'>``````

## So how does a none type object is not iterable occur?

Let’s take an example and walk through it. In the below code we have a variable that is empty. In other words, we are asking the program to loop through something that does not exist and does not have any values.

``````a = None
print(a)
print(type(a))

for i in a:
print(i)

Giving output:
for i in a:
TypeError: 'NoneType' object is not iterable
None
<class 'NoneType'>``````

Repeating what we said above, it is not possible to loop through something that does not have a value.

But if we change it to a string value of ‘1’, the loop will work.

``````a = '1'
print(type(a))

for i in a:
print(i)
``````

So in summary:

(A) NoneTypes are useful if you just want to return an empty value or check for an empty value.

(B) You cannot loop over NoneTypes as they do not have any values, they are empty!

## how do I declare a null value in python?

In writing about this question, it is important to understand a few things , before we step into the code.

Null values appear in almost every programming language, but they can have a different meaning.

A null value basically means it is empty. So when you return a value that is null, it has nothing in it.

Sometimes it can be confused with the value zero, but as zero is an actual integer, it is not an empty value in a field.

Python uses None to define the keyword null, that returns an empty value.

How can we show null in an output?

Lets look at the below output. From observation it can be seen that a,b,d retuns an int value, but that is quite straight forward.

Let’s focus on c. When it is printed out the value on the screen is showing nothing, and its data type is str. But why is that, surely it is None or empty as we were expecting?

Well Python will return as a string , unless it is explicitly declared as empty. The next section will take you through that.

```a = 1
b = 1
c = ""
d = a-b
print(a)
print(b)
print(c)
print(d)
print(type(a))
print(type(b))
print(type(c))
print(type(d))

Returns:
1
1

0
<class 'int'>
<class 'int'>
<class 'str'>
<class 'int'>
```

So based on the above, how do I declare a null value in python?

We have modified the code above, and declared c as None, and in this instance the code now recognises the output as empty.

```a = 1
b = 1
c = None
d = a-b
print(a)
print(b)
print(c)
print(d)
print(type(a))
print(type(b))
print(type(c))
print(type(d))

Result:
1
1
None
0
<class 'int'>
<class 'int'>
<class 'NoneType'>
<class 'int'>```

What are the other scenarios that None will be returned?

Python also returns values on None, even though they have not been explicitly declared.

In the below if you try to print nothing, it will by default return an empty value.

On the other hand using an if statement you can check if a value is a None.

The final example is where you are using a function, to check if a value is in a list. If the value does not appear in the list it returns nothing.

This is especially handy , if you want to completely sure that that the returned values are nothing.

It gives you a level of comfort that that the code will not pass anything to other parts of the programme.

```a = print()
print(a)

variable = None

if variable is None:
print("Correct")
else:
print("Incorrect")

variable1 = "today"
if variable1 is None:
print("Correct")
else:
print("Incorrect")

def returnnone():
list = [1,2,3,4,5]
for i in list:
if i == 6:
print("Six found")
else:
print(None)

returnnone()

Result:
None
Correct
Incorrect
None
None
None
None
None

```