4

I have code that I have written where I rerun the script every single day using newly updated data. Every day this script works fine and there is no problem. Today I got an error message that says:

ValueError: Cannot convert non-finite values (NA or inf) to integer

after I use the following code:

 Flash_Bookings['ContractTermMonths'] = Flash_Bookings['ContractTermMonths'].astype(int)

The column that it's referring to has only about 400 rows and I just checked that not a single one of them is an NA or inf.

What could be the problem here if I don't have inf or NA values in that column?

bernando_vialli
  • 947
  • 4
  • 12
  • 27
  • 1
    I think simplier is convert `NaN`s to some int like `Flash_Bookings['ContractTermMonths'] = Flash_Bookings['ContractTermMonths'].fillna(0).astype(int)`, because by design `NaN`s are `float`s. – jezrael Aug 27 '18 at 13:13
  • Can you tell the output of `Flash_Bookings['ContractTermMonths'].isnull().sum()` as well as `np.isinf(Flash_Bookings['ContractTermMonths']).sum()` ? – Bharath M Shetty Aug 27 '18 at 13:14
  • 1
    but the problem is I don't have NaN's there! I just checked, there are no NaN's in the column. I don't get why I am getting this error message. – bernando_vialli Aug 27 '18 at 13:14
  • What does `(~np.isfinite(Flash_Bookings['ContractTermMonths'])).sum()` return? – ayhan Aug 27 '18 at 13:14
  • 2
    Why did you mark this question as duplicate? he clearly stated that he had no NaN's – VegardKT Aug 27 '18 at 13:14
  • @Dark, it is showing me that I have 1 NAN based on the code you wrote. However, that still makes no sense to me as I am checking the excel file that I imported and there is not a single NaN in the excel flie, I don't get why it's showing a NaN now – bernando_vialli Aug 27 '18 at 13:15
  • Can you add the output of `Flash_Bookings[Flash_Bookings['ContractTermMonths'].isnull()]` – Bharath M Shetty Aug 27 '18 at 13:17
  • hmmm, the output is very bizzare. Every row in that column is either a NaT or a NaN which makes 0 sense to me because when I look in the excel file and look at that row, its populated with regular values just like every other row in the excel file, nothing stands out about it – bernando_vialli Aug 27 '18 at 13:19
  • I can avoid the error using the Flash_Bookings['ContractTermMonths'] = Flash_Bookings['ContractTermMonths'].fillna(0).astype(int) stated above but it makes no sense to me in this context since there shouldnt be any null values in the 1 row where it is showing null values – bernando_vialli Aug 27 '18 at 13:23
  • ok I figured out the problem, it was the row after all the data that something the new person saving this excel file did different that it included rows that already don't have any data in the dataframe, so I just deleted the row in the excel with that was blank at the end and it fixed it – bernando_vialli Aug 27 '18 at 13:28

0 Answers0