|
| 1 | +# Copyright 2025 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | + |
| 16 | +def test_performance_optimizations() -> None: |
| 17 | + # [START bigquery_bigframes_use_peek_to_preview_data] |
| 18 | + import bigframes.pandas as bpd |
| 19 | + |
| 20 | + # Read the "Penguins" table into a dataframe |
| 21 | + df = bpd.read_gbq("bigquery-public-data.ml_datasets.penguins") |
| 22 | + |
| 23 | + # Preview 3 random rows |
| 24 | + df.peek(3) |
| 25 | + # [END bigquery_bigframes_use_peek_to_preview_data] |
| 26 | + assert df.peek(3) is not None |
| 27 | + |
| 28 | + import bigframes.pandas as bpd |
| 29 | + |
| 30 | + users = bpd.DataFrame({"user_name": ["John"]}) |
| 31 | + groups = bpd.DataFrame({"group_id": ["group_1"]}) |
| 32 | + transactions = bpd.DataFrame({"amount": [3], "completed": [True]}) |
| 33 | + |
| 34 | + # [START bigquery_bigframes_use_cache_after_expensive_operations] |
| 35 | + # Assume you have 3 large dataframes "users", "group" and "transactions" |
| 36 | + |
| 37 | + # Expensive join operations |
| 38 | + final_df = users.join(groups).join(transactions) |
| 39 | + final_df.cache() |
| 40 | + # Subsequent derived results will reuse the cached join |
| 41 | + print(final_df.peek()) |
| 42 | + print(len(final_df[final_df["completed"]])) |
| 43 | + print(final_df.groupby("group_id")["amount"].mean().peek(30)) |
| 44 | + # [END bigquery_bigframes_use_cache_after_expensive_operations] |
| 45 | + assert final_df is not None |
| 46 | + |
| 47 | + # [START bigquery_bigframes_enable_deferred_repr_for_debugging] |
| 48 | + import bigframes.pandas as bpd |
| 49 | + |
| 50 | + bpd.options.display.repr_mode = "deferred" |
| 51 | + # [END bigquery_bigframes_enable_deferred_repr_for_debugging] |
| 52 | + assert bpd.options.display.repr_mode == "deferred" |
0 commit comments