Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
124 views
in Technique[技术] by (71.8m points)

python - Bad datetime conversion in pandas when a csv file it's opened

I have a simple csv in which there are a Date and Activity column like this: enter image description here

and when I open it with pandas and I try to convert the Date column with pd.to_datetime its change the date. When there are a change of month like this

enter image description here

Its seems that pandas change the day by the month or something like that:

enter image description here

The format of date that I want it's dd-mm-yyyy or yyyy-mm-dd.

This it's the code that I using:

import pandas as pd
dataset = pd.read_csv(directory + "Time 2020 (Activities).csv", sep = ";")
dataset[["Date"]] = dataset[["Date"]].apply(pd.to_datetime)

How can I fix that?

question from:https://stackoverflow.com/questions/65557360/bad-datetime-conversion-in-pandas-when-a-csv-file-its-opened

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You could specify the date format in the pd.to_datetime parameters:

dataset['Date'] = pd.to_datetime(dataset['Date'], format='%Y-%m-%d')

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...