gpt4 book ai didi

python - ExcelWriter 值错误 : Excel does not support datetime with timezone when saving df to Excel

转载 作者:行者123 更新时间:2023-12-03 13:44:28 25 4
gpt4 key购买 nike

我在这个问题上运行了很长一段时间。

我将作者设置如下:

writer = pd.ExcelWriter(arquivo+'.xlsx', engine = 'xlsxwriter', options = {'remove_timezone': True})
df.to_excel(writer, header = True, index = True)

此代码在 s 函数内。问题是每次我运行代码时,它都会从数据库中获取信息,其中包含两列 datetime64[ns, UTC] 对象和时区信息。但是当保存到 Excel 的代码运行时,我收到:
ValueError: Excel does not support datetimes with timezones. Please ensure that datetimes are timezone unaware before writing to Excel.

我已经尝试了几种方法,例如 'dt.tz_convert'、replace(tzinfo=None) 和我在这里和周围找到的其他解决方案。

代码在我的个人电脑上运行没有问题,我的同事使用相同的机器规范可以运行代码。只有在我的机器上没有。我已经重新安装了python和所有包,包括格式化机器什么的,错误仍然存​​在。

xlrd v1.1.0

xlsxwriter v1.0.4

python 3.7.4

Pandas v0.25.1

如果有人能给这个问题带来一些启示,我将不胜感激。

谢谢

最佳答案

时间戳记采用什么格式?
我只是有一个类似的问题。
我试图将数据框保存到Excel。但是我得到了:
Error Code
我检查了我的日期格式,该格式为'2019-09-01T00:00:00.000Z'这是一个时间戳pandas._libs.tslibs.timestamps.Timestamp来自pandas.to_datetime其中包括一种date()方法,该方法将日期转换为excel可接受的"%Y-%m-%d"格式
所以我的代码是这样的:

#Pseudo
df['date'] = old_dates
df['date'] = df['date'].apply(lambda a: pd.to_datetime(a).date())
# .date() removes timezone

...df.to_excel etc.

关于python - ExcelWriter 值错误 : Excel does not support datetime with timezone when saving df to Excel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61802080/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com