Converting different date time formats to MM/DD/YYYY format in pandas dataframe

后端 未结 3 1720
长情又很酷
长情又很酷 2020-12-11 09:54

I have a date column in a pandas.DataFrame in various date time formats and stored as list object, like the following:

            date
1    [May 23rd, 2011]         


        
相关标签:
3条回答
  • 2020-12-11 10:04

    Provided code will work for following scenarios.

    • Change date format from M/D/YY to MM/DD/YY (5/2/2009 to 05/02/2009)
    • change form ANY FORMAT to MM/DD/YY

    import pandas as pd

    '''
           * checking provided input file date format correct or not
           * if format is correct change date format from M/D/YY to MM/DD/YY
           * else date format is not correct in input file
             Date format  change form ANY FORMAT to MM/DD/YY
      '''
    input_file_name = 'C:/Users/Admin/Desktop/SarenderReddy/predictions.csv'
    dest_file_name = 'C:/Users/Admin/Desktop/SarenderReddy/Enrich.csv'
    #input_file_name = 'C:/Users/Admin/Desktop/SarenderReddy/enrichment.csv'
    read_data = pd.read_csv(input_file_name)
    print(pd.to_datetime(read_data['Date'], format='%m/%d/%Y', errors='coerce').notnull().all())
    
    if pd.to_datetime(read_data['Date'], format='%m/%d/%Y', errors='coerce').notnull().all():
        print("Provided correct input date format in input file....!")
        read_data['Date'] = pd.to_datetime(read_data['Date'],format='%m/%d/%Y')
        read_data['Date'] = read_data['Date'].dt.strftime('%m/%d/%Y')
        read_data.to_csv(dest_file_name,index=False)
        print(read_data['Date'])
    else:
        print("NOT... Provided correct input date format in input file....!")
        data_format = pd.read_csv(input_file_name,parse_dates=['Date'], dayfirst=True)
        #print(df['Date'])
        data_format['Date'] = pd.to_datetime(data_format['Date'],format='%m/%d/%Y')
        data_format['Date'] = data_format['Date'].dt.strftime('%m/%d/%Y')
        data_format.to_csv(dest_file_name,index=False)
        print(data_format['Date'])
    
    0 讨论(0)
  • 2020-12-11 10:10

    It would be better if you use this it'll give you the date format in MM-DD-YYYY the you can apply strftime:

    df['Date_ColumnName'] = pd.to_datetime(df['Date_ColumnName'], dayfirst = False, yearfirst = False)
    
    0 讨论(0)
  • 2020-12-11 10:23

    Given your sample data, with the addition of a NaT, this works:

    Code:

    df.date.apply(lambda x: pd.to_datetime(x).strftime('%m/%d/%Y')[0])
    

    Test Code:

    import pandas as pd
    
    df = pd.DataFrame([
        [['']],
        [['May 23rd, 2011']],
        [['January 1st, 2010']],
        [['Apr. 15, 2008']],
        [['07-11-2013']],
        [['9/01/1995']],
        [['04/15/2000']],
        [['11/22/68']],
        [['12/1997']],
        [['08/2002']],
        [['2014']],
        [['2016']],
    ], columns=['date'])
    
    df['clean_date'] = df.date.apply(
        lambda x: pd.to_datetime(x).strftime('%m/%d/%Y')[0])
    
    print(df)
    

    Results:

                       date  clean_date
    0                    []         NaT
    1      [May 23rd, 2011]  05/23/2011
    2   [January 1st, 2010]  01/01/2010
    3       [Apr. 15, 2008]  04/15/2008
    4          [07-11-2013]  07/11/2013
    5           [9/01/1995]  09/01/1995
    6          [04/15/2000]  04/15/2000
    7            [11/22/68]  11/22/1968
    8             [12/1997]  12/01/1997
    9             [08/2002]  08/01/2002
    10               [2014]  01/01/2014
    11               [2016]  01/01/2016
    
    0 讨论(0)
提交回复
热议问题