2

I have dataframe which is using 2 parameters:

data_frame = pd.DataFrame(columns=l_header, data=conv)

With below content:

  column_1                    date
0      XYZ  04/11/2016 09:13:55 AM
1      QAZ  05/10/2019 11:31:05 PM
2      WSX  12/08/2019 08:00:00 AM

How can I change date column format (it's a string) from:

%m/%d/%Y %I:%M:%S %p

to:

%Y-%m-%d %H:%M:%S

2 Answers2

2

You can use the strptime and strftime methods from the built-in datetime module to use in the lambda function that will go into the brackets of the apply method:

from datetime import datetime
import pandas as pd

df = {"column_1": ["XYZ", "QAZ", "WSX"],
      "date": ["04/11/2016 09:13:55 AM", "05/10/2019 11:31:05 PM", "12/08/2019 08:00:00 AM"]}

df = pd.DataFrame(df)

format1 ="%m/%d/%Y %I:%M:%S %p"
format2 = "%Y-%m-%d %H:%M:%S"
df["date"] = df["date"].apply(lambda x: datetime.strptime(x, format1).strftime(format2))
print(df)

Output:

  column_1                 date
0      XYZ  2016-04-11 09:13:55
1      QAZ  2019-05-10 23:31:05
2      WSX  2019-12-08 08:00:00

UPDATE:

As pointed out by @YevKad in the comments, there is no need to import datetime, as pandas comes with its own datetime section:

import pandas as pd

df = {"column_1": ["XYZ", "QAZ", "WSX"],
      "date": ["04/11/2016 09:13:55 AM", "05/10/2019 11:31:05 PM", "12/08/2019 08:00:00 AM"]}

df = pd.DataFrame(df)

format1 ="%m/%d/%Y %I:%M:%S %p"
format2 = "%Y-%m-%d %H:%M:%S"

df['date'] = pd.to_datetime(df['date'], format=format1).dt.strftime(format2)

print(df)
Red
  • 26,798
  • 7
  • 36
  • 58
  • 2
    if you want to avoid using `apply` you can do this: `df['date']= pd.to_datetime(df['date'],format=format1).dt.strftime(format2)` – YevKad Jan 03 '21 at 20:05
2

I think this should be the most convenient way?

>>> df
  column_1                    date
0      XYZ  04/11/2016 09:13:55 AM
1      QAZ  05/10/2019 11:31:05 PM
2      WSX  12/08/2019 08:00:00 AM

>>> df['date']=pd.to_datetime(df['date'], infer_datetime_format='%Y-%m-%d %H:%M:%S')

  column_1                date
0      XYZ 2016-04-11 09:13:55
1      QAZ 2019-05-10 23:31:05
2      WSX 2019-12-08 08:00:00

But it's best if you do this during the loading of the .csv file

>>> data_frame = pd.DataFrame(columns=l_header, 
        parse_dates=['date'], 
        infer_datetime_format='%Y-%m-%d %H:%M:%S')
Danail Petrov
  • 1,875
  • 10
  • 12