What you will be able to do
- Explain how a Snowpark DataFrame is built, transformed and then run by an action method
- Keep only the rows and columns that matter, with Snowpark filter/select or SQL SELECT * EXCLUDE
- Cast values to the right type in Snowpark and in SQL, and pick TRY_CAST when bad strings should become NULL
Key concept
Lazy evaluation of Snowpark DataFrames — A Snowpark DataFrame is a description of a query, not a set of rows already pulled into memory. Each cleaning step you chain onto it only defines the query. Nothing runs in Snowflake until you call an action method such as collect().
1.Why cleaning in Snowpark is really writing a query
In this subdomain, every cleaning task can be done two ways: as SQL, or as Snowpark for Python. They are less different than they look. The Snowpark library is built so that your Python code can process data in Snowflake without moving data to the system where your application code runs. You work through a DataFrame, which the documentation compares to a query that still has to be evaluated.
That comparison explains how a Snowpark cleaning pipeline behaves. Using a DataFrame takes three steps. First you construct it and name the data source: a table, a staged file, local values, or a SQL statement. Then you say how to transform it: which columns to select, how to filter rows, how to sort and group them. Last, you call a method that performs an action, such as collect(). Only that final step sends work to Snowflake.
Checkpoint 1 of 5· Put it in order
Put the three steps of working with a Snowpark DataFrame in order.
- 1.Specify how the dataset should be transformed (select columns, filter rows, sort, group)
- 2.Construct a DataFrame, specifying the source of the data (table, staged file, local data or SQL statement)
- 3.Invoke an action method such as collect() to execute the statement and retrieve the data
Construction and transformation only define the query. The action method is what makes Snowflake run it.
“In order to retrieve the data into the DataFrame, you must invoke a method that performs an action (for example, the collect() method).”Source: docs.snowflake.com
SQL and Python meet in session.sql(). This method returns a DataFrame, and the documentation notes that the SQL statement won’t be executed until you call an action method. There is one catch. Transformation methods such as filter and select work only when the underlying statement is a SELECT. If you wrap a command like ls @my_stage in session.sql() and then call filter on it, the call fails. In the documented example the error is a SQL compilation error reporting invalid identifier 'SIZE'.
df = session.sql("select id, parent_id from sample_product_data where id < 10")
# Because the underlying SQL statement for the DataFrame is a SELECT statement,
# you can call the filter method to transform this DataFrame.
results = df.filter(col("id") < 3).select(col("id")).collect()Checkpoint 2 of 5· Check yourself
A data scientist writes df = session.sql("ls @my_stage") and then calls df.filter(col("size") > 50).collect(). What happens?
filter and select are supported only on a DataFrame whose SQL is a SELECT. The documented example of this call raises an error (a SQL compilation error reporting invalid identifier 'SIZE').
“these methods work only if the underlying SQL statement is a SELECT statement.”Source: docs.snowflake.com
2.Keeping the critical rows and dropping irrelevant fields
The exam guide asks you to identify critical data and remove irrelevant fields. None of the sources here defines "critical data" as a formal term, so treat it as a practical job. Decide which rows and which columns the analysis actually needs, and cut the rest before anything else runs. In Snowpark you do that with the DataFrame transformation methods. The documentation's guidance is to call the DataFrame methods that transform the dataset to specify which columns to select and how to filter, sort, group, etc. results. filter keeps rows, select keeps columns, and you name columns with the col function. The example in the previous section does both: it filters to id < 3 and keeps only the id column.
In SQL, select only the columns you need. When the table is wide and only a few fields are irrelevant, SELECT * EXCLUDE is shorter: it returns every column except the ones you list. Use the parenthesised form to exclude several at once.
SELECT * EXCLUDE (department_id, employee_id) FROM employee_table;Checkpoint 3 of 5· Check yourself
A 40-column table has two ID columns that would leak into a model. Which SQL keeps every other column without listing all 38 by name?
EXCLUDE drops the named columns and keeps the rest. DISTINCT removes duplicate rows, SAMPLE takes a subset of rows, and RENAME only changes a column name.
“This example shows how to select all columns in employee_table except for the department_id column:”Source: docs.snowflake.com
Removing duplicate rows is another way of keeping only the rows that matter. In SQL, SELECT DISTINCT eliminates duplicate values from the result set, which only removes rows that are identical in every selected column. When duplicates share a key but differ in other columns, such as several versions of the same customer, you must choose which one to keep. The documentation's pattern for this is QUALIFY with ROW_NUMBER(): a window function numbers the rows inside each PARTITION BY group, the ORDER BY decides which row comes first, and keeping row number 1 keeps one row per key. The documentation describes this version as explicit about which duplicate to keep. If you need only one value per key, such as the latest updated_at, a GROUP BY with MAX or MIN does that instead.
QUALIFY ROW_NUMBER() OVER (PARTITION BY customer_id ORDER BY updated_at DESC) = 1;Checkpoint 4 of 5· Exam question
A Snowpark for Python pipeline loads a `CUSTOMER_SNAPSHOT` DataFrame with several rows per `customer_id`, each carrying a different `updated_at` timestamp. The churn model must train on only the most recent row for every customer. Which approach is MOST appropriate?
Correct answer: A — Add `row_number()` over a window partitioned by `customer_id` and ordered by `updated_at` descending, then keep only the rows numbered 1.
- A. Correct. A ranked window gives a deterministic winner per customer, so the newest snapshot is always the row kept.
- B. Incorrect. When a subset of columns is given, `drop_duplicates` keeps an arbitrary row per key and ignores any earlier sort order, so the newest row is not guaranteed.
- C. Incorrect. `distinct()` compares entire rows, and snapshots with different timestamps are never identical, so every row survives.
- D. Incorrect. The aggregation returns only the grouping key and the maximum timestamp; the other feature columns are dropped unless the result is joined back.
3.Casting types: Column.cast in Snowpark, TRY_CAST in SQL
Raw data often arrives with the wrong types, for example numbers stored as text or values inside a VARIANT. In Snowpark you cast a column expression with .cast() and a type from snowflake.snowpark.types. The documentation shows this on JSON values pulled out with FLATTEN. The code below adds casting the values to a specific type and changing the names of the columns to the previous example. In the output, the quoted JSON strings become plain strings under readable headers.
df.join_table_function("flatten", col("src")["customer"]).select(col("value")["name"].cast(StringType()).as_("Customer Name"), col("value")["address"].cast(StringType()).as_("Customer Address")).show()In SQL, CAST (or ::) raises an error on a value it cannot convert, and one bad row is enough to stop the whole statement. TRY_CAST is a special version of CAST for a subset of conversions. A failed conversion becomes NULL instead of an error, so you can find and handle those rows later as missing values. It has two limits. It only works for string expressions. Its target must be VARCHAR, NUMBER, DOUBLE, BOOLEAN, DATE, an interval variation, TIME, or one of the TIMESTAMP types.
SELECT TRY_CAST('05/16' AS TIMESTAMP);Checkpoint 5 of 5· Check yourself
Which of these is a valid use of TRY_CAST according to its documented restrictions?
TRY_CAST accepts only string input, and NUMBER is one of its allowed targets. The other options start from a non-string source, and ARRAY is not in the list of allowed targets either.
“Only works for string expressions.”Source: docs.snowflake.com
Exam traps
Each one states something that sounds right. Open it to see what is actually true.
1.TRY_CAST is a drop-in, error-free replacement for CAST on any column type.Why is that wrong?
TRY_CAST accepts only string expressions and converts only to a listed set of target types. For other sources you still need CAST.
Covered in Casting types: Column.cast in Snowpark, TRY_CAST in SQL
2.Any statement passed to session.sql() returns a DataFrame you can keep cleaning with filter and select.Why is that wrong?
session.sql() always returns a DataFrame, but the transformation methods work only when the underlying statement is a SELECT. Other commands raise an error when transformed.
Covered in Why cleaning in Snowpark is really writing a query
Sources
Every claim above is drawn from one of these pages, quoted as it was written on the date shown.
- 1.
“process data in Snowflake without moving data to the system where your application code runs.”
↩︎ Why cleaning in Snowpark is really writing a query - 2.
“Note that the SQL statement won’t be executed until you call an action method.”
↩︎ Why cleaning in Snowpark is really writing a query“invalid identifier 'SIZE'”
↩︎ Why cleaning in Snowpark is really writing a query“To specify which columns to select and how to filter, sort, group, etc. results, call the DataFrame methods that transform the dataset.”
↩︎ Keeping the critical rows and dropping irrelevant fields“The following code adds to the previous example by casting the values to a specific type and changing the names of the columns:”
↩︎ Casting types: Column.cast in Snowpark, TRY_CAST in SQL“A DataFrame represents a relational dataset that is evaluated lazily: it only executes when a specific action is triggered.”
↩︎ Key concept“The transformation methods are not supported for other kinds of SQL statements.”
↩︎ Exam trap 2“In order to retrieve the data into the DataFrame, you must invoke a method that performs an action (for example, the collect() method).”
↩︎ Checkpoint“these methods work only if the underlying SQL statement is a SELECT statement.”
↩︎ Checkpoint - 3.
“This example shows how to select all columns in employee_table except for the department_id column:”
↩︎ Keeping the critical rows and dropping irrelevant fields“DISTINCT eliminates duplicate values from the result set.”
↩︎ Keeping the critical rows and dropping irrelevant fields - 4.
“The QUALIFY version is explicit about which duplicate to keep.”
↩︎ Keeping the critical rows and dropping irrelevant fields - 5.
“returns a NULL value instead of raising an error when the conversion can not be performed.”
↩︎ Casting types: Column.cast in Snowpark, TRY_CAST in SQL“Only works for string expressions.”
↩︎ Exam trap 1“Only works for string expressions.”
↩︎ Checkpoint