Home > OS >  How can I convert following string date-time into pandas datetime
How can I convert following string date-time into pandas datetime

Time:06-30

How can I convert the following string format of datetime into datetime object to be used in pandas Dataframe? I tried many examples, but it seems my format is different from the standard Pandas datetime object. I know this could be a repetition, but I tried solutions on the Stackexchange, but they don't work!

enter image description here

CodePudding user response:

Below code will convert it into appropriate format

df = pd.DataFrame({'datetime':['2013-11-1_00:00','2013-11-1_00:10','2013-11-1_00:20']})
df['datetime_changed'] = pd.to_datetime(df['datetime'].str.replace('_','T'))
df.head()

output:

enter image description here

CodePudding user response:

You can use pd.to_datetime with format

df['datetime'] = pd.to_datetime(df['datetime'], format='%Y-%m-%d_%H:%M')
  • Related