0

I have a data frame with a lot of timedata looking like this:

01-08-2018 14:02:03

I have converted it to datetime using this code:

df["Date"]=pd.to_datetime((df["Date"]))

and it gives me this: 2018-01-08 14:02:03

At first glance it looks fine, but it has mistaken days for months. So my question is how do i tell python that days is days and month is months?

I hope some of you can help me

Rahul Agarwal
  • 4,034
  • 7
  • 27
  • 51

2 Answers2

1

Use dayfirst=True

Ex:

import pandas as pd
print(pd.to_datetime("01-08-2018 14:02:03", dayfirst=True))

Output:

2018-08-01 14:02:03
Rakesh
  • 81,458
  • 17
  • 76
  • 113
0

Use this:

import pandas as pd

df["Date"]=pd.to_datetime(df["Date"], format='%d-%m-%Y %H:%M:%S')#you will specify in the format key what each number means so it doesn't misunderstands it
Ines
  • 141
  • 1
  • 6