Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 68
feat: Add BigFrames.bigquery.st_regionstats method#2200
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
03a153cab4607892c69aa8b681c021243e4459c5b2350d241b4db49ca6805269775fb6bcd404701edd21bf000553c172bb13a67bd6a4b1c9745211e4252e90b44d52018a6c761b007b92483e80c8eb2666d436ffad021b77a57dddFile filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| # Copyright 2025 Google LLC | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
| def test_st_regionstats(): | ||
| project_id = "bigframes-dev" | ||
| # [START bigquery_dataframes_st_regionstats] | ||
| from typing import cast | ||
| import bigframes.bigquery as bq | ||
CollaboratorAuthor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's use bbq instead of bq. | ||
| import bigframes.pandas as bpd | ||
| # TODO: Set the project_id to your Google Cloud project ID. | ||
| # project_id = "your-project-id" | ||
| # | ||
| # TODO: Set the dataset_id to the ID of the dataset that contains the | ||
| # `climate` table. This is likely a linked dataset to Earth Engine. | ||
| # See: https://cloud.google.com/bigquery/docs/link-earth-engine | ||
| linked_dataset = "era5_land_daily_aggregated" | ||
| # Load the table of country boundaries. | ||
| bpd.options.bigquery.project = project_id | ||
| countries = bpd.read_gbq("bigquery-public-data.overture_maps.division_area") | ||
| # Filter to just the countries. | ||
| countries = countries[countries["subtype"] == "country"].copy() | ||
| countries["name"] = countries["names"].struct.field("primary") | ||
| # TODO: Add st_simplify when it is available in BigFrames. | ||
| # https://github.com/googleapis/python-bigquery-dataframes/issues/1497 | ||
| # countries["simplified_geometry"] = bq.st_simplify(countries["geometry"], 10000) | ||
Comment on lines
+41
to
+43
CollaboratorAuthor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd like to add | ||
| countries["simplified_geometry"] = countries["geometry"] | ||
| # Get the reference to the temperature data from a linked dataset. | ||
| # Note: This sample assumes you have a linked dataset to Earth Engine. | ||
| # See: https://cloud.google.com/bigquery/docs/link-earth-engine | ||
| image_href = bpd.read_gbq(f"{project_id}.{linked_dataset}.climate").where( | ||
| lambda df: df["start_datetime"] == "2025-01-01 00:00:00" | ||
| ) | ||
| raster_id = image_href["assets"].struct.field("image").struct.field("href").item | ||
| stats = bq.st_regionstats( | ||
| countries["simplified_geometry"], | ||
| raster_id=cast(str, raster_id), | ||
| band="temperature_2m", | ||
| ) | ||
| # Extract the mean and convert from Kelvin to Celsius. | ||
| countries["mean_temperature"] = stats.struct.field("mean") - 273.15 | ||
| # Sort by the mean temperature to find the warmest countries. | ||
| result = countries[["name", "mean_temperature"]].sort_values( | ||
| "mean_temperature", ascending=False | ||
| ) | ||
| print(result.head()) | ||
| # [END bigquery_dataframes_st_regionstats] | ||
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.