Convert DD-MMM-YY to YYYY/MM/DD in Pandas

Gautam Mishra

DoB column in my dataset has date in the format below:

0       12-Jan-79
1       13-Jan-70
2       11-Mar-84
3       11-Mar-84
4       01-May-86
          ...    
1080    15-Mar-81
1081    07-Jul-96
1082    11-Apr-90
1083    22-Oct-64
1084    12-Jul-95

I need to convert the same into standard date format YYYY/MM/DD.

I tried df['DateOfBirth'] = pd.to_datetime(df['DateOfBirth']). This is working fine for most of the cases.

But in some instances like 22-Oct-64, I can observe the data is converted into the future dates for example 2064-10-22.

I want to try something like DoB.str[:-2]+'19'+DoB.str[-2:] on entire column.

How to fix the problem?

Gautam Mishra

The objective can be achieved by cut the two characters,add 19 and then paste the last two characters.

df['DateOfBirth']

Out[14]:
0       12-Jan-79
1       13-Jan-70
2       11-Mar-84
3       11-Mar-84
4       01-May-86
          ...    
1080    15-Mar-81
1081    07-Jul-96
1082    11-Apr-90
1083    22-Oct-64
1084    12-Jul-95
Name: DateOfBirth, Length: 1085, dtype: object

Code: df['DateOfBirth'] = df['DateOfBirth'].str[:-2]+'19'+df['DateOfBirth'].str[-2:]

Output:

Out[16]:
0       12-Jan-1979
1       13-Jan-1970
2       11-Mar-1984
3       11-Mar-1984
4       01-May-1986
           ...     
1080    15-Mar-1981
1081    07-Jul-1996
1082    11-Apr-1990
1083    22-Oct-1964
1084    12-Jul-1995
Name: DateOfBirth, Length: 1085, dtype: object

Simply use df['DateOfBirth'] = pd.to_datetime(df['DateOfBirth']) to convert the columnn to datetime format.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

puzzled with how to convert ddmmyy string to dd-MMM-yy date

Convert date from DD-MON-YY to YYYYMMDD

java.time.format.DateTimeParseException for dd-MMM-yy format

Reformatting a Date from DD-MMM-YYYY to YYYYDDMM or YYYYMMDD

oracle convert DD-MON-YY to DD/MM/YYYY

pandas - converting d-mmm-yy to datetime object

Convert dd, mm, yy arrays to numpy datetime

Openquery convert yyyymmdd to MM/DD/YYYY

Convert MMM-YY to date where date before 1968

Convert MMM YY to Date time in python

Convert dd/mmm/yyyy to yyyy-mm-dd

Convert mm-dd-yy, hr-mm-ss to yy-mm-dd timestamp in pyspark

Converting string format dd - mmm - yy to date

convert yyyymmdd into yyyy/mm/dd in sybase

How to convert date dd/mm/YY to mm-dd-YY format in PHP

How to convert date format from dd-MMM-yyyy to yyyymmdd in Javascript?

convert epoch time into dd/mm/yy in a file

Convert a character in the form of DD-MMM-YY to DD/MM/YY in R

Convert MMM-YY string into Sortable Date Format

Regular expression convert Date format from dd-mon-yy to dd/MM/yy

PHP convert date to DD-M-YY

GoogleFinace convert dd/mm/yy to yy,mm,dd format

Convert YYYY-mm-dd to yyyymmdd in MYSQL

convert dd MMM yyyy to unix time

How to convert date format "dd/mm/yy" to "yy/mm/dd" for mysql database inserting?

Convert UTC format to dd/mm/yy hh:mm:ss format in pandas?

How to convert datetime to mmm-yy format in R

Convert Timestamp string (dd MMM yyyy HH:mm:ss) to Date (DD/MM/YY) in Google Sheets

Reformatting Text from "yyyymmdd" or "yyyyddmm" format to "dd MMM yyyy" format in SQL Server