简体   繁体   English

ExcelWriter ValueError: Excel 将 df 保存到 Excel 时不支持带时区的日期时间

[英]ExcelWriter ValueError: Excel does not support datetime with timezone when saving df to Excel

I'm running on this issue for quite a while now.我在这个问题上已经有一段时间了。

I set the writer as follows:我将作者设置如下:

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

This code is inside s function. The problem is every time I run the code, it gets information from the database, which contains two columns datetime64[ns, UTC] object with time zone info.此代码位于 s function 内。问题是每次我运行代码时,它都会从数据库中获取信息,其中包含两列 datetime64[ns, UTC] object 和时区信息。 But when the code to save to Excel runs I receive:但是当保存到 Excel 的代码运行时,我收到:

ValueError: Excel does not support datetimes with timezones. Please ensure that datetimes are timezone unaware before writing to Excel.

I have already tried several things like 'dt.tz_convert', replace(tzinfo=None) and other solutions I have found here and around.我已经尝试过几种方法,例如“dt.tz_convert”、replace(tzinfo=None) 以及我在这里和周围找到的其他解决方案。

The code runs without problem in my personal computer, my colleague at work with the same machine specs can run the code.代码在我的个人电脑上运行没有问题,我的同事使用相同的机器规格可以运行代码。 Only in my machine it doesn't.只有在我的机器上它没有。 I already reinstalled python and all the packages, including formatting the machine and nothing, the error persists.我已经重新安装了 python 和所有软件包,包括格式化机器什么都没有,错误仍然存在。

xlrd v1.1.0 xlrd v1.1.0

xlsxwriter v1.0.4 xlsxwriter v1.0.4

python 3.7.4 python 3.7.4

pandas v0.25.1 pandas v0.25.1

If someone could bring some light into this issue I would much appreciate it.如果有人能对这个问题有所了解,我将不胜感激。

Thanks谢谢

What format is your timestamps in?你的时间戳是什么格式的?

I just had a similar problem.我只是有一个类似的问题。

I was trying to save a data frame to Excel.我试图将数据框保存到 Excel。 However I was getting:但是我得到了:

错误代码

I checked my date format which was in this format '2019-09-01T00:00:00.000Z'我检查了我的日期格式,格式为'2019-09-01T00:00:00.000Z'

This is a timestamp pandas._libs.tslibs.timestamps.Timestamp from pandas.to_datetime这是来自pandas.to_datetime的时间戳pandas._libs.tslibs.timestamps.Timestamp

which includes a method date() that converted the date into a format "%Y-%m-%d" that was acceptable by excel其中包括一个方法date()将日期转换为 excel 可接受的格式"%Y-%m-%d"

So my code was something like:所以我的代码是这样的:

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

...df.to_excel etc.

This should do the job, remove timezone from columns before exporting to excel (using tz_localize(None)).这应该可以完成工作,在导出到 excel 之前从列中删除时区(使用 tz_localize(None))。

# Check which columns have timezones datetime64[ns, UTC] 
df.dtypes

# Remove timezone from columns
df['date'] = df['date'].dt.tz_localize(None)

# Export to excel
df.to_excel('filename.xlsx')

I found this way easier and more dynamic.我发现这种方式更容易,更有活力。 This solution you select the columns by the type and applied the desire conversion.此解决方案您 select 列按类型并应用了所需的转换。

date_columns = df.select_dtypes(include=['datetime64[ns, UTC]']).columns
for date_column in date_columns:
    df[date_column] = df[date_column].dt.date
    
df.to_excel('anbima_feed.xlsx',engine='xlsxwriter')

The Accepted Answer works only if you need the Date without the Time in the corresponding Timezone.仅当您需要相应时区中没有时间的日期时,接受的答案才有效。 If your Time is in UTC as a Epoch you need to convert it to Striftime and then again to Datetime to conserve the Time in the Timezone.如果您的时间以 UTC 为纪元,您需要将其转换为 Striftime,然后再转换为 Datetime 以保存时区中的时间。

Reference: https://python-forum.io/thread-31300.html参考: https://python-forum.io/thread-31300.html

Example: the field ts is a timestamp in UTC as Epoch in millisecond.示例:字段 ts 是 UTC 中的时间戳,以 Epoch 为单位,以毫秒为单位。

df['ts']
OUT:
0      1619801902867
1      1619765681594
2      1619712291984
3      1619680298648
4      1619629032109
5      1619593388626
6      1619531314509
7      1619509338368
8      1619449287828
9      1619433411243
10     1619103667781
11     1619078244871
12     1619021782951
13     1618990214111
14     1618931135540
15     1618903774632

Then you need to convert it into the desired Timezone:然后您需要将其转换为所需的时区:

df['ts'] = pd.to_datetime(df['ts'],unit='ms').dt.tz_localize('utc').dt.tz_convert('Europe/Vatican')
df['ts'] = df['ts'].apply(lambda a: datetime.datetime.strftime(a,"%Y-%m-%d %H:%M:%S"))
df['ts'] = pd.to_datetime(df['ts'])

The Result will look like:结果将如下所示:

df['ts']
OUT:
0     2021-04-30 18:58:22
1     2021-04-30 08:54:41
2     2021-04-29 18:04:51
3     2021-04-29 09:11:38
4     2021-04-28 18:57:12
5     2021-04-28 09:03:08
6     2021-04-27 15:48:34
7     2021-04-27 09:42:18
8     2021-04-26 17:01:27
9     2021-04-26 12:36:51
10    2021-04-22 17:01:07
11    2021-04-22 09:57:24
12    2021-04-21 18:16:22
13    2021-04-21 09:30:14
14    2021-04-20 17:05:35
15    2021-04-20 09:29:34

After this, the xlsxwriter will accept it and write the excel without the error Message.在此之后,xlsxwriter 将接受它并写入 excel 而不会出现错误消息。

There is also another way to use UTC parameter in PandasPandas中还有另一种使用UTC参数的方法

import pandas as pd
# Adjust time zone from columns
df['date'] = pd.to_datetime( df['date'], errors='coerce',utc=True)
# Export to excel
df.to_excel('filename.xlsx')

I encountered the same issue and did some documentation search and found a solution for pandas我遇到了同样的问题,做了一些文档搜索,找到了 pandas 的解决方案

The below change (added- options={'remove_timezone': True} ) worked for me.以下更改( options={'remove_timezone': True} )对我有用。

exwriter = pd.ExcelWriter(fullpath, engine='xlsxwriter', options={'remove_timezone': True})

If you are oke with the values in the sheet being a string, you can use the following code to transform the datetime如果您对工作表中的值是字符串感到满意,则可以使用以下代码来转换日期时间

date_columns = df.select_dtypes(include=['datetime64[ns, UTC]']).columns
for date_column in date_columns:
    df[date_column] = df[date_column].apply(str)

I had a similar problem.我有一个类似的问题。 In my case the dates were the index.就我而言,日期是索引。 In case anyone else encounters that very problem (oftentimes with stock/currency/crypto price data) you can use the following:如果其他人遇到这个问题(通常是股票/货币/加密货币价格数据),您可以使用以下内容:

df.index = df.index.tz_localize(None)
df.to_excel(path)

Simply, convert the column to str :只需将列转换为str

df['date'] = df['date'].astype(str)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM