I have a column that I want to convert from a string to a date time timestamp. Each row in the respective column contains data as a string in this format: "01.01.2020 00:00 - 01.01.2020 00:15". I want to convert it to a date time object "2020-01-01 00:00:00 00:00"
I just need the first part of the date and the excess is unnecessary. I know I could split it in multiple columns on space as a delimiter and use:
pd.to_datetime(df['Date/time before conversion'].format='%d.%m.%Y %H:%M')
But is there a more efficient manner where I can directly assign on the string which bit is year, month etc. while skipping the fluff I don't need?
CodePudding user response:
Use:
pd.to_datetime(df['Date/time before conversion'].str.split().str[0], dayfirst=True)
CodePudding user response:
IIUC, you could extract the first part and convert to datetime:
pd.to_datetime(df['Date/time before conversion'].str.extract('(\S )', expand=False))