【问题标题】:extract a casout SAS table to pandas dataframe将 casout SAS 表提取到 pandas 数据帧
【发布时间】:2020-02-11 14:40:17
【问题描述】:

我正在使用 SAS 的 python API,并通过以下方式上传了一个表格:

s.upload("./data/hmeq.csv", casout=dict(name=tbl_name, replace=True))

我可以通过s.tableinfo()查看表格的详细信息。

§ TableInfo
    Name    Rows    Columns IndexedColumns  Encoding    CreateTimeFormatted ModTimeFormatted    AccessTimeFormatted JavaCharSet CreateTime  ... Repeated    View    MultiPart   SourceName  SourceCaslib    Compressed  Creator Modifier    SourceModTimeFormatted  SourceModTime
0   HMEQ    5960    13  0   utf-8   2020-02-10T16:48:02-05:00   2020-02-10T16:48:02-05:00   2020-02-10T21:10:34-05:00   UTF8    1.896990e+09    ... 0   0   0           0   aforoo      2020-02-10T16:48:02-05:00   1.896990e+09
1 rows × 23 columns

但是,我无法在 python 中访问表的任何值。例如,假设我想将行数和列数作为 python 标量。我知道我可以使用 pd.DataFrame 将 SAS 表放入 pandas 表中,但它不适用于此表,我得到:

~\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\core\frame.py in __init__(self, data, index, columns, dtype, copy)
    346                                  dtype=dtype, copy=copy)
    347         elif isinstance(data, dict):
--> 348             mgr = self._init_dict(data, index, columns, dtype=dtype)
    349         elif isinstance(data, ma.MaskedArray):
    350             import numpy.ma.mrecords as mrecords

~\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\core\frame.py in _init_dict(self, data, index, columns, dtype)
    457             arrays = [data[k] for k in keys]
    458 
--> 459         return _arrays_to_mgr(arrays, data_names, index, columns, dtype=dtype)
    460 
    461     def _init_ndarray(self, values, index, columns, dtype=None, copy=False):

~\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\core\frame.py in _arrays_to_mgr(arrays, arr_names, index, columns, dtype)
   7354     # figure out the index, if necessary
   7355     if index is None:
-> 7356         index = extract_index(arrays)
   7357 
   7358     # don't force copy because getting jammed in an ndarray anyway

~\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\core\frame.py in extract_index(data)
   7391 
   7392         if not indexes and not raw_lengths:
-> 7393             raise ValueError('If using all scalar values, you must pass'
   7394                              ' an index')
   7395 

ValueError: If using all scalar values, you must pass an index

我对 SAS 中的任何其他 casout 表都有同样的问题。我感谢任何帮助或评论。

【问题讨论】:

  • SAS的python api是什么?你说的是 saspy 吗?
  • 由于您的结果和表格在 CAS 中,您可能需要将结果下载到您的客户端才能读入数据框。我对 SWAT 还不是很熟悉,但可能有一个额外的功能可以做到这一点。 go.documentation.sas.com/…
  • @Tom,在任何基于 cas 的 API 中,当我获取 casout 表结果时,我遇到了这个问题。例如,考虑在sdataSciencePilot.exploreData(table=tbl_name, target='bad', casout=casout) 中使用的casout = dict(name = 'out1', replace=True),其中sswat.CAS(server, port) 的一个实例。
  • @Tom,我也有关于s.tableinfo() 的问题示例,这是一个swat.cas.results.CASResults 表。
  • @Tom,我找到并添加了解决方案。

标签: sas


【解决方案1】:

我建议您直接使用 Pandas 从 SAS 读取数据。

来自另一个答案的参考:Read SAS file with pandas

这是另一个例子 https://www.marsja.se/how-to-read-sas-files-in-python-with-pandas/

【讨论】:

  • 如果他的数据是本地的,这将起作用,但它位于称为 CAS 的分布式计算环境中。所有处理都在那里完成。
  • 我没有保存数据,正如@StuSztukowski 提到的数据在内存中。
  • @StuSztukowski,我找到了一个解决方案并将其发布,以便对其他人有所帮助。
【解决方案2】:

我在下面找到了解决方案,它工作正常。例如,这里我使用了dataSciencePilot.exploreData 动作,我可以通过以下方式获得结果:

casout = dict(name = 'out1', replace=True)
s.dataSciencePilot.exploreData(table=tbl_name, target='bad', casout=casout)
fetch_opts = dict(maxrows=100000000, to=1000000)
df = s.fetch(table='out1', **fetch_opts)['Fetch']
features = pd.DataFrame(df)
type(features)

返回pandas.core.frame.DataFrame

【讨论】:

    猜你喜欢
    • 2020-02-09
    • 2016-11-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-10
    • 2019-04-08
    相关资源
    最近更新 更多