Home > OS >  Pandas is confusing the date format
Pandas is confusing the date format

Time:04-02

Something of context: Using the format YY/mm/dd HH-MM-SS, today is 2022-04-01 15:00:33 (Fake hour) I'm exporting a pandas DataFrame to CSV in the next line

df.to_csv('filepath/archive.csv', header=False, index=False, date_format='%Y-%m-%d %H:%M:%S', mode='a')

But I have as result this:

"0.85","0.7","0.5","0.7","0.65","0.65","0.8","0.8","0.7","0.7055555555555555","2022-01-04 15:00:33"

Look how the hour confuses the day and the month and I have in my code "Year-Month-Day" and it presents as final result "Year-Day-Month" like this:

"0.85","0.7","0.5","0.7","0.65","0.65","0.8","0.8","0.7","0.7055555555555555","2022-04-01 15:00:33"

Is this a bug of pandas? What can I do to fix it?

CodePudding user response:

This works for me:

import pandas as pd
import datetime
dt = datetime.datetime.strptime("2022-04-01 15:00:33", '%Y-%m-%d %H:%M:%S')
print(dt)
df = pd.DataFrame([[0.85,0.7,0.5,0.7,0.65,0.65,0.8,0.8,0.7,0.7055555555555555,dt]])
print(df)
df.to_csv('archive.csv', header=False, index=False, date_format='%Y-%m-%d %H:%M:%S', mode='a')

Output is:

2022-04-01 15:00:33
     0    1    2    3     4     5    6    7    8         9                   10
0  0.85  0.7  0.5  0.7  0.65  0.65  0.8  0.8  0.7  0.705556 2022-04-01 15:00:33

... and archive.csv contains the following line:

0.85,0.7,0.5,0.7,0.65,0.65,0.8,0.8,0.7,0.7055555555555555,2022-04-01 15:00:33
  • Related