BookmarkSubscribeRSS Feed

Getting a Python CASTable Object from an Existing CAS Table

Started ‎04-14-2016 by
Modified ‎04-16-2019 by
Views 965

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

In[2]: 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 following step.

 

In[3]: conn.read_csv('https://raw.githubusercontent.com/sassoftware/sas-viya-programming/master/data/class.csv', 
                     casout=dict(name='class', caslib='casuser'))
Out[3]: 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[4]: conn.table.tableinfo(caslib='casuser')

Screen Shot 2016-08-12 at 2.22.46 PM.png

 

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[5]: cls = conn.CASTable('class', caslib='CASUSER')

In[6]: cls
Out[6]: CASTable('class', caslib='CASUSER')

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

 

In[7]: cls.to_frame()

Screen Shot 2016-08-12 at 2.24.00 PM.png

 

In[8]: conn.close()

 

Resources

 

You can download the Jupyter notebook version of this article at https://github.com/sassoftware/sas-viya-programming/tree/master/communities.

Version history
Last update:
‎04-16-2019 09:12 AM
Updated by:
Contributors

SAS INNOVATE 2024

Innovate_SAS_Blue.png

Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.

If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website. 

Register now!

Free course: Data Literacy Essentials

Data Literacy is for all, even absolute beginners. Jump on board with this free e-learning  and boost your career prospects.

Get Started

Article Tags