Home > OS >  Convert timestemp in pandas dataframe to a special format
Convert timestemp in pandas dataframe to a special format

Time:08-11

I have a pandas dataframe df_data_raw that has a column with the name "timestemp". This column has timestemp information in the format "2022-05-01 00:15:00 00:00" for every 15 minutes. I would like to convert this time information into the following format "01.05.2022 00:15". Can you tell me how to do this?

enter image description here

CodePudding user response:

Use str.strftime with the '%d.%m.%Y %H:%M' format:

df_data_raw['timestemp'] = (pd.to_datetime(df_data_raw['timestemp'])
                              .dt.strftime('%d.%m.%Y %H:%M')
                            )

CodePudding user response:

You can use

df['timestamp'] = pd.to_datetime(df['timestamp']).dt.strftime('%d.%m.%Y %H:%M')
s = '2022-05-01 00:15:00 00:00'
print(pd.to_datetime(s).strftime('%d.%m.%Y %H:%M'))

01.05.2022 00:15

CodePudding user response:

Use datetime

import datetime

timestamp = "2022-05-01 00:15:00 00:00"
dt_obj = datetime.datetime.strptime(timestamp, "%Y-%m-%d %H:%M:%S 00:00")
dt_string = dt_obj.strftime("%d.%m.%Y %H:%M:%S")
print(dt_string)

Output

01.05.2022 00:15:00

CodePudding user response:

You can use pd.to_datetime with the appropriate format argument.

pd.to_datetime(df_data_raw['timestemp'], format='%d.%m.%Y %H:%M')
  • Related