Getting a Python CASTable Object from an Existing CAS Table

Many of the examples in the Python series of articles here use a CASTable object to invoke actions or apply DataFrame-like syntax to CAS tables. In those examples, the CASTable object is generally the result of an action that loads the CAS table from a data file or other data source. But what if you have a CAS table already loaded in a session and you want to create a new CASTable object that points to it?

The first thing you need is a connection to CAS.


In [1]:
import swat

conn = swat.CAS(host, port, username, password)

We'll load a table of data here so we have something to work with. We'll also specify a table name and caslib so they are easier to reference in the next step.


In [2]:
conn.read_csv('https://raw.githubusercontent.com/sassoftware/sas-viya-programming/master/data/class.csv', 
              casout=dict(name='class', caslib='casuser'))


Out[2]:
CASTable('CLASS', caslib='CASUSER(kesmit)')

Using the tableinfo action, we can see that the table exists, however, we didn't store the output of the read_csv method, so we don't have a CASTable object pointing to it.


In [3]:
conn.tableinfo(caslib='casuser')


Out[3]:
§ TableInfo
Name Rows Columns Encoding CreateTimeFormatted ModTimeFormatted JavaCharSet CreateTime ModTime Global Repeated View SourceName SourceCaslib Compressed Creator Modifier
0 CLASS 19 5 utf-8 09Sep2016:11:21:53 09Sep2016:11:21:53 UTF8 1.789039e+09 1.789039e+09 0 0 0 0 kesmit

elapsed 0.00529s · user 0.006s · sys 0.011s · mem 0.482MB

The solution is fairly simple, you use the CASTable method of the CAS connection object. You just pass it the name of the table and the name of the CASLib just as it is printed in In[2] above.


In [4]:
cls = conn.CASTable('class', caslib='CASUSER')
cls


Out[4]:
CASTable('class', caslib='CASUSER')

We now have a CASTable object that we can use to interact with.


In [5]:
cls.to_frame()


Out[5]:
Selected Rows from Table CLASS
Name Sex Age Height Weight
0 Alfred M 14 69.0 112.5
1 Henry M 14 63.5 102.5
2 Jeffrey M 13 62.5 84.0
3 Louise F 12 56.3 77.0
4 Ronald M 15 67.0 133.0
5 Alice F 13 56.5 84.0
6 James M 12 57.3 83.0
7 John M 12 59.0 99.5
8 Mary F 15 66.5 112.0
9 Thomas M 11 57.5 85.0
10 Barbara F 13 65.3 98.0
11 Jane F 12 59.8 84.5
12 Joyce F 11 51.3 50.5
13 Philip M 16 72.0 150.0
14 William M 15 66.5 112.0
15 Carol F 14 62.8 102.5
16 Janet F 15 62.5 112.5
17 Judy F 14 64.3 90.0
18 Robert M 12 64.8 128.0

In [6]:
conn.close()

In [ ]: