0

I've converted from string to datetimes in columns numerous times. However in each of those instances, the string format was consistent. Now I have a dataframe with mixed formats to change. Example below, but this is throughout 100,000s of rows.

index    date    
0        30 Jan 2018
1        January 30 2018

I could convert each type on an individual basis, but is there a way to convert that df['date'] to datetime with mixed formats easily?

2 Answers 2

3

Here is a module which can do this for you dateparser

from dateparser import parse
print(parse('2018-04-18 22:33:40'))
print(parse('Wed 11 Jul 2018 23:00:00 GMT'))

Output:

datetime.datetime(2018, 4, 18, 22, 33, 40)
datetime.datetime(2018, 7, 11, 23, 0, tzinfo=<StaticTzInfo 'GMT'>)
Sign up to request clarification or add additional context in comments.

Comments

1

Here is a way to do it using datetime.strptime

from datetime import datetime

def IsNumber(s):
    try: 
        int(s)
        return True
    except ValueError:
        return False

def ConvertToDatetime(date):
    date=date.split(" ") #split by space
    if(IsNumber(date[0])): #is of the form dd month year
        if(len(date[1])==3): #if month is for form Jan,Feb...
            datetime_object = datetime.strptime(" ".join(date), '%d %b %Y')
        else: #if month is for form January ,February ...
            datetime_object = datetime.strptime(" ".join(date), '%d %B %Y')
    else:  #is of the form month date year
        if(len(date[0])==3): #if month is for form Jan,Feb...
            datetime_object = datetime.strptime(" ".join(date), '%b %d %Y')
        else: #if month is for form January ,February ...
            datetime_object = datetime.strptime(" ".join(date), '%B %d %Y')
    return datetime_object

You can add more cases based on the documentation and the format An example for the two in your question are :

ConvertToDatetime("30 Jan 2018")
2018-01-30 00:00:00
ConvertToDatetime("January 30 2018")
2018-01-30 00:00:00

1 Comment

Thanks Sruthi. Any idea if there’s a one-liner way that does this exact thing, without explicitly having to add new cases? I was hoping to avoid that, in case in the future a new case does come up, it could handle converting without getting back an error, and then adding the new case each time it may be encountered. But your answer above does get the job done.

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.