Pro ยท Lesson 9 of 13
DataFrames, Python UDFs, and stored procedures run where the data already lives.
Sign in to save your place.
Snowpark is a remote control. The Python on your laptop describes the work. Snowflake's warehouse does the work next to the table, so you are not downloading a million rows to add them up. A UDF is one small Python function the SQL engine can call per value. A procedure is a Python function that can run several statements and hand a result back.
The snowflake-snowpark-python package creates a session with an account identifier, a user, a role, a warehouse, a database, and a schema. Prefer key-pair authentication over embedding a password in a file. The DataFrame API builds a plan. show() or collect() is what sends it.
Snowpark code runs on a warehouse. A Snowpark-optimized warehouse is the documented choice when the job needs more memory, such as model training. Ordinary filters and group-bys are fine on a Standard warehouse. The Python still cannot see a table your role cannot select.
A Python UDF is created with LANGUAGE PYTHON, a RUNTIME_VERSION your account currently allows, and a HANDLER. Do not assume 3.11 is enabled. In Snowsight, check the Python versions the account offers and put that version in the statement. Packages you name must exist in the Snowflake Anaconda channel or be uploaded to a stage.
A stored procedure receives a Snowpark session as its first handler argument. It can query tables and return a string, a variant, or a table, depending on how you declare it. Procedures are on every edition, in Python, Java, Scala, JavaScript, and SQL.
Push filters into the DataFrame before you collect(). Collecting an entire fact table into the client defeats the point of the warehouse.
CREATE OR REPLACE FUNCTION double_qty(qty NUMBER)
RETURNS NUMBER
LANGUAGE PYTHON
RUNTIME_VERSION = '3.11'
HANDLER = 'double_qty'
AS
$$
def double_qty(qty):
if qty is None:
return None
return qty * 2
$$;
The RUNTIME_VERSION in the sample is a placeholder. If the create fails, the error is telling you the account does not offer that version. Change the string. Do not keep retrying a version the error rejected.
Snowpark Container Services and Streamlit in Snowflake exist and are listed in the edition matrix. They are different products from a DataFrame script. This lesson does not deploy a container.
Snowpark code looks like pandas but the work happens inside Snowflake, not on your laptop.
Step 1 of 3DataFrame calls do not download data. Each call adds to a plan that Snowpark turns into SQL.
df = session.table("orders").filter(col("amount") > 100)
Step 2 of 3Nothing runs until an action such as collect() or save_as_table(). Then the generated SQL runs on the warehouse, and only the result comes back.
df.group_by("region").agg(sum_("amount")).collect()
Step 3 of 3A Python UDF runs your function inside Snowflake for each value, and anyone can call it from plain SQL.
SELECT clean_phone(phone) FROM customers;
Run the SQL function in a worksheet after you replace the runtime version if needed. The Python is for your machine, with the Snowpark package installed. Fill the account identifier from Snowsight. Use a key file rather than a password if you already have one. The script only counts rows for one region.
-- Worksheet. Replace the runtime version if your account rejects 3.11.
CREATE OR REPLACE FUNCTION double_qty(qty NUMBER)
RETURNS NUMBER
LANGUAGE PYTHON
RUNTIME_VERSION = '3.11'
HANDLER = 'double_qty'
AS
$$
def double_qty(qty):
if qty is None:
return None
return qty * 2
$$;
SELECT double_qty(amount) FROM orders;
# Local Python, after: pip install snowflake-snowpark-python
from snowflake.snowpark import Session
from snowflake.snowpark.functions import col
session = Session.builder.configs({
"account": "<org>-<account>",
"user": "<user>",
"private_key_file": "<path-to-key.p8>",
"role": "SYSADMIN",
"warehouse": "LEARN_WH",
"database": "LEARN_DB",
"schema": "PUBLIC",
}).create()
west = session.table("orders").filter(col("region") == "west")
print(west.count())
session.close()
Checked against Snowflake documentation in October 2026. Product behavior still depends on your edition, cloud, and region.