Skip to content

Commit e035747

Browse files
author
chromy
committed
gbq: service account credentials support + optional verbosity
1 parent d77f072 commit e035747

File tree

5 files changed

+486
-95
lines changed

5 files changed

+486
-95
lines changed

doc/source/io.rst

+29-7
Original file line numberDiff line numberDiff line change
@@ -4093,6 +4093,33 @@ 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 in JSON format, or
4116+
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 `here <https://console.developers.google.com/permissions/serviceaccounts>`__. Use JSON key type.
4121+
4122+
40964123
Querying
40974124
''''''''
40984125

@@ -4107,12 +4134,6 @@ into a DataFrame using the :func:`~pandas.io.gbq.read_gbq` function.
41074134
41084135
data_frame = pd.read_gbq('SELECT * FROM test_dataset.test_table', projectid)
41094136
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/>`__.
41164137
41174138
You can define which column from BigQuery to use as an index in the
41184139
destination DataFrame as well as a preferred column order as follows:
@@ -4125,7 +4146,7 @@ destination DataFrame as well as a preferred column order as follows:
41254146
41264147
.. note::
41274148

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

41304151

41314152
.. note::
@@ -4134,6 +4155,7 @@ destination DataFrame as well as a preferred column order as follows:
41344155

41354156
.. _io.bigquery_writer:
41364157

4158+
41374159
Writing DataFrames
41384160
''''''''''''''''''
41394161

doc/source/whatsnew/v0.18.0.txt

+3
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,9 @@ Other enhancements
152152
- ``Series`` gained an ``is_unique`` attribute (:issue:`11946`)
153153
- ``DataFrame.quantile`` and ``Series.quantile`` now accept ``interpolation`` keyword (:issue:`10174`).
154154
- ``DataFrame.select_dtypes`` now allows the ``np.float16`` typecode (:issue:`11990`)
155+
- Added Google ``BigQuery`` service account authentication support, which enables authentication on remote servers. (:issue:`11881`)
156+
For further details see :ref:`here <io.bigquery_authentication>`
157+
155158

156159
.. _whatsnew_0180.enhancements.rounding:
157160

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)