Reputation: 45
I have a dataframe with column named PERIODCODE as
PERIODCODE
20211401
20211402
20211403
I have used below code to separate out Year, Date and Month information and create new fields -
import datetime
df["date"] = pd.datetime(df["PERIODCODE"])
df['year'] = df['date'].dt.year
df['month'] = df['date'].dt.month
df['day'] = df['date'].dt.day
Results I am getting with this are incorrect. Below Output I am seeking
PERIODCODE| Year | Month |Key|
20211401 |2021|Jan|Jan-21|
20211402 |2021|Feb|Feb-21|
20211403 |2021|Mar|Mar-21|
Upvotes: 0
Views: 19
Reputation: 863531
Add format
parameter to to_datetime
here YYYYDDMM
and for custom strings use Series.dt.strftime
:
df["date"] = pd.to_datetime(df["PERIODCODE"], format='%Y%d%m')
df['year'] = df['date'].dt.year
df['month'] = df['date'].dt.strftime('%b')
df['key'] = df['date'].dt.strftime('%b-%y')
print (df)
PERIODCODE date year month key
0 20211401 2021-01-14 2021 Jan Jan-21
1 20211402 2021-02-14 2021 Feb Feb-21
2 20211403 2021-03-14 2021 Mar Mar-21
Upvotes: 1