Skip to content

Commit cccc6ca

Browse files
mattyoplMatthew Laurence Chenchelsea-lin
authored
feat: allow setting table labels in to_gbq (#941)
* chore: allow setting table labels in `to_gbq` --------- Co-authored-by: Matthew Laurence Chen <[email protected]> Co-authored-by: Chelsea Lin <[email protected]>
1 parent 27764a6 commit cccc6ca

File tree

3 files changed

+26
-3
lines changed

3 files changed

+26
-3
lines changed

bigframes/dataframe.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3027,6 +3027,7 @@ def to_gbq(
30273027
index: bool = True,
30283028
ordering_id: Optional[str] = None,
30293029
clustering_columns: Union[pandas.Index, Iterable[typing.Hashable]] = (),
3030+
labels: dict[str, str] = {},
30303031
) -> str:
30313032
temp_table_ref = None
30323033

@@ -3081,9 +3082,11 @@ def to_gbq(
30813082
export_array, id_overrides = self._prepare_export(
30823083
index=index and self._has_index, ordering_id=ordering_id
30833084
)
3084-
destination = bigquery.table.TableReference.from_string(
3085-
destination_table,
3086-
default_project=default_project,
3085+
destination: bigquery.table.TableReference = (
3086+
bigquery.table.TableReference.from_string(
3087+
destination_table,
3088+
default_project=default_project,
3089+
)
30873090
)
30883091
_, query_job = self._session._export(
30893092
export_array,
@@ -3106,6 +3109,11 @@ def to_gbq(
31063109
+ constants.DEFAULT_EXPIRATION,
31073110
)
31083111

3112+
if len(labels) != 0:
3113+
table = bigquery.Table(result_table)
3114+
table.labels = labels
3115+
self._session.bqclient.update_table(table, ["labels"])
3116+
31093117
return destination_table
31103118

31113119
def to_numpy(

tests/system/small/test_dataframe.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4657,6 +4657,17 @@ def test_to_gbq_and_create_dataset(session, scalars_df_index, dataset_id_not_cre
46574657
assert not loaded_scalars_df_index.empty
46584658

46594659

4660+
def test_to_gbq_table_labels(scalars_df_index):
4661+
destination_table = "bigframes-dev.bigframes_tests_sys.table_labels"
4662+
result_table = scalars_df_index.to_gbq(
4663+
destination_table, labels={"test": "labels"}, if_exists="replace"
4664+
)
4665+
client = scalars_df_index._session.bqclient
4666+
table = client.get_table(result_table)
4667+
assert table.labels
4668+
assert table.labels["test"] == "labels"
4669+
4670+
46604671
@pytest.mark.parametrize(
46614672
("col_names", "ignore_index"),
46624673
[

third_party/bigframes_vendored/pandas/core/frame.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,7 @@ def to_gbq(
390390
index: bool = True,
391391
ordering_id: Optional[str] = None,
392392
clustering_columns: Union[pd.Index, Iterable[Hashable]] = (),
393+
labels: dict[str, str] = {},
393394
) -> str:
394395
"""Write a DataFrame to a BigQuery table.
395396
@@ -467,6 +468,9 @@ def to_gbq(
467468
clustering order within the Index/DataFrame columns follows the order
468469
specified in `clustering_columns`.
469470
471+
labels (dict[str, str], default None):
472+
Specifies table labels within BigQuery
473+
470474
Returns:
471475
str:
472476
The fully-qualified ID for the written table, in the form

0 commit comments

Comments
 (0)