Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to deal with multiple date string formats in a python series

I have a csv file which I am trying to complete operations on. I have created a dataframe with one column titled "start_date" which has the date of warranty start. The problem I have encountered is that the format of the date is not consistent. I would like to know the number of days passed from today's calendar date and the date warranty started for this product.

Two examples of the entries in this start_date series:

9/11/15
9/11/15 0:00

How can I identify each of these formats and treat them accordingly?

like image 337
JL1515 Avatar asked Sep 29 '15 22:09

JL1515


1 Answers

Unfortunately you just have to try each format it might be. If you give an example format, strptime will attempt to parse it for you as discussed here.

The code will end up looking like:

import datetime    

POSSIBLE_DATE_FORMATS = ['%m/%d/%Y', '%Y/%m/%d', etc...] # all the formats the date might be in

for date_format in POSSIBLE_DATE_FORMATS :
    try:
        parsed_date = datetime.strptime(raw_string_date, date_format) # try to get the date
        break # if correct format, don't test any other formats
    except ValueError:
        pass # if incorrect format, keep trying other formats
like image 170
alksdjg Avatar answered Nov 14 '22 22:11

alksdjg