Skip to content Skip to sidebar Skip to footer

Pandas Reading Dates From Csv In Yy-mm-dd Format

I have a csv files with dates in the format displayed as dd-mmm-yy and i want to read in the format yyyy-mm-dd. parse dates option works but it not converting dates correct before

Solution 1:

from dateutil.relativedeltaimport relativedelta
import datetime

let's assume you have a csv like this:

mydates
18-Aug-68
13-Jul-45
12-Sep-00
20-Jun-10
15-Jul-60

Define your date format

d = lambda x: pd.datetime.strptime(x, '%d-%b-%y')

Put a constraint on them

dateparse = lambda x: d(x) ifd(x) < datetime.datetime.now() elsed(x) - relativedelta(years=100) 

read your csv:

df = pd.read_csv("myfile.csv", parse_dates=['mydates'], date_parser=dateparse)

here is your result:

printdfmydates01968-08-1811945-07-1322000-09-1232010-06-2041960-07-15

Voilà

Post a Comment for "Pandas Reading Dates From Csv In Yy-mm-dd Format"