2

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'
  • @shaikmoeed OP mentioned `"ABR" is the Spanish for "APR"` – Ch3steR Dec 16 '21 at 06:35
  • 2
    Does this answer your question? [Parse date and change format from spanish](https://stackoverflow.com/questions/42760474/parse-date-and-change-format-from-spanish) – bradley101 Dec 16 '21 at 06:37

1 Answers1

0

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.

Tamil Selvan
  • 1,600
  • 1
  • 9
  • 25