3

I have a Dataset, with Timestamp as one of the column with the format 09/07/2016 23:58.

I'm trying to apply Naive Bayes on this Data, and i'm facing the below error. Please let me know how to use this Data in my model

ValueError: invalid literal for float(): 12/06/2016 23:59

Anagha
  • 3,073
  • 8
  • 25
  • 43

2 Answers2

2

You need to_datetime with parameter errors='coerce' for convert bad not parseable values to NaT:

df = pd.DataFrame({'date':['12/06/2016 23:59','12/06/2016 23:59', 'a']})
print (df)
               date
0  12/06/2016 23:59
1  12/06/2016 23:59
2                 a


print (pd.to_datetime(df.date, errors='coerce'))
0   2016-12-06 23:59:00
1   2016-12-06 23:59:00
2                   NaT
Name: date, dtype: datetime64[ns]

For test bad values use boolean indexing - return all rows where is NaT:

print (df[pd.to_datetime(df.date, errors='coerce').isnull()])
  date
2    a
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
  • Thank you @jezrael. Since I'm new to python, I'm having one more query. Now that I have got this column, how do i append it to my original Dataframe Or is there anyway, the existing Dataframe will be updated – Anagha Dec 15 '16 at 08:43
  • thank you for accepting! use `df.date = pd.to_datetime(df.date, errors='coerce')` or `df['date'] = pd.to_datetime(df['date'], errors='coerce')` – jezrael Dec 15 '16 at 08:44