> For the complete documentation index, see [llms.txt](https://docs.predicthq.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.predicthq.com/integrations/third-party-integrations/snowflake/snowflake-data-science-guide/snowpark-method-guide.md). Agents can also search and read this documentation through its [MCP server](https://docs.predicthq.com/~gitbook/mcp).

# Snowpark method guide

Transforming Event Data into ML-Ready Features using Snowpark and Python

## Calling the Features API from Snowflake to calculate features

There are two options for calculating features in Snowflake:

1. Call the Features API using [Snowpark](https://docs.snowflake.com/en/developer-guide/snowpark/python/index)
2. Use the [Python Connector ](https://docs.snowflake.com/en/developer-guide/python-connector/python-connector)or other libraries.

If you take either of the earlier approaches, you can call the Features API for each location and save the results into a table instead of using SQL. These options skip the maintenance of SQL and is the recommended approach if possible.

The following is an example of calling the Features API with Python in Snowpark. This uses the PredictHQ Python SDK. It loops over the **SAVED\_LOCATIONS** table, calls the Features API using the SDK, and outputs the results into a table. So, it achieves a similar result to the SQL method but using the API. Modify this code to include the features you want to fetch.

This code is an example, not production-ready. Test and optimize it before you use it.

### Python code

The following code calls the Features API for each location in the **SAVED\_LOCATIONS** table:

{% code fullWidth="true" %}

```python
from snowflake.snowpark import Session
from predicthq import Client
import pandas as pd

# Snowflake connection parameters
connection_parameters = {
  "account": "<your_account>",
  "user": "<your_username>",
  "password": "<your_password>",
  "role": "<your_role>",
  "warehouse": "<your_warehouse>",
  "database": "<your_database>",
  "schema": "<your_schema>"
}

# Create a session
session = Session.builder.configs(connection_parameters).create()

# PredictHQ Client setup
phq = Client(access_token="your_predicthq_access_token")

# Fetch location data from the Snowflake table
locations_df = session.table("SAVED_LOCATIONS").to_pandas()

# Prepare a DataFrame to collect all data
all_data = []

# Iterate over each row in the location DataFrame
for index, location in locations_df.iterrows():
    # Prepare the radius in the appropriate unit
    radius_with_unit = f"{location['RADIUS']}{location['RADIUS_UNIT']}"

    # Call the Features API for each location
    for feature in phq.features.obtain_features(
            active__gte=str(location['DATE_START']),
            active__lte=str(location['DATE_END']),
            location__geo={
                "lon": location['LON'],
                "lat": location['LAT'],
                "radius": radius_with_unit
            },
            phq_attendance_sports__stats=["sum"],
            phq_attendance_conferences__stats=["sum"]

 # add more ML features here like phq_attendance_community, phq_attendance_concerts,
 # phq_attendance_expos, phq_attendance_festivals, phq_attendance_performing_arts,
 # and so on.

    ):
                data_point = {
            'location': location['LOCATION'],
            'date': feature.date,
            'phq_attendance_conferences': getattr(feature.phq_attendance_conferences.stats, 'sum', 0),
            'phq_attendance_sports': getattr(feature.phq_attendance_sports.stats, 'sum', 0)
        }
        all_data.append(data_point)

# Convert all collected data to a DataFrame
results_df = pd.DataFrame(all_data)

# Convert the DataFrame to a Snowpark DataFrame
snow_df = session.create_dataframe(results_df)

# Append the results to the existing Snowflake table
snow_df.write.mode("append").save_as_table("ML_FEATURES_FOR_LOCATIONS")

# Print the contents of the table to verify
print(session.table("ML_FEATURES_FOR_LOCATIONS").show())

# Close the session
session.close()

```

{% endcode %}

### Table output

The output of the Python script should look similar to the following table:

<table data-full-width="true"><thead><tr><th width="226">location</th><th width="178">date</th><th width="321" data-type="number">phq_attendance_conferences</th><th data-type="number">phq_attendance_sports</th></tr></thead><tbody><tr><td>store1-chicago</td><td>2024-01-16</td><td>231</td><td>19329</td></tr><tr><td>store1-chicago</td><td>2024-01-17</td><td>666</td><td>12312</td></tr><tr><td>store1-chicago</td><td>2024-01-18</td><td>215</td><td>0</td></tr><tr><td>store1-chicago</td><td>2024-01-19</td><td>87</td><td>23246</td></tr><tr><td>store1-chicago</td><td>2024-01-20</td><td>395</td><td>19448</td></tr></tbody></table>

### Refer back to [main guide](https://docs.predicthq.com/integrations/third-party-integrations/snowflake/snowflake-data-science-guide.md)


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.predicthq.com/integrations/third-party-integrations/snowflake/snowflake-data-science-guide/snowpark-method-guide.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
