Skip to content

Commit 6a32f10

Browse files
chromyjreback
chromy
authored andcommitted
ENH: service account credentials support + optional verbosity
closes pandas-dev#11881 closes pandas-dev#8489
1 parent 357729d commit 6a32f10

File tree

5 files changed

+489
-102
lines changed

5 files changed

+489
-102
lines changed

doc/source/io.rst

+30-7
Original file line numberDiff line numberDiff line change
@@ -4093,6 +4093,34 @@ The key functions are:
40934093

40944094
.. _io.bigquery_reader:
40954095

4096+
4097+
Authentication
4098+
''''''''''''''
4099+
4100+
Authentication is possible with either user account credentials or service account credentials.
4101+
4102+
Authenticating with user account credentials is as simple as following the prompts in a browser window
4103+
which will be automatically opened for you. You will be authenticated to the specified
4104+
``BigQuery`` account via Google's ``Oauth2`` mechanism. Additional information on the
4105+
authentication mechanism can be found `here <https://developers.google.com/identity/protocols/OAuth2#clientside/>`__.
4106+
4107+
Authentication with service account credentials is possible via the `'private_key'` parameter. This method
4108+
is particularly useful when working on remote servers (eg. jupyter iPython notebook on remote host).
4109+
The remote authentication using user account credentials is not currently supported in Pandas.
4110+
Additional information on service accounts can be found
4111+
`here <https://developers.google.com/identity/protocols/OAuth2#serviceaccount>`__.
4112+
4113+
.. note::
4114+
4115+
The `'private_key'` parameter can be set to either the file path of the service account key
4116+
in JSON format, or key contents of the service account key in JSON format.
4117+
4118+
.. note::
4119+
4120+
A private key can be obtained from the Google developers console by clicking
4121+
`here <https://console.developers.google.com/permissions/serviceaccounts>`__. Use JSON key type.
4122+
4123+
40964124
Querying
40974125
''''''''
40984126

@@ -4107,12 +4135,6 @@ into a DataFrame using the :func:`~pandas.io.gbq.read_gbq` function.
41074135
41084136
data_frame = pd.read_gbq('SELECT * FROM test_dataset.test_table', projectid)
41094137
4110-
You will then be authenticated to the specified BigQuery account
4111-
via Google's Oauth2 mechanism. In general, this is as simple as following the
4112-
prompts in a browser window which will be opened for you. Should the browser not
4113-
be available, or fail to launch, a code will be provided to complete the process
4114-
manually. Additional information on the authentication mechanism can be found
4115-
`here <https://developers.google.com/accounts/docs/OAuth2#clientside/>`__.
41164138
41174139
You can define which column from BigQuery to use as an index in the
41184140
destination DataFrame as well as a preferred column order as follows:
@@ -4125,7 +4147,7 @@ destination DataFrame as well as a preferred column order as follows:
41254147
41264148
.. note::
41274149

4128-
You can find your project id in the `BigQuery management console <https://code.google.com/apis/console/b/0/?noredirect>`__.
4150+
You can find your project id in the `Google developers console <https://console.developers.google.com>`__.
41294151

41304152

41314153
.. note::
@@ -4134,6 +4156,7 @@ destination DataFrame as well as a preferred column order as follows:
41344156

41354157
.. _io.bigquery_writer:
41364158

4159+
41374160
Writing DataFrames
41384161
''''''''''''''''''
41394162

doc/source/whatsnew/v0.18.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,7 @@ Other enhancements
201201
- ``DataFrame.quantile`` and ``Series.quantile`` now accept ``interpolation`` keyword (:issue:`10174`).
202202
- ``DataFrame.select_dtypes`` now allows the ``np.float16`` typecode (:issue:`11990`)
203203
- ``pivot_table()`` now accepts most iterables for the ``values`` parameter (:issue:`12017`)
204+
- Added Google ``BigQuery`` service account authentication support, which enables authentication on remote servers. (:issue:`11881`). For further details see :ref:`here <io.bigquery_authentication>`
204205

205206
.. _whatsnew_0180.api_breaking:
206207

pandas/core/frame.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -859,7 +859,7 @@ def to_dict(self, orient='dict'):
859859
raise ValueError("orient '%s' not understood" % orient)
860860

861861
def to_gbq(self, destination_table, project_id, chunksize=10000,
862-
verbose=True, reauth=False, if_exists='fail'):
862+
verbose=True, reauth=False, if_exists='fail', private_key=None):
863863
"""Write a DataFrame to a Google BigQuery table.
864864
865865
THIS IS AN EXPERIMENTAL LIBRARY
@@ -883,14 +883,18 @@ def to_gbq(self, destination_table, project_id, chunksize=10000,
883883
'fail': If table exists, do nothing.
884884
'replace': If table exists, drop it, recreate it, and insert data.
885885
'append': If table exists, insert data. Create if does not exist.
886+
private_key : str (optional)
887+
Service account private key in JSON format. Can be file path
888+
or string contents. This is useful for remote server
889+
authentication (eg. jupyter iPython notebook on remote host)
886890
887891
.. versionadded:: 0.17.0
888892
"""
889893

890894
from pandas.io import gbq
891895
return gbq.to_gbq(self, destination_table, project_id=project_id,
892896
chunksize=chunksize, verbose=verbose, reauth=reauth,
893-
if_exists=if_exists)
897+
if_exists=if_exists, private_key=private_key)
894898

895899
@classmethod
896900
def from_records(cls, data, index=None, exclude=None, columns=None,

0 commit comments

Comments
 (0)