CertSafari
    Snowflake SnowPro Advanced: Data Scientist (DSA-C03)· Lessons

    Domain 2 · Lesson 5/16

    Snowpark DataFrames for Data Cleaning: Lazy Evaluation, Selecting Fields and Casting Types

    Prepare and clean data in Snowflake.

    9 min read
    6.75% of exam
    5 sources
    Published 5 Oct 2026
    Docs as of 4 Oct 2026

    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. 1.Specify how the dataset should be transformed (select columns, filter rows, sort, group)
    2. 2.Construct a DataFrame, specifying the source of the data (table, staged file, local data or SQL statement)
    3. 3.Invoke an action method such as collect() to execute the statement and retrieve the data

    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'.

    A SELECT wrapped by session.sql() can be transformed further. Nothing runs until collect().python
    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?

    Sources12

    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.

    SQL: return every column of employee_table except department_id and employee_idsql
    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?

    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.

    SQL: keep the latest row per customer_idsql
    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?

    Sources234

    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.

    Snowpark: cast extracted VARIANT fields to StringType and rename thempython
    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.

    An unparseable string becomes NULL rather than an errorsql
    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?

    Sources25

    Exam traps

    Each one states something that sounds right. Open it to see what is actually true.

    1. 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. 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. 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. 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
      “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. 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. 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

    Continue to page 2 of 2

    Joins, Aggregation, NULLs, Duplicates and Sampling in Snowflake SQL

    Spotted a mistake, or was something unclear? Tell us.