0
$\begingroup$

I have an atypical time format that I need to convert into a datetime index for time series analysis. I'm working in Python / Pandas.

The column is 'BC_DT', and the format is "27-MAR-18". Example is below.

BC_DT
27-MAR-18
28-MAR-18
29-MAR-18

I tried this method, but I'm getting an error: ValueError: time data '27-MAR-18' does not match format '%d-%b-%Y'

df['Converted_Date'] = df['BC_DT'].apply(lambda x: dt.datetime.strptime(x, '%d-%b-%Y'))
$\endgroup$
3
$\begingroup$

Let pandas determine what datetime format you are using automatically.

import pandas as pd
raw_data = pd.DataFrame(data={'BC_DT':['27-MAR-18','28-MAR-18','29-MAR-18']})
raw_data['BC_DT'] =  pd.to_datetime(raw_data['BC_DT'])
print(raw_data)

BC_DT
0: 2018-03-27
1: 2018-03-28
2: 2018-03-29

| improve this answer | |
$\endgroup$
  • 1
    $\begingroup$ I didn't realize Pandas could do the conversion automatically. Thanks! $\endgroup$ – ForsakenPlague Apr 9 '18 at 11:54
0
$\begingroup$
df['Converted_Date'] = df['BC_DT'].apply(lambda x: dt.datetime.strptime(x, '%d-%b-%y'))

you should use %y instead of %Y. As it is for YYYY format not YY year foramt

| improve this answer | |
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.