Python编程:将SAS数据读入python时出错

时间:2018-09-11 18:56:48

标签: python pandas sas

当我尝试使用pandas在python中读取sas7bdat文件时。它显示流出错误,如下所示。有谁知道如何解决这一问题?非常感谢。

import pandas as pd
pd.read_sas('D:/My Documents/data.sas7bdat')

错误消息:

OverflowError                            
Traceback (most recent call last)
pandas\_libs\tslib.pyx in pandas._libs.tslib.array_with_unit_to_datetime()   
pandas\_libs\tslibs\timedeltas.pyx in pandas._libs.tslibs.timedeltas.cast_from_unit()

OverflowError: int too big to convert

在处理上述异常期间,发生了另一个异常:

OutOfBoundsDatetime                       
Traceback (most recent call last)
<ipython-input-17-e9567d888396> in <module>()
----> 1 mdldata1=pd.read_sas('H:/My Documents/data.sas7bdat')
~\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\io\sas\sasreader.py in read_sas(filepath_or_buffer, format, index, encoding, chunksize, iterator)
     66         return reader
     67 
---> 68     data = reader.read()
     69     reader.close()
     70     return data
...
pandas\_libs\tslib.pyx in pandas._libs.tslib.array_with_unit_to_datetime()

pandas\_libs\tslib.pyx in pandas._libs.tslib.array_with_unit_to_datetime()

OutOfBoundsDatetime: cannot convert input 1609887547.0 with the unit 'd'

1 个答案:

答案 0 :(得分:0)

看起来您的SAS数据集的变量类型可能附加了错误的格式,这使Python例程混乱。请注意,SAS将日期存储为天数,将日期时间存储为秒数。只是更改附加到变量的显示格式并不会更改变量的值。

如果您可以将SAS数据集修复为使用DATETIME格式,则read_sas()不会尝试将其转换为日期值。或使用DATEPART()函数将值转换为有效日期。

如果您不能然后查看read_sas方法是否具有忽略变量附加格式的选项,只需返回秒数作为其浮点值即可。然后,您可以根据需要编写自己的代码以将其转换为日期。