11

I have pandas column like following

   January 2014
   February 2014

I want to convert it to following format 201401 201402 I am doing following

   df.date = pd.to_datetime(df.date,format= '%Y%B') 

But,it gives me an error.

3
  • 4
    df['date'] = pd.to_datetime(df['date']) should just work Commented Mar 9, 2017 at 10:53
  • It does not work. Giving me an error. Commented Mar 9, 2017 at 10:55
  • Post raw data, code to read and create your df, what you tried and all errors in your question so this doesn't become a guessing game Commented Mar 9, 2017 at 10:55

1 Answer 1

15

You shouldn't need the format string, it just works:

In [207]:
pd.to_datetime('January 2014')

Out[207]:
Timestamp('2014-01-01 00:00:00')

besides your format string is incorrect, it should be '%B %Y':

In [209]:
pd.to_datetime('January 2014', format='%B %Y')

Out[209]:
Timestamp('2014-01-01 00:00:00')
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.