python - 将 df 保存到 excel 然后读回 df 后,Pandas 日期时间值搞砸了

标签 python python-3.x excel pandas dataframe

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/

相关文章:

excel - 来自存储在单元格中的 CSV 列表的单元格验证

Python-如何将用户输入写入 Excel 文件?

python - 我可以从 MSSql 服务器获取数据并通过一些 python 脚本将其上传到应用程序引擎吗?

python - Notify2 在 Python3 IDLE 中工作,但并不总是在终端中

django - path() 和 re_path() 有什么区别?

c# - 通过 C# 为 MS Excel 的 VSTO 加载项代码签名证书

python - 如何高效创建跨平台1000+条目数据库系统?

python - 在哪里调用 gc.collect()

python - 为什么我的代码给出了错误的变量值?

excel - 计算excel行中的连续条纹