jan_21=[datetime(2021,1,1) + timedelta(hours=i) for i in range(5)]
jan_21
[datetime.datetime(2021, 1, 1, 0, 0),
datetime.datetime(2021, 1, 1, 1, 0),
datetime.datetime(2021, 1, 1, 2, 0),
datetime.datetime(2021, 1, 1, 3, 0),
datetime.datetime(2021, 1, 1, 4, 0)]
prices = np.random.randint(1,100,size=(5,))
prices
[46 23 13 26 52]
df = pd.DataFrame({'datetime':jan_21, 'price':prices})
df
datetime price
0 2021-01-01 00:00:00 83
1 2021-01-01 01:00:00 60
2 2021-01-01 02:00:00 29
3 2021-01-01 03:00:00 97
4 2021-01-01 04:00:00 67
All good so far, this is how I expected the dataframe and datetime values to be displayed. The problem comes when I save the dataframe to an excel file and then read it back into a dataframe, the datetime values get messed up.
df.to_excel('price_data.xlsx', index=False)
new_df = pd.read_excel('price_data.xlsx')
new_df
datetime price
0 2021-01-01 00:00:00.000000 83
1 2021-01-01 00:59:59.999999 60
2 2021-01-01 02:00:00.000001 29
3 2021-01-01 03:00:00.000000 97
4 2021-01-01 03:59:59.999999 67
I'd like df == new_df
to evaluate to True