Shmelky
Shmelky

Reputation: 83

How to make a conditional slice of a numeric string and replace it with another numeric string value?

My date field CRT_DT has dates coded as follows 1190314 which represents the following date March 14th 2019, also 03/14/2019 990201 which represents the the following date February 1st 1999, also 02/01/1999

I would like to make a field that normalizes this date field to regular dates, so for the above it would have 03/14/2019 and 02/01/1999. The rule is that if it starts with a "1" then replace it with a "20" and convert it to date type, if it begins with "9" then add a "19".

df['CRT_DT_Fix'] = 
np.where(df['CRT_DT'].str.slice(stop=1)='1','20'+df['CRT_DT'].str.slice(start=2),'19'+df['CRT_DT'].str.slice(start=2))

Upvotes: 1

Views: 72

Answers (2)

Nev1111
Nev1111

Reputation: 1049

If you're still looking for a solution, this also works:

import datetime
from datetime import datetime
string='990201'
day=string[-2:]
month=string[-4:][:2]
year=int(string[:-4])+1900

new_date_str=month+day+str(year)
new_date=datetime.strptime(new_date_str, '%m%d%Y')

Upvotes: 0

Code Different
Code Different

Reputation: 93161

Use regex for the replacement:

df = pd.DataFrame({
    'CRT_DT': ['1190314', '9990201']
})
s = df['CRT_DT'].str.replace('^1', '20') \
        .str.replace('^9', '19')
df['Date'] = pd.to_datetime(s, format='%Y%m%d')

Result:

    CRT_DT       Date
0  1190314 2019-03-14
1  9990201 1999-02-01

Upvotes: 1

Related Questions