Separating date and year, converting to datetime and creating a new column

Question:

Convert the date column to a datetime type.
Create a new column, issue_year, and set it to the year from date column.

I need to separate a date in the format ‘Dec-11’ into month and year. Dec being the month and 11 being the year.

I need to also create a new column for the year.

All data needs to be converted to datetime type.

I have tried:

loan_df.issue_d = pd.to_datetime(loan_df.issue_d, errors='coerce',format='%Y%m')

I am getting an NaT error when I run loan_df.issue_d.head()

But obviously I am missing something. Any suggestions all?

    print(loan_df['issue_d'].head())
    0    Dec-11
    1    Dec-11
    2    Dec-11
    3    Dec-11
    4    Dec-11
    Name: issue_d, dtype: object
Asked By: ric_m

||

Answers:

You can try format="%b-%y":

df['new_issue_d'] = pd.to_datetime(df['issue_d'], format='%b-%y')
print(df)

Prints:

  issue_d new_issue_d
0  Dec-11  2011-12-01
1  Jan-11  2011-01-01
2  Feb-13  2013-02-01

  • %b (ex. Sep) – Month as locale‚Äôs abbreviated name.
  • %y (ex. 11) – Year without century as a zero-padded decimal number.
Answered By: Andrej Kesely

If you want keep the issue_date format:

loan_df['issue_year'] = pd.to_datetime(loan_df['issue_date'], format='%b-%y').dt.year
print(loan_df)

  issue_date  issue_year
0     Dec-11        2011
1     Dec-11        2011
2     Dec-11        2011
3     Dec-11        2011

If you want to format issue_date :

loan_df['issue_date'] = pd.to_datetime(loan_df['issue_date'], format='%b-%y')
loan_df['issue_year'] = loan_df['issue_date'].dt.year
print(loan_df)

  issue_date  issue_year
0 2011-12-01        2011
1 2011-12-01        2011
2 2011-12-01        2011
3 2011-12-01        2011
Answered By: Jamiu S.
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.