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
到目前为止一切都很好,这就是我期望显示数据框和日期时间值的方式。当我将数据框保存到 excel 文件然后将其读回数据框时,问题就出现了,日期时间值被弄乱了。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
我想要 df == new_df
评估为 True
最佳答案
在问题的可能原因的背景下(请参阅 sophros 的回答),您可以做的 - 表面上 - 规避问题是转换 df["datetime"]
的单元格在生成 excel 文件之前转换为字符串,然后在 new_df
之后再次将字符串转换为日期时间已经被创造了:
df["datetime"] = df["datetime"].dt.strftime("%m/%d/%Y, %H:%M:%S")
df.to_excel('price_data.xlsx', index=False)
new_df = pd.read_excel('price_data.xlsx')
new_df["datetime"] = pd.to_datetime(new_df["datetime"], format="%m/%d/%Y, %H:%M:%S")
关于python - 将 df 保存到 excel 然后读回 df 后,Pandas 日期时间值搞砸了,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68081938/