Python coding for date conversion

Viewed 42

My requirement is, I have a csv file which has multiple date columns. I am reading this CSV data file from AWS S3 bucket and need to load to Postgres DB table. Before loading to DB, i need to convert all date columns which are in Oracle Date format(DD-MON-YY) to Postgres date format(YYYY-MM-DD timestamp).

Need to do with python. Can anyone help me on this?

1 Answers

If you convert date column in dataframe to datetime format, it will automatically format date(YYYY-MM-DD):

import pandas as pd
df = pd.read_csv("date.csv")
df['date'] = pd.to_datetime(df['date'])
print(df)

Input data:

        date
0  16-Jan-21
1  17-Feb-21
2  18-Mar-21
3  19-Apr-21
4  20-May-21
5  21-Jun-21
6  22-Jul-21
7  23-Aug-21
8  24-Sep-21

Output:

        date
0 2021-01-16
1 2021-02-17
2 2021-03-18
3 2021-04-19
4 2021-05-20
5 2021-06-21
6 2021-07-22
7 2021-08-23
8 2021-09-24
Related