@@ -269,6 +269,7 @@ def __init__(
269269 client_secret = None ,
270270 user_agent = None ,
271271 rfc9110_delimiter = False ,
272+ bigquery_client = None ,
272273 ):
273274 global context
274275 from google .api_core .exceptions import ClientError , GoogleAPIError
@@ -288,6 +289,14 @@ def __init__(
288289 self .client_secret = client_secret
289290 self .user_agent = user_agent
290291 self .rfc9110_delimiter = rfc9110_delimiter
292+ self .use_bqstorage_api = use_bqstorage_api
293+
294+ if bigquery_client is not None :
295+ # If a bq client is already provided, use it to populate auth fields.
296+ self .project_id = bigquery_client .project
297+ self .credentials = bigquery_client ._credentials
298+ self .client = bigquery_client
299+ return
291300
292301 default_project = None
293302
@@ -325,8 +334,9 @@ def __init__(
325334 if context .project is None :
326335 context .project = self .project_id
327336
328- self .client = self .get_client ()
329- self .use_bqstorage_api = use_bqstorage_api
337+ self .client = _get_client (
338+ self .user_agent , self .rfc9110_delimiter , self .project_id , self .credentials
339+ )
330340
331341 def _start_timer (self ):
332342 self .start = time .time ()
@@ -702,6 +712,7 @@ def read_gbq(
702712 client_secret = None ,
703713 * ,
704714 col_order = None ,
715+ bigquery_client = None ,
705716):
706717 r"""Read data from Google BigQuery to a pandas DataFrame.
707718
@@ -849,6 +860,9 @@ def read_gbq(
849860 the user is attempting to connect to.
850861 col_order : list(str), optional
851862 Alias for columns, retained for backwards compatibility.
863+ bigquery_client : google.cloud.bigquery.Client, optional
864+ A Google Cloud BigQuery Python Client instance. If provided, it will be used for reading
865+ data, while the project and credentials parameters will be ignored.
852866
853867 Returns
854868 -------
@@ -900,6 +914,7 @@ def read_gbq(
900914 auth_redirect_uri = auth_redirect_uri ,
901915 client_id = client_id ,
902916 client_secret = client_secret ,
917+ bigquery_client = bigquery_client ,
903918 )
904919
905920 if _is_query (query_or_table ):
@@ -971,6 +986,7 @@ def to_gbq(
971986 client_secret = None ,
972987 user_agent = None ,
973988 rfc9110_delimiter = False ,
989+ bigquery_client = None ,
974990):
975991 """Write a DataFrame to a Google BigQuery table.
976992
@@ -1087,6 +1103,9 @@ def to_gbq(
10871103 rfc9110_delimiter : bool
10881104 Sets user agent delimiter to a hyphen or a slash.
10891105 Default is False, meaning a hyphen will be used.
1106+ bigquery_client : google.cloud.bigquery.Client, optional
1107+ A Google Cloud BigQuery Python Client instance. If provided, it will be used for reading
1108+ data, while the project, user_agent, and credentials parameters will be ignored.
10901109
10911110 .. versionadded:: 0.23.3
10921111 """
@@ -1157,6 +1176,7 @@ def to_gbq(
11571176 client_secret = client_secret ,
11581177 user_agent = user_agent ,
11591178 rfc9110_delimiter = rfc9110_delimiter ,
1179+ bigquery_client = bigquery_client ,
11601180 )
11611181 bqclient = connector .client
11621182
@@ -1492,3 +1512,22 @@ def create_user_agent(
14921512 user_agent = f"{ user_agent } { identity } "
14931513
14941514 return user_agent
1515+
1516+
1517+ def _get_client (user_agent , rfc9110_delimiter , project_id , credentials ):
1518+ import google .api_core .client_info
1519+
1520+ bigquery = FEATURES .bigquery_try_import ()
1521+
1522+ user_agent = create_user_agent (
1523+ user_agent = user_agent , rfc9110_delimiter = rfc9110_delimiter
1524+ )
1525+
1526+ client_info = google .api_core .client_info .ClientInfo (
1527+ user_agent = user_agent ,
1528+ )
1529+ return bigquery .Client (
1530+ project = project_id ,
1531+ credentials = credentials ,
1532+ client_info = client_info ,
1533+ )
0 commit comments