0

I have a date column that has the following format:

date
10jan2018
12feb2018
14mar2018

And so on. I guess the best approach is to turn the middle string into a number, and then apply todatetime() so I created a dictionary like this:

dict_month={'jan':01, 'feb':02,'mar':03,'apr':04,'may':05,'jun':06'
      'jul':07, 'aug':08,'sep':09,'oct':10,'nov':11,'dec':12}

But I'm not sure how to proceed. Maybe I could use a regex, but I'm not sure how to combine regex and dictionary.

Any ideas?

1 Answer 1

3

Specify a format to pd.to_datetime(). %b is the month as locale’s abbreviated name (and is case-insensitive in this direction):

>>> df
        date
0  10jan2018
1  12feb2018
2  14mar2018

>>> pd.to_datetime(df['date'], format='%d%b%Y')
0   2018-01-10
1   2018-02-12
2   2018-03-14
Name: date, dtype: datetime64[ns]

From datetime module: Formatting directives.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.