Snowflake Snowpark Python
repository·main·Indexed 43 days ago
https://github.com/snowflakedb/snowpark-pythonIntuitive Python APIs for querying and processing data directly within Snowflake. It features a native Snowpark Python API and a Snowpark pandas API (via Modin), enabling data pipeline development without moving data out of the Snowflake environment.
What's inside Snowpark
- Snowpark provides an intuitive API for querying and handling data directly within Snowflake. It allows you to build complex data pipelines and interact with Snowflake without the need to move data to the environment where your application code is running. The library includes both the standard Snowpark Python API and the Snowpark pandas API.
Overview of the Snowpark pandas API
mainThe Snowpark pandas API provides a pandas-compatible interface for performing distributed data processing on Snowflake. It allows users to use familiar pandas syntax (Series, DataFrames, indexing, etc.) while the underlying execution is offloaded to Snowflake's engine.
Key functional areas covered by the API include:
- Session & IO: Managing connections and reading/writing data.
- Core Data Structures:
SeriesandDataFrameobjects. - Data Manipulation:
groupby,resampling,windowfunctions, andindexing. - Functions:
general_functionsandnumpycompatibility. - Execution Models:
hybrid_executionandperformanceoptimizations. - Interoperability: Moving data between Snowpark pandas and other formats.
Access secrets in Snowpark Python
mainThe
snowflake.snowpark.secretsmodule provides functions and classes to retrieve sensitive information (secrets) stored in Snowflake. This allows you to securely access credentials like usernames, passwords, OAuth tokens, or cloud provider tokens within your Snowpark code without hardcoding them.Available classes:
UsernamePassword: Represents a username and password credential pair.CloudProviderToken: Represents a token for a cloud provider.
Available functions:
get_generic_secret_string: Retrieves a generic secret string.get_oauth_access_token: Retrieves an OAuth access token.get_secret_type: Determines the type of a secret.get_username_password: Retrieves username and password credentials.get_cloud_provider_token: Retrieves a cloud provider token.get_wif_token: Retrieves a Workload Identity Federation (WIF) token.
Use Snowflake Snowpark Python functions
mainThe
snowflake.snowpark.functionsmodule provides a comprehensive suite of functions that can be used to manipulate Snowpark DataFrames. These functions are designed to mirror Snowflake SQL functions, allowing you to perform complex data transformations, aggregations, and analytical operations directly on your data within Snowflake.Common categories of functions available include:
- AI/Cortex Functions: For LLM-based tasks like
ai_complete,ai_summarize_agg, andai_sentiment. - Array/Object/JSON Functions: For handling semi-structured data using functions like
array_construct,parse_json, andget. - Geospatial Functions: For spatial analysis using
st_prefixed functions likest_distanceandst_intersects. - H3 Functions: For hexagonal hierarchical geospatial indexing.
- Mathematical & Statistical Functions: Such as
abs,avg,stddev, andcorr. - Date & Time Functions: For temporal manipulation like
date_trunc,datediff, andcurrent_timestamp. - Window Functions: For analytical queries using
rank,row_number,lead, andlag. - String Functions: For text processing like
concat,substring, andregexp_replace. - User-Defined Function (UDF) Wrappers: To register and call Python UDFs, UDTFs, and UDAFs.
- AI/Cortex Functions: For LLM-based tasks like
Supported general functions in Snowpark pandas (Modin)
mainSnowpark pandas (via Modin) supports a variety of general-purpose data manipulation and utility functions. These functions are categorized into data manipulation, missing data handling, numeric processing, datetime processing, and hybrid execution control.
### Data manipulations - `melt` - `crosstab` - `pivot` - `pivot_table` - `cut` - `qcut` - `concat` - `get_dummies` - `merge` - `merge_asof` - `unique` ### Top-level missing data - `isna` - `isnull` - `notna` - `notnull` ### Top-level dealing with numeric data - `to_numeric` ### Top-level dealing with datetimelike data - `date_range` - `bdate_range` - `to_datetime` ### Hybrid Execution functions - `explain_switch`Supported GroupBy APIs in Modin pandas
mainThe
modin.pandas.groupbymodule provides a pandas-compatible API for grouping data. The supported operations are categorized into indexing/iteration, function application, and descriptive statistics for bothDataFrameGroupByandSeriesGroupByobjects.For a complete list of specific method signatures and behaviors, refer to the individual API documentation for each method.
View Snowpark pandas supported APIs
mainSnowpark pandas provides a subset of the pandas API implemented for Snowflake. Because new methods are merged into release branches continuously, the list of supported APIs is subject to change. To ensure you are using the correct documentation for your specific installation, always verify that you are viewing the documentation for the stable version that matches your installed library version.
The supported API surface is organized into several categories:
- General pandas APIs
- Series methods
- DataFrame methods
- Index methods (including DateTime, Timedelta, and specialized index types)
- Window functions
- GroupBy operations
- Resampling operations
- Series-specific accessor methods (e.g.,
.dtand.str)
Use the Snowpark Catalog module to manage Snowflake objects
mainThesnowflake.snowpark.catalog.Catalogmodule provides a set of methods to inspect, manage, and manipulate the Snowflake catalog. You can use it to check for the existence of objects (databases, schemas, tables, views, procedures, and UDFs), list metadata, and perform destructive operations like dropping objects. Most methods follow a snake_case naming convention (e.g.,list_tables) but also support CamelCase aliases (e.g.,listTables).Understand Snowpark implementation flags for window functions
mainWhen using Snowpark pandas, window function support is indicated by specific flags. Note that
engineandengine_kwargsare always ignored in Snowpark pandas; the execution engine is always Snowflake.Implementation Flags:
Y: Supported via distributed implementation.N: Not supported; the API will error out.P: Partial support; some parameters may not be supported.D: Defaults to single-node pandas execution via UDF/Sproc.
Use the Table class in Snowpark Python
mainThe
snowflake.snowpark.Tableclass represents a Snowflake table. It provides methods to perform DML (Data Manipulation Language) and DDL (Data Definition Language) operations directly on the table, such as deleting rows, updating rows, merging data, or dropping the table entirely.from snowflake.snowpark import Table # Assuming 'session' is an active Snowpark session table = session.table("MY_TABLE_NAME")Use CaseExpr for conditional logic
mainThe
CaseExprclass allows you to buildCASEexpressions in SQL using a fluent API. You can chain.when()calls to define multiple conditions and use.otherwise()to define the fallback value when no conditions are met.# Example pattern for CaseExpr from snowflake.snowpark.functions import col # Logic: CASE WHEN condition1 THEN result1 WHEN condition2 THEN result2 ELSE result_else END expr = col("MY_COL").when(col("MY_COL") > 10, "High").otherwise("Low")Use the Row class in Snowpark Python
mainThe
Rowclass insnowflake.snowparkrepresents a single record from a Snowpark DataFrame. It allows you to access individual field values by name or index. Common operations include converting the row to a dictionary or retrieving its index.from snowflake.snowpark import Row # Example of creating a Row manually row = Row({'col1': 1, 'col2': 'A'}) print(row['col1']) # Access by name print(row[1]) # Access by index