0

I have my dataset as below:

Sl.No   Date1
1   08-09-1990
2   01-06-1988
3   04-10-1989
4   15-11-1991
5   01-06-1968

When I tried to load the data:

df = pd.read_csv("file",parse_dates=True, dayfirst=True)

I am getting the output as:

0   08-09-90
1   01-06-88
2   04-10-89
3   15-11-91
4   01-06-68

Problem is:

  1. The date format is dd-mm-YY instead of dd-mm-YYYY
  2. As a result when I try to convert datetime format the year 1968 is taken as 2068 (eg. Index 4 in output/sl.no 5 in input)

also as per suggested link [how to specify the datetime format in read_csv

i tried it was the same issue as before

and i also tried with [time data does not match format

df=pd.read_csv("file",infer_datetime_format=True) df[Date1]=pd.to_datetime(df['Date1'], format='%d-%m-%Y')

am facing ValueError '08-09-90' does not match format '%d-%m-%Y'

  • 1
    Possible duplicate of [how to specify the datetime format in read\_csv](https://stackoverflow.com/questions/28862956/how-to-specify-the-datetime-format-in-read-csv) – Chris Apr 22 '19 at 20:17
  • I think this thread already has the answer to your question: https://stackoverflow.com/questions/23797491/parse-dates-in-pandas – Andy Apr 22 '19 at 20:23
  • Thanks!!! I tried the suggested answer even then the problem is same, The format of the year after loaded is in dd-mm-YY only. – Karthikeyan Apr 22 '19 at 20:40

1 Answers1

0

Give this a try - It seems to work for me

import pandas as pd

filepath = '' # insert your files path here (I created a csv with columns 'SI_No' and 'Date' to test this and then copied your data)

df = pd.read_csv(filepath, parse_dates=['Date'])

df = df.set_index('SI_No')

df

                Date
SI_No
1     1990-08-09
2     1988-01-06
3     1989-04-10
4     1991-11-15
5     1968-01-06
Ando21
  • 26
  • 1
  • 4