0

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?

5

1 Answer 1

0

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) 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.