@@ -236,6 +236,7 @@ def __init__(
236
236
dialect = "legacy" ,
237
237
location = None ,
238
238
try_credentials = None ,
239
+ credentials = None ,
239
240
):
240
241
global context
241
242
from google .api_core .exceptions import GoogleAPIError
@@ -249,11 +250,14 @@ def __init__(
249
250
self .private_key = private_key
250
251
self .auth_local_webserver = auth_local_webserver
251
252
self .dialect = dialect
253
+ self .credentials = credentials
252
254
self .credentials_path = _get_credentials_file ()
255
+ default_project = None
253
256
254
257
# Load credentials from cache.
255
- self .credentials = context .credentials
256
- default_project = context .project
258
+ if not self .credentials :
259
+ self .credentials = context .credentials
260
+ default_project = context .project
257
261
258
262
# Credentials were explicitly asked for, so don't use the cache.
259
263
if private_key or reauth or not self .credentials :
@@ -563,7 +567,7 @@ def schema_is_subset(self, dataset_id, table_id, schema):
563
567
564
568
def delete_and_recreate_table (self , dataset_id , table_id , table_schema ):
565
569
table = _Table (
566
- self .project_id , dataset_id , private_key = self .private_key
570
+ self .project_id , dataset_id , credentials = self .credentials
567
571
)
568
572
table .delete (table_id )
569
573
table .create (table_id , table_schema )
@@ -621,12 +625,13 @@ def read_gbq(
621
625
index_col = None ,
622
626
col_order = None ,
623
627
reauth = False ,
624
- private_key = None ,
625
628
auth_local_webserver = False ,
626
629
dialect = None ,
627
630
location = None ,
628
631
configuration = None ,
632
+ credentials = None ,
629
633
verbose = None ,
634
+ private_key = None ,
630
635
):
631
636
r"""Load data from Google BigQuery using google-cloud-python
632
637
@@ -655,10 +660,6 @@ def read_gbq(
655
660
reauth : boolean, default False
656
661
Force Google BigQuery to re-authenticate the user. This is useful
657
662
if multiple accounts are used.
658
- private_key : str, optional
659
- Service account private key in JSON format. Can be file path
660
- or string contents. This is useful for remote server
661
- authentication (eg. Jupyter/IPython notebook on remote host).
662
663
auth_local_webserver : boolean, default False
663
664
Use the `local webserver flow`_ instead of the `console flow`_
664
665
when getting user credentials.
@@ -699,10 +700,28 @@ def read_gbq(
699
700
700
701
For more information see `BigQuery REST API Reference
701
702
<https://cloud.google.com/bigquery/docs/reference/rest/v2/jobs#configuration.query>`__.
703
+ credentials : google.auth.credentials.Credentials, optional
704
+ Credentials for accessing Google APIs. Use this parameter to override
705
+ default credentials, such as to use Compute Engine
706
+ :class:`google.auth.compute_engine.Credentials` or Service Account
707
+ :class:`google.oauth2.service_account.Credentials` directly.
708
+
709
+ .. versionadded:: 0.8.0
702
710
verbose : None, deprecated
703
711
Deprecated in Pandas-GBQ 0.4.0. Use the `logging module
704
712
to adjust verbosity instead
705
713
<https://pandas-gbq.readthedocs.io/en/latest/intro.html#logging>`__.
714
+ private_key : str, deprecated
715
+ Deprecated in pandas-gbq version 0.8.0. Use the ``credentials``
716
+ parameter and
717
+ :func:`google.oauth2.service_account.Credentials.from_service_account_info`
718
+ or
719
+ :func:`google.oauth2.service_account.Credentials.from_service_account_file`
720
+ instead.
721
+
722
+ Service account private key in JSON format. Can be file path
723
+ or string contents. This is useful for remote server
724
+ authentication (eg. Jupyter/IPython notebook on remote host).
706
725
707
726
Returns
708
727
-------
@@ -736,10 +755,11 @@ def read_gbq(
736
755
connector = GbqConnector (
737
756
project_id ,
738
757
reauth = reauth ,
739
- private_key = private_key ,
740
758
dialect = dialect ,
741
759
auth_local_webserver = auth_local_webserver ,
742
760
location = location ,
761
+ credentials = credentials ,
762
+ private_key = private_key ,
743
763
)
744
764
schema , rows = connector .run_query (query , configuration = configuration )
745
765
final_df = _parse_data (schema , rows )
@@ -779,12 +799,13 @@ def to_gbq(
779
799
chunksize = None ,
780
800
reauth = False ,
781
801
if_exists = "fail" ,
782
- private_key = None ,
783
802
auth_local_webserver = False ,
784
803
table_schema = None ,
785
804
location = None ,
786
805
progress_bar = True ,
806
+ credentials = None ,
787
807
verbose = None ,
808
+ private_key = None ,
788
809
):
789
810
"""Write a DataFrame to a Google BigQuery table.
790
811
@@ -822,10 +843,6 @@ def to_gbq(
822
843
If table exists, drop it, recreate it, and insert data.
823
844
``'append'``
824
845
If table exists, insert data. Create if does not exist.
825
- private_key : str, optional
826
- Service account private key in JSON format. Can be file path
827
- or string contents. This is useful for remote server
828
- authentication (eg. Jupyter/IPython notebook on remote host).
829
846
auth_local_webserver : bool, default False
830
847
Use the `local webserver flow`_ instead of the `console flow`_
831
848
when getting user credentials.
@@ -861,10 +878,28 @@ def to_gbq(
861
878
chunk by chunk.
862
879
863
880
.. versionadded:: 0.5.0
881
+ credentials : google.auth.credentials.Credentials, optional
882
+ Credentials for accessing Google APIs. Use this parameter to override
883
+ default credentials, such as to use Compute Engine
884
+ :class:`google.auth.compute_engine.Credentials` or Service Account
885
+ :class:`google.oauth2.service_account.Credentials` directly.
886
+
887
+ .. versionadded:: 0.8.0
864
888
verbose : bool, deprecated
865
889
Deprecated in Pandas-GBQ 0.4.0. Use the `logging module
866
890
to adjust verbosity instead
867
891
<https://pandas-gbq.readthedocs.io/en/latest/intro.html#logging>`__.
892
+ private_key : str, deprecated
893
+ Deprecated in pandas-gbq version 0.8.0. Use the ``credentials``
894
+ parameter and
895
+ :func:`google.oauth2.service_account.Credentials.from_service_account_info`
896
+ or
897
+ :func:`google.oauth2.service_account.Credentials.from_service_account_file`
898
+ instead.
899
+
900
+ Service account private key in JSON format. Can be file path
901
+ or string contents. This is useful for remote server
902
+ authentication (eg. Jupyter/IPython notebook on remote host).
868
903
"""
869
904
870
905
_test_google_api_imports ()
@@ -889,21 +924,21 @@ def to_gbq(
889
924
connector = GbqConnector (
890
925
project_id ,
891
926
reauth = reauth ,
892
- private_key = private_key ,
893
927
auth_local_webserver = auth_local_webserver ,
894
928
location = location ,
895
929
# Avoid reads when writing tables.
896
930
# https://github.com/pydata/pandas-gbq/issues/202
897
931
try_credentials = lambda project , creds : creds ,
932
+ credentials = credentials ,
933
+ private_key = private_key ,
898
934
)
899
935
dataset_id , table_id = destination_table .rsplit ("." , 1 )
900
936
901
937
table = _Table (
902
938
project_id ,
903
939
dataset_id ,
904
- reauth = reauth ,
905
- private_key = private_key ,
906
940
location = location ,
941
+ credentials = connector .credentials ,
907
942
)
908
943
909
944
if not table_schema :
@@ -980,12 +1015,17 @@ def __init__(
980
1015
project_id ,
981
1016
dataset_id ,
982
1017
reauth = False ,
983
- private_key = None ,
984
1018
location = None ,
1019
+ credentials = None ,
1020
+ private_key = None ,
985
1021
):
986
1022
self .dataset_id = dataset_id
987
1023
super (_Table , self ).__init__ (
988
- project_id , reauth , private_key , location = location
1024
+ project_id ,
1025
+ reauth ,
1026
+ location = location ,
1027
+ credentials = credentials ,
1028
+ private_key = private_key ,
989
1029
)
990
1030
991
1031
def exists (self , table_id ):
@@ -1031,12 +1071,12 @@ def create(self, table_id, schema):
1031
1071
"Table {0} already " "exists" .format (table_id )
1032
1072
)
1033
1073
1034
- if not _Dataset (self .project_id , private_key = self .private_key ).exists (
1074
+ if not _Dataset (self .project_id , credentials = self .credentials ).exists (
1035
1075
self .dataset_id
1036
1076
):
1037
1077
_Dataset (
1038
1078
self .project_id ,
1039
- private_key = self .private_key ,
1079
+ credentials = self .credentials ,
1040
1080
location = self .location ,
1041
1081
).create (self .dataset_id )
1042
1082
@@ -1084,10 +1124,19 @@ def delete(self, table_id):
1084
1124
1085
1125
class _Dataset (GbqConnector ):
1086
1126
def __init__ (
1087
- self , project_id , reauth = False , private_key = None , location = None
1127
+ self ,
1128
+ project_id ,
1129
+ reauth = False ,
1130
+ location = None ,
1131
+ credentials = None ,
1132
+ private_key = None ,
1088
1133
):
1089
1134
super (_Dataset , self ).__init__ (
1090
- project_id , reauth , private_key , location = location
1135
+ project_id ,
1136
+ reauth ,
1137
+ credentials = credentials ,
1138
+ location = location ,
1139
+ private_key = private_key ,
1091
1140
)
1092
1141
1093
1142
def exists (self , dataset_id ):
0 commit comments