Skip to content

Instantly share code, notes, and snippets.

@aydinemre
Created December 11, 2018 11:21
Show Gist options
  • Save aydinemre/a03e9433ccb063a673c9ed285a7b7315 to your computer and use it in GitHub Desktop.
Save aydinemre/a03e9433ccb063a673c9ed285a7b7315 to your computer and use it in GitHub Desktop.
If pandas data frame has multiple format time
import pandas as pd
df = pd.DataFrame({"purchase_time": ["19-09-1995 12:14:00.000","12/09/2018 15:34"]})
df['Timestamp'] = pd.to_datetime(df.purchase_time, format='%d/%m/%Y %H:%M', errors='coerce')
mask = df.Timestamp.isnull()
df.loc[mask, 'Timestamp'] = pd.to_datetime(df[mask]['purchase_time'], format='%d-%m-%Y %H:%M:%S.%f', errors='coerce')
print(df)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment