valueerror: cannot convert float nan to integer – Code Example

Total
0
Shares

In this article we will look at Python code to resolve valueerror: cannot convert float nan to integer. Before Pandas v1.0.0, conversion of NaN to integer resulted in error. According to Pandas documentation, this limit was removed.

Why error occurred?

NaN is represented as float in Python. Pandas use to fill missing data in array, dataframe, series etc. with NaN. When we try to convert values of such array to int, this error occurred. But limitation is removed in v1.0.0.

Also, previously numpy.nan was used as missing value but now pandas.NA is used. Let’s see an example –

arr = pd.array([1, 2, None], dtype=pd.Int64Dtype())

# Output
# [1, 2, <NA>]
# Length: 3, dtype: Int64

These are all converted to pandas.NA

  • None
  • np.nan
  • pd.NA
pd.array([1, 2, np.nan, None, pd.NA], dtype="Int64")

# Output
# [1, 2, <NA>, <NA>, <NA>]
# Length: 5, dtype: Int64

Solution with Code Example

1. To identify null values use isNa()

array = np.array([[1, np.nan, 3], [4, 5, np.nan]])
pd.isna(array)

Output –

array([[False,  True, False],
       [False, False,  True]])

2. Convert all non-numeric values to NaN –

s = pd.Series(['1.0', '2', -3, 'a'])
s = pd.to_numeric(s, errors='coerce')

using errors='coerce' will convert all invalid parsing to NaN (source). Output will be –

0    1.0
1    2.0
2   -3.0
3    NaN
dtype: float64

3. Remove all NA values using dropna

s = pd.Series(['1.0', '2', -3, 'a'])
s = pd.to_numeric(s, errors='coerce')
s = s.dropna()

Output –

0    1.0
1    2.0
2   -3.0

Using these 3 steps, your data will be cleaned from NA and is ready to get converted to Int. Now you won’t get valueerror: cannot convert float nan to integer.

Live Demo

Open Live Demo