1

I have column date values as below in the dataframe

Jan 2009
Feb 2003
2017-09-01 00:00:00

but when I use

np.where(df['Date'].astype(str).apply(len) == 8,pd.to_datetime(df['Date'],format="%b %Y"),pd.to_datetime(df['Date']))

it fails with the below error:-

ValueError: time data datetime.datetime(2017, 9, 1, 0, 0) does not match format '%b %Y' (match)

Any ideas?

1 Answer 1

1

I think there are some datetimes in format YYYYMMDD, so length is 8 so not matched %b %Y:

print (df)
                  Date
0             20170901
1             Jan 2009
2             Feb 2003
3  2017-09-01 00:00:00

For me working only to_datetime:

df['Date'] = pd.to_datetime(df['Date'])
print (df)
        Date
0 2017-09-01
1 2009-01-01
2 2003-02-01
3 2017-09-01
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.