Python Pandas : pandas.to_datetime() is switching day & month when day is less than 13

前端 未结 3 1813
别那么骄傲
别那么骄傲 2020-12-20 16:39

I wrote a code that reads multiple files, however on some of my files datetime swaps day & month whenever the day is less than 13, and any day that is from day 13 or abo

3条回答
  •  感动是毒
    2020-12-20 17:21

    I ran into the same issue. In my case the dates were the index column (called "Date"). The above mentioned solution using to_datetime() directly on the dataframe with index column "Date" didn't work for me. I had to use read_csv() first without setting the index to "Date", then apply to_datetime() on it and only then set the index to "Date".

    df= pd.read_csv(file, parse_dates=True)
    df.Date = pd.to_datetime(df.Date, dayfirst=True)
    df = df.set_index('Date')
    

提交回复
热议问题