处理 pandas.datetime 类型时消息 "Exception ignored"
Message "Exception ignored" when dealing pandas.datetime type
我有一个 xlsx 文件,其中有一列包含格式为“01.01.1900 09:01:25”的日期。该文件受密码保护,因此我通过 win32com.client 库将其转换为数据框。
代码如下:
import pandas as pd
import win32com.client
xlApp = win32com.client.Dispatch("Excel.Application")
xlApp.DisplayAlerts = False
xlwb = xlApp.Workbooks.Open(File, False, True, None, " ") #Open Workbook password " "
xlws = xlwb.Sheets("Sheet 1") #Open Sheet 1
#Get table dimensions
LastRow = xlws.Range("A1").CurrentRegion.Rows.Count
LastColumn = xlws.Range("A1").CurrentRegion.Columns.Count
header=list((xlws.Range(xlws.Cells(1, 1), xlws.Cells(1, LastColumn)).Value)[0])
content = list(xlws.Range(xlws.Cells(2, 1), xlws.Cells(LastRow, LastColumn)).Value)
#Get the dataframe
df=pd.DataFrame(data=content, columns=header)
print (df)
我检查过一旦导入的 dtype 已自动并正确地分配给该列的 datetime64。问题是,每当我尝试对该列的任何值执行任何操作(只需打印或比较它)时,我都会收到一条消息:
File "pandas\_libs\tslibs\timezones.pyx", line 227, in pandas._libs.tslibs.timezones.get_dst_info
AttributeError: 'NoneType' object has no attribute 'total_seconds'
Exception ignored in: 'pandas._libs.tslib._localize_tso'
Traceback (most recent call last):
File "pandas\_libs\tslibs\timezones.pyx", line 227, in pandas._libs.tslibs.timezones.get_dst_info
AttributeError: 'NoneType' object has no attribute 'total_seconds'
Traceback (most recent call last):
虽然代码运行完美,但警告消息让我很烦。
我可以对数据类型做些什么来避免该警告吗?
这样打开excel,content
变量就是一个元组列表
查看这些元组,有一个 TimeZoneInfo 将所有日期本地化在一种时区中,在我的例子中 "GMT Standard Time"。
因此,一旦转换为数据帧,在执行 df.dtypes
时,结果不仅是 "datetime64",而且是 "datetime64 (UTC+0:00) Dublin, Edimburg, ..."
此时区设置仅在通过 win32com.client
打开 excel 文件时发生。如果您删除了密码,您可以使用 pandas.read_excel
打开它并发现没有为这些日期时间设置时区并且不会出现提到的警告。
不知道它发生的确切原因,但我有一个针对原始示例的解决方案。将 tz 数据库识别的时区设置为 "UTC"
或简单地 None
时,警告消失了。类似于:
df["col_name"]=df["col_name"].dt.tz_convert(None)
我有一个 xlsx 文件,其中有一列包含格式为“01.01.1900 09:01:25”的日期。该文件受密码保护,因此我通过 win32com.client 库将其转换为数据框。
代码如下:
import pandas as pd
import win32com.client
xlApp = win32com.client.Dispatch("Excel.Application")
xlApp.DisplayAlerts = False
xlwb = xlApp.Workbooks.Open(File, False, True, None, " ") #Open Workbook password " "
xlws = xlwb.Sheets("Sheet 1") #Open Sheet 1
#Get table dimensions
LastRow = xlws.Range("A1").CurrentRegion.Rows.Count
LastColumn = xlws.Range("A1").CurrentRegion.Columns.Count
header=list((xlws.Range(xlws.Cells(1, 1), xlws.Cells(1, LastColumn)).Value)[0])
content = list(xlws.Range(xlws.Cells(2, 1), xlws.Cells(LastRow, LastColumn)).Value)
#Get the dataframe
df=pd.DataFrame(data=content, columns=header)
print (df)
我检查过一旦导入的 dtype 已自动并正确地分配给该列的 datetime64。问题是,每当我尝试对该列的任何值执行任何操作(只需打印或比较它)时,我都会收到一条消息:
File "pandas\_libs\tslibs\timezones.pyx", line 227, in pandas._libs.tslibs.timezones.get_dst_info
AttributeError: 'NoneType' object has no attribute 'total_seconds'
Exception ignored in: 'pandas._libs.tslib._localize_tso'
Traceback (most recent call last):
File "pandas\_libs\tslibs\timezones.pyx", line 227, in pandas._libs.tslibs.timezones.get_dst_info
AttributeError: 'NoneType' object has no attribute 'total_seconds'
Traceback (most recent call last):
虽然代码运行完美,但警告消息让我很烦。
我可以对数据类型做些什么来避免该警告吗?
这样打开excel,content
变量就是一个元组列表
查看这些元组,有一个 TimeZoneInfo 将所有日期本地化在一种时区中,在我的例子中 "GMT Standard Time"。
因此,一旦转换为数据帧,在执行 df.dtypes
时,结果不仅是 "datetime64",而且是 "datetime64 (UTC+0:00) Dublin, Edimburg, ..."
此时区设置仅在通过 win32com.client
打开 excel 文件时发生。如果您删除了密码,您可以使用 pandas.read_excel
打开它并发现没有为这些日期时间设置时区并且不会出现提到的警告。
不知道它发生的确切原因,但我有一个针对原始示例的解决方案。将 tz 数据库识别的时区设置为 "UTC"
或简单地 None
时,警告消失了。类似于:
df["col_name"]=df["col_name"].dt.tz_convert(None)