Home > database >  String to DateTime in Python Correct Format error
String to DateTime in Python Correct Format error

Time:11-02

I know this question is out there quite a bit, but I cannot find a solution for my case. I have a DataFrame with a column Time in string format that I need to convert to datetime. Ultimately, I want the date as an int for ML purposes, but I cannot seem to get it to datetime first.

I have:

testDate = tripOrig[' Time'][0]
newDate = dt.datetime.strptime(testDate,'%d-%b-%Y %H:%M:%S.%f %Z')

Where the dates are strings like:

05-Jun-2016 00:00:00.000 EDT

For whatever reason, I keep getting the error that it is in the wrong format. I cannot for the life of me figure out what I am doing wrong. I checked the datetime docs over many times but I keep getting:

ValueError: time data '05-Jun-2016 00:00:00.000 EDT' does not match format '%d-%b-%Y %H:%M:%S.%f %Z'

What am I missing here?!?

As another note, I have also tried:

tripOrig['correct date'] = pd.to_datetime(tripOrig[' Time'])

This is very slow, throws a timezone warning, and does not account for seconds when convert to int, so I cannot use it.

How can I get this converted?

CodePudding user response:

Using a couple other questions, I found my solution:

Parser must be a string or character stream, not Series

how to convert a string datetime with unknown timezone to timestamp in python

From string to Posix/Unix int:

import datetime as dt
from time import mktime
from dateutil import parser

def timeCorrect(stringDate):
    stamp = parser.parse(stringDate, tzinfos={"EDT": -4 * 3600})
    work = mktime(stamp.timetuple())
    return work
        
tripOrig['Correct Time'] =  tripOrig[' Time'].apply(timeCorrect) 
  • Related