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

    Domain 3 · Lesson 9/16

    Connecting Python and VS Code to Snowflake data

    Connect data science tools directly to data in Snowflake.

    20 min read
    6.2% of exam
    9 sources
    Published 20 Oct 2026
    Docs as of 18 Oct 2026

    What you will be able to do

    • Open a Snowpark session from connection parameters or a connections.toml entry, and choose among the Java, Python and Scala Snowpark libraries
    • Predict when a Snowpark DataFrame actually sends SQL to Snowflake, and keep heavy transformations in the warehouse
    • Read query results into pandas with the Snowflake Connector for Python, including batch reads for results too large for client memory, and write pandas data back
    • Install snowflake-ml-python and hand it a Snowpark session pointed at the right warehouse
    • Set up the Snowflake extension for Visual Studio Code to browse objects, run SQL and debug Snowpark code

    Key concept

    Pushdown — Snowpark client code only describes the work. The transformations are compiled to SQL and run inside Snowflake, so data of any size is processed next to where it is stored and does not have to be pulled into the Python, Java or Scala process.

    1.Snowpark sessions and the three Snowpark languages

    Snowpark lets a data scientist work on Snowflake tables from ordinary code without copying the tables out. Applications built with it process data in Snowflake rather than on the machine running the code, and the elastic Snowflake engine handles scale. You can write Snowpark Python on a laptop in a local development environment or in a Python worksheet in Snowsight. Locally, Jupyter, VS Code and IntelliJ are all supported authoring tools.

    Snowpark is not only for Python. Snowflake ships Snowpark libraries for Java, Python and Scala, and each has its own developer guide and API reference. A platform team with Python data scientists and Java application developers can therefore standardise on Snowpark for both groups. Whichever language a team uses, the computation happens in Snowflake, so Snowpark needs no Spark-style cluster of its own, and Snowflake manages scale and compute.

    Everything in Snowpark starts with a session. Import Session from snowflake.snowpark, put the connection parameters in a Python dict, pass the dict to Session.builder.configs(...), then call create(). These are the same parameters the Snowflake Connector for Python's connect function takes, and authentication works the same way: username and password, SSO through "authenticator": "externalbrowser", MFA, key pair or OAuth. The account value is the account identifier without the snowflakecomputing.com suffix. Role, warehouse, database and schema are optional, but the warehouse you set is where your queries will run.

    Creating a Snowpark session from a dict of connection parameterspython
    connection_parameters = {
      "account": "<your snowflake account>",
      "user": "<your snowflake user>",
      "password": "<your snowflake password>",
      "role": "<your snowflake role>",  # optional
      "warehouse": "<your snowflake warehouse>",  # optional
      "database": "<your snowflake database>",  # optional
      "schema": "<your snowflake schema>",  # optional
    }
    
    new_session = Session.builder.configs(connection_parameters).create()

    Credentials do not need to be hard-coded. You can define a named connection such as [myconnection] in a connections.toml file, using the same options that snowflake.connector.connect accepts, and open it with Session.builder.config("connection_name", "myconnection").create(). Keeping a second entry such as [myconnection_test] that points at a test warehouse and database lets the same script switch environments by name. When you are done, close() ends the session and cancels any queries it still has running.

    Checkpoint 1 of 9· Fill the gap

    This sample creates a Snowpark session from a dict of connection parameters. Which builder method goes in the blank?

    connection_parameters = {
      "account": "<your snowflake account>",
      "user": "<your snowflake user>",
      "password": "<your snowflake password>",
      "role": "<your snowflake role>",  # optional
      "warehouse": "<your snowflake warehouse>",  # optional
      "database": "<your snowflake database>",  # optional
      "schema": "<your snowflake schema>",  # optional
    }
    
    new_session = Session.builder. ? (connection_parameters).create()

    Checkpoint 2 of 9· Check yourself

    A platform team has Python data scientists and Java application developers. Both groups need to process Snowflake tables without moving the data out of Snowflake. What should the team do?

    Sources123

    2.Lazy DataFrames: when Snowpark actually runs SQL

    The Snowpark DataFrame is the main object you work with. It represents a set of data and has methods for selecting columns, filtering rows and the other steps that shape the data. You can build the query with methods such as select and col(...) instead of SQL strings, which gives you code completion and type checking.

    Snowpark DataFrames are lazily evaluated. Calling session.table, filter or select adds a step to a query plan and nothing else. Until an action is called, the client sends no SQL, the warehouse does no work and QUERY_HISTORY shows nothing. A script that only builds DataFrames and prints "features ready" has never run a query. Nothing is held in a local cache either: the filter and the select both become part of one SQL statement that the action sends to Snowflake.

    Building a DataFrame runs nothing; collect() sends the querypython
    >>> # Create a DataFrame with the "id" and "name" columns from the "sample_product_data" table.
    >>> # This does not execute the query.
    >>> df = session.table("sample_product_data").select(col("id"), col("name"))
    
    >>> # Send the query to the server for execution and
    >>> # return a list of Rows containing the results.
    >>> results = df.collect()
    DataFrame methods that are actions: each one evaluates the plan and sends SQL to the server
    ClassMethodWhat happens
    DataFramecollectRuns the query and returns the result as a list of Row objects to the client
    DataFramecountRuns the query and returns the number of rows
    DataFrameshowRuns the query and prints rows to the console, 10 by default
    DataFrameWritersave_as_tableRuns the query and saves the result to a table in Snowflake

    Laziness is what makes pushdown cheap. Snowpark collects many transformations into one statement and runs it as late as possible, which cuts the data passing between client and database. For feature engineering on a 2-billion-row table from a laptop, chain the transformations on session.table(...) and end with save_as_table so the result stays in Snowflake. Calling to_pandas() on the full table, or running SELECT * into local pandas, pulls every row into laptop memory. Unloading the table to files and reading them with a local Spark session also moves all the data out, so it gives up the reason for using Snowpark at all.

    Custom logic can be pushed down as well. A Snowpark UDF wraps a Python function so Snowflake can call it from SQL (Python UDFs (scalar and vectorized)). Snowpark ships the function's code to the server, so it runs next to the data and Snowflake can parallelise it, and no rows travel to the client to be processed.

    Checkpoint 3 of 9· Check yourself

    From a laptop, a data scientist must engineer features on a 2-billion-row Snowflake table and keep the result for training. Which approach makes the best use of Snowpark?

    Checkpoint 4 of 9· Exam question

    A data scientist working in Visual Studio Code on a laptop must engineer features from a 2-billion-row table without moving the data out of Snowflake. Which approach is MOST appropriate?

    Checkpoint 5 of 9· Exam question

    A data scientist runs this Snowpark code from an external IDE and then checks QUERY_HISTORY, but no statement from the script appears. from snowflake.snowpark.functions import col df = session.table("CUSTOMERS").filter(col("REGION") == "EMEA").select("ID", "SPEND") print("features ready") What explains this?

    Sources14

    3.The Python Connector and moving data to and from pandas

    The Snowflake Connector for Python is the lower-level option. It implements the Python DB API v2 (PEP 249): you open a Connection, get a Cursor, and run SQL or DDL/DML with it from any Python environment. It does no pushdown of Python code. Your SQL runs in Snowflake, but every row you fetch is copied into the client process. That is the right trade when the result is small enough to hold locally and the next step needs pandas or another library that only runs locally.

    The pandas methods come from a package extra: pip install "snowflake-connector-python[pandas]". You must type the square brackets, and the quotes stop the shell from reading the brackets as a wildcard. The extra installs a compatible PyArrow. If a different PyArrow version is already installed, uninstall it first, and don't install another version afterwards. With these methods built in, you no longer need SQLAlchemy to turn cursor results into a DataFrame, although the connector still works with it.

    Reading into pandas takes two steps: run the query on a cursor, then call one of two cursor methods. fetch_pandas_all() returns one DataFrame with the whole result, so the whole result has to fit in client memory. fetch_pandas_batches() returns an iterator that yields one DataFrame per result batch. Use it to process each chunk and then drop it, so only one batch is in memory at a time. On a 16 GB VM that has to feed an 80-million-row result to a pandas featurizer, the batch iterator is the fix. A larger warehouse does not help, because the warehouse runs the query but the client still receives every row. fetchall() builds the full list of tuples in memory, and appending fetchone() rows to a growing DataFrame still ends up holding every row.

    Writing back works the other way. Call write_pandas(), or use pandas DataFrame.to_sql() with pd_writer() as the insert method. Both upload the local frame into a Snowflake table. Data types change on the way in. One mapping causes surprises: a whole-number column with a NULL comes back as float64, not an integer. If a value overflows during conversion, the connector raises an exception instead of silently corrupting the value.

    How Snowflake types arrive in pandas through the connector
    Snowflake data typepandas data type
    FIXED NUMERIC, scale = 0 (not DECIMAL)(u)int{8,16,32,64}, or float64 if NULL
    FIXED NUMERIC, scale > 0 (not DECIMAL)float64
    FIXED NUMERIC DECIMALdecimal
    VARCHAR, BINARY, VARIANTstr
    DATEobject (datetime.date objects)
    TIME, TIMESTAMP_NTZ, TIMESTAMP_LTZ, TIMESTAMP_TZpandas.Timestamp(np.datetime64[ns])

    Checkpoint 6 of 9· Match them up

    You read a Snowflake result into pandas with fetch_pandas_all(). Match each Snowflake column to the pandas data type it arrives as.

    Tap a term, then the definition that fits it.

    Checkpoint 7 of 9· Exam question

    A job on a 16 GB VM must read an 80-million-row result set through the Snowflake Connector for Python and feed it to a pandas-based featurizer. fetch_pandas_all() runs out of memory. Which approach is MOST appropriate?

    Sources567

    4.Using snowflake-ml-python through a Snowpark session

    Snowflake ML's Python APIs all ship in one package, snowflake-ml-python, which includes modeling modules such as snowflake.ml.modeling.preprocessing. Snowflake Notebooks and Snowsight worksheets need no install: choose snowflake-ml-python from the Packages menu. In both places, your organization's package policy must allow the package and its dependencies. To use the package on a workstation or anywhere else outside Snowflake, you must install it yourself.

    Conda is the preferred way to install it, from the Snowflake conda channel, because packages there have been validated with Snowflake ML. pip from PyPI also works, but only in a plain virtualenv. Don't use pip inside a conda environment. scikit-learn installs by default. LightGBM, XGBoost, Keras and PyTorch are optional, so you add them yourself, for example with pip install 'snowflake-ml-python[lightgbm]' before using snowflake.ml.modeling.lightgbm. Installing snowflake-ml-python also installs Snowpark Python, because Snowpark is a dependency.

    Snowflake ML connects through a Snowpark Session. Once you create one, you pass it to any Snowflake ML API that needs it. SnowflakeLoginOptions in snowflake.ml.utils.connection_params builds the parameter dict for you, either from a named connection in your SnowSQL configuration file or from environment variables. If you already have a Python Connector connection, pass it as Session.builder.configs({"connection": connection}).create().

    Building a session for Snowflake ML from a named SnowSQL connectionpython
    from snowflake.snowpark import Session
    from snowflake.ml.utils import connection_params
    
    params = connection_params.SnowflakeLoginOptions("myaccount")
    sp_session = Session.builder.configs(params).create()

    The session decides where your ML work runs. Many Snowflake ML operations, such as training and inference, run in the warehouse set on the session, not on your laptop. If the named configuration has no warehouse, add params["warehouse"] = "mlwarehouse" before calling create(), or call sp_session.use_warehouse("mlwarehouse") afterwards to set one or switch to another.

    Checkpoint 8 of 9· Fill the gap

    A data scientist wants to read the named connection "myaccount" from the SnowSQL configuration file and turn it into a Snowpark session for Snowflake ML. Which function from snowflake.ml.utils.connection_params belongs in the blank?

    from snowflake.snowpark import Session
    from snowflake.ml.utils import connection_params
    
    params = connection_params. ? ("myaccount")
    sp_session = Session.builder.configs(params).create()

    Sources8

    5.Working from Visual Studio Code with the Snowflake extension

    The Snowflake extension for Visual Studio Code lets you write and run Snowflake SQL in VS Code. It also works with Snowpark Python, adding debugging, plus syntax highlighting and autocomplete for SQL inside Python strings. Install it from the Visual Studio Marketplace by searching for Snowflake and checking for the Snowflake badge, or install a downloaded .vsix file with Install from VSIX. You must sign in to an account before you can run SQL or use Snowpark Python.

    Ways to sign in to Snowflake from the VS Code extension
    MethodWhat you provide
    Username/passwordAccount identifier or URL, username and password
    Single sign-onUsername in VS Code, then SSO credentials on a separate authentication page
    Key PairKey-pair authentication credentials
    OAuthAn OAuth connection defined in the connections.toml configuration file

    Once you sign in, the sidebar shows your account, your default role, an Object Explorer listing databases, and your query history. The status area shows the current role, database, schema and active warehouse. Connection definitions are stored in connections.toml, which you can edit with Snowflake: Edit Connections File. The extension can also load connections from a SnowSQL configuration file through the Snowsql Config Path setting. Only the connection values from that file are used, and for new work Snowflake recommends Snowflake CLI config.toml connections instead.

    SQL runs from Snowflake SQL files. Execute All Statements runs the whole file; selecting statements and choosing Execute runs them in order from top to bottom. You can cancel a running query from Query History. Results open in the Snowsight results viewer, which shows up to 1 million rows. You can sort, hide or freeze columns there, or save the results as CSV. To fill that pane, the extension silently runs DESC RESULT '<query_id>' after every query, so LAST_QUERY_ID() no longer returns the query you ran. Scripts that rely on it give wrong answers when run from VS Code.

    For Snowpark, write a stored procedure as a Python function whose first parameter is a Snowpark Session. A Snowflake: Debug option then appears above the function. It runs the procedure with the extension's active session and stops at your breakpoints. With Auto Detect Sql in Python turned on, a Python string whose first word is an all-caps SQL keyword gets SQL highlighting. If you turn detection off, --begin-sql / --end-sql comments mark the SQL instead. While you are connected, the extension also suggests table and column names in these strings.

    Checkpoint 9 of 9· Check yourself

    In a Snowflake SQL file in VS Code, a data scientist runs a CREATE TABLE ... AS SELECT statement and then queries RESULT_SCAN(LAST_QUERY_ID()) to inspect its output. The same script works correctly in other clients. What happens when it runs through the Snowflake extension?

    Sources9

    Practise it for real

    Open a Snowpark session from a named connection and confirm that a DataFrame runs only when an action is called

    1. 1.Add a [myconnection] entry with account, user, password, warehouse, database and schema to connections.toml.

      Why: A named connection keeps credentials out of code and works for both Snowpark and the connector.

      You should see: The file saves with the new connection definition.

    2. 2.In Python, run session = Session.builder.config("connection_name", "myconnection").create().

      Why: Every Snowpark operation needs a session.

      You should see: A Session object is returned without an error.

    3. 3.Build df = session.table("sample_product_data").select(col("id"), col("name")) and check the account's query history.

      Why: Building a DataFrame only records the query plan.

      You should see: No SELECT on sample_product_data appears yet.

    4. 4.Call df.collect(), check query history again, then call session.close().

      Why: collect is an action, so it sends the SQL to the warehouse. close ends the session.

      You should see: One SELECT appears, and you get back a list of Row objects.

    Stuck? Get a nudge

    If the query history shows nothing after collect, check that the session has a warehouse set.

    Sources

    (9)

    Every claim above is drawn from one of these pages, quoted as it was written on the date shown.

    1. 1.
      “Snowflake currently provides Snowpark libraries for three languages: Java, Python, and Scala.”
      ↩︎ Snowpark sessions and the three Snowpark languages
      “Support for authoring Snowpark code using local tools such as Jupyter, VS Code, or IntelliJ.”
      ↩︎ Snowpark sessions and the three Snowpark languages
      “This reduces the amount of data transferred between your client and the Snowflake database.”
      ↩︎ Lazy DataFrames: when Snowpark actually runs SQL
      “No requirement for a separate cluster outside of Snowflake for computations.”
      ↩︎ Lazy DataFrames: when Snowpark actually runs SQL
      “When you call the UDF in your client code, your custom code is executed on the server (where the data is).”
      ↩︎ Lazy DataFrames: when Snowpark actually runs SQL
      “Snowpark pushes down all data transformation and heavy lifting to the Snowflake data cloud, enabling you to efficiently work with data of any size.”
      ↩︎ Key concept
      “Snowflake currently provides Snowpark libraries for three languages: Java, Python, and Scala.”
      ↩︎ Exam trap 3
      “The data isn’t retrieved when you construct the DataFrame object.”
      ↩︎ Prediction
      “Snowpark pushes down all data transformation and heavy lifting to the Snowflake data cloud, enabling you to efficiently work with data of any size.”
      ↩︎ Checkpoint
    2. 2.
      “You can write Snowpark Python code in a local development environment or in a Python worksheet in Snowsight.”
      ↩︎ Snowpark sessions and the three Snowpark languages
    3. 3.
      “To authenticate, you use the same mechanisms that the Snowflake Connector for Python supports.”
      ↩︎ Snowpark sessions and the three Snowpark languages
      “Note that the account identifier does not include the snowflakecomputing.com suffix.”
      ↩︎ Snowpark sessions and the three Snowpark languages
      “you want to cancel any queries that are currently running, call the close method of the Session object.”
      ↩︎ Snowpark sessions and the three Snowpark languages
    4. 4.
      “the DataFrame is lazily evaluated, which means the SQL statement isn’t sent to the server for execution until you perform an action.”
      ↩︎ Lazy DataFrames: when Snowpark actually runs SQL
      “the DataFrame is lazily evaluated, which means the SQL statement isn’t sent to the server for execution until you perform an action.”
      ↩︎ Exam trap 1
    5. 5.
      “The connector supports developing applications using the Python Database API v2 specification (PEP-249)”
      ↩︎ The Python Connector and moving data to and from pandas
    6. 6.
      “You must enter the square brackets ([ and ]) as shown in the command.”
      ↩︎ The Python Connector and moving data to and from pandas
      “To read data into a pandas DataFrame, you use a Cursor to retrieve the data”
      ↩︎ The Python Connector and moving data to and from pandas
      “the value is NULL, then the value is converted to float64, not an integer type.”
      ↩︎ The Python Connector and moving data to and from pandas
      “SQLAlchemy is no longer needed to convert data in a cursor into a DataFrame.”
      ↩︎ The Python Connector and moving data to and from pandas
      “These methods return a DataFrame for every result set, including JSON results from SHOW, DESCRIBE, DDL, PUT, and GET.”
      ↩︎ Prediction
      “and if the value is NULL, then the value is converted to float64, not an integer type.”
      ↩︎ Checkpoint
    7. 7.
      “fetch_pandas_batches(): Call this method to return an iterator that you can use to return a pandas DataFrame for each result batch.”
      ↩︎ The Python Connector and moving data to and from pandas
      “fetch_pandas_batches(): Call this method to return an iterator that you can use to return a pandas DataFrame for each result batch.”
      ↩︎ Exam trap 2
    8. 8.
      “All Snowflake ML features are available in a single package, snowflake-ml-python.”
      ↩︎ Using snowflake-ml-python through a Snowpark session
      “The snowflake-ml-python package and its dependencies must be allowed by your organization’s package policy.”
      ↩︎ Using snowflake-ml-python through a Snowpark session
      “Snowpark Python is a dependency of snowflake-ml-python and is installed automatically with it.”
      ↩︎ Using snowflake-ml-python through a Snowpark session
      “These operations run in the warehouse specified by the session you use to connect.”
      ↩︎ Using snowflake-ml-python through a Snowpark session
      “To create a Snowpark Python session from a Snowflake Connector for Python connection, pass the connection object to the session builder.”
      ↩︎ Using snowflake-ml-python through a Snowpark session
      “Do not use this installation procedure if you are using a conda environment.”
      ↩︎ Exam trap 5
    9. 9.
      “The extension also integrates with Snowpark Python to provide debugging, syntax highlighting, and autocomplete features for SQL in Python code.”
      ↩︎ Working from Visual Studio Code with the Snowflake extension
      “the sidebar displays your account information, your default role, the OBJECT EXPLORER with a Databases list, and your QUERY HISTORY.”
      ↩︎ Working from Visual Studio Code with the Snowflake extension
      “For new work, Snowflake recommends Snowflake CLI config.toml connections.”
      ↩︎ Working from Visual Studio Code with the Snowflake extension
      “This process makes LAST_QUERY_ID() inaccurate.”
      ↩︎ Working from Visual Studio Code with the Snowflake extension
      “Write a Snowflake stored procedure in a Python function where the first parameter is a Snowpark Session object.”
      ↩︎ Working from Visual Studio Code with the Snowflake extension
      “This process makes LAST_QUERY_ID() inaccurate.”
      ↩︎ Exam trap 4
      “To display Snowflake query results, the VS Code extension automatically runs DESC RESULT '<query_id>' in the background after every query. This process makes LAST_QUERY_ID() inaccurate.”
      ↩︎ Checkpoint

    Ready to test yourself?

    Practise the 22 questions on this subdomain.

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