Home > Enterprise >  Pandas Timestamp reformatting
Pandas Timestamp reformatting

Time:11-09

I have a pandas Timestamp column that looks like this:

2021.11.04_23.03.33

How do I convert this in a single liner to be able to look like this:

2021-11-04 23:03:33

CodePudding user response:

use a regular expression by looking for the hour and minute second pattern (\d{4})-\d{2}-(\d{2})\s (\d{2})_(\d{2}).(\d{2}).(\d{2}) and use re.findall then get each group part then reassemble the datetime stringthen convert to a datetime

CodePudding user response:

This one does the trick:

df['Time'] = pd.to_datetime(df2['Timestamp'], format = '%Y.%m.%d_%H.%M.%S')
  • Related