-1

I am trying to convert a column of dates in the format 4-Mar-20 into a integer or numeric value in order to fit as vector in machine learning model.

I keep getting error message:

ValueError: time data 'Date' does not match format '%d/%m/%Y' (match)

Nubok
  • 3,502
  • 7
  • 27
  • 47

2 Answers2

1

You should use the format like %d-%b-%y

from datetime import datetime

date1 = datetime.strptime('4-Mar-20', '%d-%b-%y')

print(date1) #2020-02-04 00:00:00
  • Sorry if this may be a stupid question but how would I apply that to an entire 'Date' Column – Munir Yousef Mar 08 '20 at 16:01
  • @MunirYousef Refer this [answer](https://stackoverflow.com/q/17134716/3091398). This `df['Dates'] = pd.to_datetime(df['Dates'])` or `df['Dates'] = df['Dates'].map(lambda x: datetime.strptime(x, '%d-%b-%y'))` will do. – CodeIt Mar 08 '20 at 17:33
0

Pandas library has inbuilt datetime convertor which does a very good job Here is a code snippet:

Import pandas as pd

pd.to_datetime("4-Mar-20")

This will convert without any issues

AmitJ
  • 11
  • 1