close

[Solved] Data type conversion error: ValueError: Cannot convert non-finite values (NA or inf) to integer

Hello Guys, How are you all? Hope You all Are Fine. Today I get the following error Data type conversion error: ValueError: Cannot convert non-finite values (NA or inf) to integer in python. So Here I am Explain to you all the possible solutions here.

Without wasting your time, Let’s start This Article to Solve This Error.

How Data type conversion error: ValueError: Cannot convert non-finite values (NA or inf) to integer Error Occurs?

Today I get the following error Data type conversion error: ValueError: Cannot convert non-finite values (NA or inf) to integer in python.

How To Solve Data type conversion error: ValueError: Cannot convert non-finite values (NA or inf) to integer Error ?

  1. How To Solve Data type conversion error: ValueError: Cannot convert non-finite values (NA or inf) to integer Error ?

    To Solve Data type conversion error: ValueError: Cannot convert non-finite values (NA or inf) to integer Error If your DF is big, you're probably not seeing the missing numbers. But you can use the fillna function to help

  2. Data type conversion error: ValueError: Cannot convert non-finite values (NA or inf) to integer

    To Solve Data type conversion error: ValueError: Cannot convert non-finite values (NA or inf) to integer Error If your DF is big, you're probably not seeing the missing numbers. But you can use the fillna function to help

Solution 1

If your DF is big, you’re probably not seeing the missing numbers. But you can use the fillna function to help

>>> df = pd.DataFrame(data=data, columns=['id', 'birth_year'])
>>> df
   id  birth_year
0   1      1989.0
1   2      1990.0
2   3         NaN
>>> df.birth_year
0    1989.0
1    1990.0
2       NaN
Name: birth_year, dtype: float64
>>> df.birth_year.astype(int)
ERROR   |2018.01.29T18:14:04|default:183: Unhandled Terminal Exception
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/devtools/uat/anaconda4321/lib/python3.6/site-
packages/pandas/util/_decorators.py", line 91, in wrapper
    return func(*args, **kwargs)
  File "/usr/local/devtools/uat/anaconda4321/lib/python3.6/site-
packages/pandas/core/generic.py", line 3410, in astype
    **kwargs)
  File "/usr/local/devtools/uat/anaconda4321/lib/python3.6/site-
packages/pandas/core/internals.py", line 3224, in astype
    return self.apply('astype', dtype=dtype, **kwargs)
  File "/usr/local/devtools/uat/anaconda4321/lib/python3.6/site-
packages/pandas/core/internals.py", line 3091, in apply
    applied = getattr(b, f)(**kwargs)
  File "/usr/local/devtools/uat/anaconda4321/lib/python3.6/site-
packages/pandas/core/internals.py", line 471, in astype
    **kwargs)
  File "/usr/local/devtools/uat/anaconda4321/lib/python3.6/site-
packages/pandas/core/internals.py", line 521, in _astype
    values = astype_nansafe(values.ravel(), dtype, copy=True)
  File "/usr/local/devtools/uat/anaconda4321/lib/python3.6/site-
packages/pandas/core/dtypes/cast.py", line 620, in astype_nansafe
    raise ValueError('Cannot convert non-finite values (NA or inf) to '
ValueError: Cannot convert non-finite values (NA or inf) to integer

>>> df = df.fillna(0)
>>> df.birth_year.astype(int)
0    1989
1    1990
2       0
Name: birth_year, dtype: int64

Summery

It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you? Thank You.

Also, Read