Convert spanish date into a pandas datetime object

Viewed 176

I want to convert this df from an object to a datetime object, "ABR" is the Spanish for "APR"

Initial date final date
30 ABR 21 31 MAY 21
30 JUN 21 31 JUL 21

my code:

import locale
locale.setlocale(locale.LC_TIME, '')
a["Fecha Inicial"].apply(lambda x: dt.strptime(x, "%d %b %y"))

ValueError

ValueError: time data '30 ABR 21' does not match format '%d %b %y'
1 Answers

This can solve your issue

import pandas as pd
import datetime as dt

a = pd.DataFrame({"Fecha Inicial": ['30 ABR 21', '30 JUN 21']})

mapping_dict = {'ENERO':'JAN',
'FEB':'FEB',
'MARZO':'MAR',
'ABR':'APR',
'MAYO':'MAY',
'JUN':'JUN',
'JUL':'JUL',
'AGOSTO':'AUG',
'SEPT':'SEP',
'OCT':'OCT',
'NOV':'NOV',
'DIC':'DEC'
                }

a["Fecha Inicial"].apply(lambda x: dt.datetime.strptime(x.replace(x.split(' ')[1],mapping_dict[x.split(' ')[1]].capitalize()), "%d %b %y"))

locale has some issue that affects the dt.datetime.strptime.

Related