Search code examples
pythonpython-2.7pandastimestampnaivebayes

How to use Timestamp Data in Building Naive Bayes model in Python


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


Solution

  • 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