How to Get Started with Snowflake Snowpark Python and Snowpark Pandas APIs

Apr 2, 2023 | Programming

Welcome to the world of Snowflake Snowpark! The Snowpark library offers powerful APIs that allow you to query and process data effortlessly in your data pipelines. With Snowpark, you can create applications that use Snowflake’s power without the need to transfer your data. In this guide, we will walk you through everything you need to know to start using Snowpark Python and Snowpark Pandas APIs.

Step 1: Prerequisites

  • Have your Snowflake account ready. If you don’t have one, you can sign up for a free 30-day trial.
  • Ensure that you have Python (3.8, 3.9, 3.10, or 3.11) installed in your environment.

Step 2: Create a Python Virtual Environment

To keep your project dependencies clean and organized, it’s recommended to create a virtual environment. You can use options like miniconda, anaconda, or virtualenv to set up your environment. For Snowpark Pandas, Python 3.9, 3.10, or 3.11 is required.

Step 3: Installing the Snowpark Library

Use the following command to install the Snowpark library in your virtual environment:

pip install snowflake-snowpark-python

If you want to use the Snowpark pandas API, you can also install the optional modin package with:

pip install snowflake-snowpark-python[modin]

Step 4: Creating a Session Using the Snowpark API

Now we can create a session to start using the Snowpark API. Here’s a sample of how you can accomplish this:

from snowflake.snowpark import Session

connection_parameters = {
    "account": "your_snowflake_account",
    "user": "your_snowflake_user",
    "password": "your_snowflake_password",
    "role": "snowflake_user_role",
    "warehouse": "snowflake_warehouse",
    "database": "snowflake_database",
    "schema": "snowflake_schema"
}

session = Session.builder.configs(connection_parameters).create()

Now that the session is created, let’s say you have a Snowpark DataFrame that you want to manipulate. Think of a DataFrame like a detailed book of your data—a collection of rows and columns. You can filter, add, and manipulate this “book” to derive meaningful insights.

Step 5: Using the Snowpark Pandas API

You can also utilize the Snowpark Pandas API, which provides an interface familiar to users of pandas, allowing seamless data operations. Here’s how:

import modin.pandas as pd
from snowflake.snowpark import Session

CONNECTION_PARAMETERS = {
    "account": "myaccount",
    "user": "myuser",
    "password": "mypassword",
    "role": "myrole",
    "database": "mydatabase",
    "schema": "myschema",
    "warehouse": "mywarehouse"
}

session = Session.builder.configs(CONNECTION_PARAMETERS).create()
df = pd.DataFrame([[a, 2.0, 1], [b, 4.0, 2], [c, 6.0, None]], columns=["COL_STR", "COL_FLOAT", "COL_INT"])

This will create a DataFrame similar to how you’d structure data in a spreadsheet, making it easy to interact with. You can drop, filter, or manipulate this data however you please.

Troubleshooting

If you experience issues, here are some common troubleshooting steps:

  • Ensure your Python version is compatible with Snowpark.
  • Check if the Snowflake connection parameters are correct.
  • Ensure that you have installed all required libraries in your virtual environment.

For more insights, updates, or to collaborate on AI development projects, stay connected with fxis.ai.

Conclusion

In this article, we’ve walked through the essentials of using Snowflake’s Snowpark Python and Pandas APIs. The versatility of both APIs allows developers and data scientists to utilize the power of Snowflake without the hassle of data movement, making processing seamless and efficient. At fxis.ai, we believe that such advancements are crucial for the future of AI, as they enable more comprehensive and effective solutions. Our team is continually exploring new methodologies to push the envelope in artificial intelligence, ensuring that our clients benefit from the latest technological innovations.

Stay Informed with the Newest F(x) Insights and Blogs

Tech News and Blog Highlights, Straight to Your Inbox