0

I am currently using the below code to convert the format from two formats and it take lots of time, looking for some library that can identify the date without explicitly mentioning the formats. For eg I have values like " 2018-05-19 06:15:00.579", "2018-19-05 06:15:00.579", "1526723623892000000", etc in my DataFrame.

def time_formatter(val):  
    l=(len(str(val)))  
    if l==19:
        formatted_date=datetime.utcfromtimestamp(int(val)/1000000000).strftime('%Y-%m-%d %H:%M:%S.%f')
    elif l==23:
        formatted_date=datetime.strptime(val, '%Y-%m-%d %H:%M:%S.%f')
    elif l==3:
        formatted_date=np.nan
    else:
        print(val)
        raise ValueError
    return formatted_date
Yuvarajan
  • 1
  • 2
  • 1
    How exactly do expect to tell the difference between say May 6, 2021 as 2021-05-06 or 2021-06-05? No library can figure that out without additional information such as the exact format used by the entry. – Mark Tolonen Jun 27 '21 at 06:04
  • I think it's safe to have an assumptions month always comes before date, and in case month is greater than 12 I can assume that the month and date has been swapped. But could you also help me on making my code efficient? to handle both date in date & time format along with unix format. My code is really slow but let's rule out the case where month and date swaps for now. – Yuvarajan Jun 27 '21 at 06:09

0 Answers0