Snowflake Snowpark Python

1.48.0 · active · verified Sun Mar 29

Snowflake Snowpark for Python provides an intuitive API for querying and processing data in Snowflake using Python. It enables data engineers and data scientists to build scalable data pipelines and machine learning workflows directly within Snowflake, leveraging its elastic, scalable, and secure engine. The library is actively maintained with frequent releases, typically every few weeks, bringing new features, improvements, and bug fixes.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to establish a Snowpark Session, create a DataFrame from local data, perform a basic transformation, and display the results. Connection parameters are loaded from environment variables for secure and flexible setup. Remember to replace placeholder values with your Snowflake account details.

import os
from snowflake.snowpark import Session
from snowflake.snowpark.functions import col

# Establish a Snowpark Session using environment variables
# Replace with your actual connection parameters, or configure ~/.snowflake/connections.toml
connection_parameters = {
    "account": os.environ.get("SNOWFLAKE_ACCOUNT", "your_account_identifier"),
    "user": os.environ.get("SNOWFLAKE_USER", "your_username"),
    "password": os.environ.get("SNOWFLAKE_PASSWORD", "your_password"),
    "role": os.environ.get("SNOWFLAKE_ROLE", "your_role"),
    "warehouse": os.environ.get("SNOWFLAKE_WAREHOUSE", "your_warehouse"),
    "database": os.environ.get("SNOWFLAKE_DATABASE", "your_database"),
    "schema": os.environ.get("SNOWFLAKE_SCHEMA", "your_schema"),
}

session = Session.builder.configs(connection_parameters).create()
print("Snowpark Session created successfully.")

# Create a simple DataFrame
data = [("Alice", 1), ("Bob", 2), ("Charlie", 3)]
df = session.create_dataframe(data, schema=["name", "id"])

# Perform a simple transformation and show results
df.filter(col("id") > 1).show()

# Close the session
session.close()
print("Snowpark Session closed.")

view raw JSON →