Skip to content

Commit db382b7

Browse files
tswasttm9k1
authored andcommitted
ENH: update pandas-gbq to 0.8.0, adds credentials arg (pandas-dev#23662)
1 parent cd637ec commit db382b7

File tree

5 files changed

+88
-39
lines changed

5 files changed

+88
-39
lines changed

doc/source/install.rst

+3-1
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,9 @@ Optional Dependencies
286286
`xsel <http://www.vergenet.net/~conrad/software/xsel/>`__, or
287287
`xclip <https://github.com/astrand/xclip/>`__: necessary to use
288288
:func:`~pandas.read_clipboard`. Most package managers on Linux distributions will have ``xclip`` and/or ``xsel`` immediately available for installation.
289-
* `pandas-gbq <https://pandas-gbq.readthedocs.io/en/latest/install.html#dependencies>`__: for Google BigQuery I/O.
289+
* `pandas-gbq
290+
<https://pandas-gbq.readthedocs.io/en/latest/install.html#dependencies>`__:
291+
for Google BigQuery I/O. (pandas-gbq >= 0.8.0)
290292

291293

292294
* `Backports.lzma <https://pypi.org/project/backports.lzma/>`__: Only for Python 2, for writing to and/or reading from an xz compressed DataFrame in CSV; Python 3 support is built into the standard library.

doc/source/whatsnew/v0.24.0.rst

+12-7
Original file line numberDiff line numberDiff line change
@@ -260,9 +260,12 @@ Other Enhancements
260260
- :meth:`Series.droplevel` and :meth:`DataFrame.droplevel` are now implemented (:issue:`20342`)
261261
- Added support for reading from/writing to Google Cloud Storage via the ``gcsfs`` library (:issue:`19454`, :issue:`23094`)
262262
- :func:`to_gbq` and :func:`read_gbq` signature and documentation updated to
263-
reflect changes from the `Pandas-GBQ library version 0.6.0
264-
<https://pandas-gbq.readthedocs.io/en/latest/changelog.html#changelog-0-6-0>`__.
265-
(:issue:`21627`, :issue:`22557`)
263+
reflect changes from the `Pandas-GBQ library version 0.8.0
264+
<https://pandas-gbq.readthedocs.io/en/latest/changelog.html#changelog-0-8-0>`__.
265+
Adds a ``credentials`` argument, which enables the use of any kind of
266+
`google-auth credentials
267+
<https://google-auth.readthedocs.io/en/latest/>`__. (:issue:`21627`,
268+
:issue:`22557`, :issue:`23662`)
266269
- New method :meth:`HDFStore.walk` will recursively walk the group hierarchy of an HDF5 file (:issue:`10932`)
267270
- :func:`read_html` copies cell data across ``colspan`` and ``rowspan``, and it treats all-``th`` table rows as headers if ``header`` kwarg is not given and there is no ``thead`` (:issue:`17054`)
268271
- :meth:`Series.nlargest`, :meth:`Series.nsmallest`, :meth:`DataFrame.nlargest`, and :meth:`DataFrame.nsmallest` now accept the value ``"all"`` for the ``keep`` argument. This keeps all ties for the nth largest/smallest value (:issue:`16818`)
@@ -313,17 +316,19 @@ If installed, we now require:
313316
+-----------------+-----------------+----------+
314317
| bottleneck | 1.2.0 | |
315318
+-----------------+-----------------+----------+
319+
| fastparquet | 0.1.2 | |
320+
+-----------------+-----------------+----------+
316321
| matplotlib | 2.0.0 | |
317322
+-----------------+-----------------+----------+
318323
| numexpr | 2.6.1 | |
319324
+-----------------+-----------------+----------+
320-
| pytables | 3.4.2 | |
321-
+-----------------+-----------------+----------+
322-
| scipy | 0.18.1 | |
325+
| pandas-gbq | 0.8.0 | |
323326
+-----------------+-----------------+----------+
324327
| pyarrow | 0.7.0 | |
325328
+-----------------+-----------------+----------+
326-
| fastparquet | 0.1.2 | |
329+
| pytables | 3.4.2 | |
330+
+-----------------+-----------------+----------+
331+
| scipy | 0.18.1 | |
327332
+-----------------+-----------------+----------+
328333

329334
Additionally we no longer depend on `feather-format` for feather based storage

pandas/core/frame.py

+28-7
Original file line numberDiff line numberDiff line change
@@ -1241,9 +1241,9 @@ def to_dict(self, orient='dict', into=dict):
12411241
raise ValueError("orient '{o}' not understood".format(o=orient))
12421242

12431243
def to_gbq(self, destination_table, project_id=None, chunksize=None,
1244-
reauth=False, if_exists='fail', private_key=None,
1245-
auth_local_webserver=False, table_schema=None, location=None,
1246-
progress_bar=True, verbose=None):
1244+
reauth=False, if_exists='fail', auth_local_webserver=False,
1245+
table_schema=None, location=None, progress_bar=True,
1246+
credentials=None, verbose=None, private_key=None):
12471247
"""
12481248
Write a DataFrame to a Google BigQuery table.
12491249
@@ -1311,10 +1311,31 @@ def to_gbq(self, destination_table, project_id=None, chunksize=None,
13111311
chunk by chunk.
13121312
13131313
*New in version 0.5.0 of pandas-gbq*.
1314+
credentials : google.auth.credentials.Credentials, optional
1315+
Credentials for accessing Google APIs. Use this parameter to
1316+
override default credentials, such as to use Compute Engine
1317+
:class:`google.auth.compute_engine.Credentials` or Service
1318+
Account :class:`google.oauth2.service_account.Credentials`
1319+
directly.
1320+
1321+
*New in version 0.8.0 of pandas-gbq*.
1322+
1323+
.. versionadded:: 0.24.0
13141324
verbose : bool, deprecated
1315-
Deprecated in Pandas-GBQ 0.4.0. Use the `logging module
1325+
Deprecated in pandas-gbq version 0.4.0. Use the `logging module
13161326
to adjust verbosity instead
13171327
<https://pandas-gbq.readthedocs.io/en/latest/intro.html#logging>`__.
1328+
private_key : str, deprecated
1329+
Deprecated in pandas-gbq version 0.8.0. Use the ``credentials``
1330+
parameter and
1331+
:func:`google.oauth2.service_account.Credentials.from_service_account_info`
1332+
or
1333+
:func:`google.oauth2.service_account.Credentials.from_service_account_file`
1334+
instead.
1335+
1336+
Service account private key in JSON format. Can be file path
1337+
or string contents. This is useful for remote server
1338+
authentication (eg. Jupyter/IPython notebook on remote host).
13181339
13191340
See Also
13201341
--------
@@ -1324,11 +1345,11 @@ def to_gbq(self, destination_table, project_id=None, chunksize=None,
13241345
from pandas.io import gbq
13251346
return gbq.to_gbq(
13261347
self, destination_table, project_id=project_id,
1327-
chunksize=chunksize, reauth=reauth,
1328-
if_exists=if_exists, private_key=private_key,
1348+
chunksize=chunksize, reauth=reauth, if_exists=if_exists,
13291349
auth_local_webserver=auth_local_webserver,
13301350
table_schema=table_schema, location=location,
1331-
progress_bar=progress_bar, verbose=verbose)
1351+
progress_bar=progress_bar, credentials=credentials,
1352+
verbose=verbose, private_key=private_key)
13321353

13331354
@classmethod
13341355
def from_records(cls, data, index=None, exclude=None, columns=None,

pandas/io/gbq.py

+36-16
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ def _try_import():
2424

2525

2626
def read_gbq(query, project_id=None, index_col=None, col_order=None,
27-
reauth=False, private_key=None, auth_local_webserver=False,
28-
dialect=None, location=None, configuration=None,
29-
verbose=None):
27+
reauth=False, auth_local_webserver=False, dialect=None,
28+
location=None, configuration=None, credentials=None,
29+
private_key=None, verbose=None):
3030
"""
3131
Load data from Google BigQuery.
3232
@@ -98,10 +98,30 @@ def read_gbq(query, project_id=None, index_col=None, col_order=None,
9898
9999
For more information see `BigQuery REST API Reference
100100
<https://cloud.google.com/bigquery/docs/reference/rest/v2/jobs#configuration.query>`__.
101+
credentials : google.auth.credentials.Credentials, optional
102+
Credentials for accessing Google APIs. Use this parameter to override
103+
default credentials, such as to use Compute Engine
104+
:class:`google.auth.compute_engine.Credentials` or Service Account
105+
:class:`google.oauth2.service_account.Credentials` directly.
106+
107+
*New in version 0.8.0 of pandas-gbq*.
108+
109+
.. versionadded:: 0.24.0
101110
verbose : None, deprecated
102-
Deprecated in Pandas-GBQ 0.4.0. Use the `logging module
103-
to adjust verbosity instead
111+
Deprecated in pandas-gbq version 0.4.0. Use the `logging module to
112+
adjust verbosity instead
104113
<https://pandas-gbq.readthedocs.io/en/latest/intro.html#logging>`__.
114+
private_key : str, deprecated
115+
Deprecated in pandas-gbq version 0.8.0. Use the ``credentials``
116+
parameter and
117+
:func:`google.oauth2.service_account.Credentials.from_service_account_info`
118+
or
119+
:func:`google.oauth2.service_account.Credentials.from_service_account_file`
120+
instead.
121+
122+
Service account private key in JSON format. Can be file path
123+
or string contents. This is useful for remote server
124+
authentication (eg. Jupyter/IPython notebook on remote host).
105125
106126
Returns
107127
-------
@@ -127,20 +147,20 @@ def read_gbq(query, project_id=None, index_col=None, col_order=None,
127147

128148
return pandas_gbq.read_gbq(
129149
query, project_id=project_id, index_col=index_col,
130-
col_order=col_order, reauth=reauth, verbose=verbose,
131-
private_key=private_key, auth_local_webserver=auth_local_webserver,
132-
dialect=dialect, location=location, configuration=configuration)
150+
col_order=col_order, reauth=reauth,
151+
auth_local_webserver=auth_local_webserver, dialect=dialect,
152+
location=location, configuration=configuration,
153+
credentials=credentials, verbose=verbose, private_key=private_key)
133154

134155

135156
def to_gbq(dataframe, destination_table, project_id=None, chunksize=None,
136-
verbose=None, reauth=False, if_exists='fail', private_key=None,
137-
auth_local_webserver=False, table_schema=None, location=None,
138-
progress_bar=True):
157+
reauth=False, if_exists='fail', auth_local_webserver=False,
158+
table_schema=None, location=None, progress_bar=True,
159+
credentials=None, verbose=None, private_key=None):
139160
pandas_gbq = _try_import()
140161
return pandas_gbq.to_gbq(
141162
dataframe, destination_table, project_id=project_id,
142-
chunksize=chunksize, verbose=verbose, reauth=reauth,
143-
if_exists=if_exists, private_key=private_key,
144-
auth_local_webserver=auth_local_webserver,
145-
table_schema=table_schema, location=location,
146-
progress_bar=progress_bar)
163+
chunksize=chunksize, reauth=reauth, if_exists=if_exists,
164+
auth_local_webserver=auth_local_webserver, table_schema=table_schema,
165+
location=location, progress_bar=progress_bar,
166+
credentials=credentials, verbose=verbose, private_key=private_key)

pandas/tests/io/test_gbq.py

+9-8
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
api_exceptions = pytest.importorskip("google.api_core.exceptions")
2020
bigquery = pytest.importorskip("google.cloud.bigquery")
2121
service_account = pytest.importorskip("google.oauth2.service_account")
22-
pandas_gbq = pytest.importorskip('pandas_gbq')
22+
pandas_gbq = pytest.importorskip("pandas_gbq")
2323

2424
PROJECT_ID = None
2525
PRIVATE_KEY_JSON_PATH = None
@@ -70,15 +70,16 @@ def _get_private_key_path():
7070
return private_key_path
7171

7272

73-
def _get_client():
74-
project_id = _get_project_id()
75-
credentials = None
76-
73+
def _get_credentials():
7774
private_key_path = _get_private_key_path()
7875
if private_key_path:
79-
credentials = service_account.Credentials.from_service_account_file(
76+
return service_account.Credentials.from_service_account_file(
8077
private_key_path)
8178

79+
80+
def _get_client():
81+
project_id = _get_project_id()
82+
credentials = _get_credentials()
8283
return bigquery.Client(project=project_id, credentials=credentials)
8384

8485

@@ -144,11 +145,11 @@ def test_roundtrip(self):
144145
df = make_mixed_dataframe_v2(test_size)
145146

146147
df.to_gbq(destination_table, _get_project_id(), chunksize=None,
147-
private_key=_get_private_key_path())
148+
credentials=_get_credentials())
148149

149150
result = pd.read_gbq("SELECT COUNT(*) AS num_rows FROM {0}"
150151
.format(destination_table),
151152
project_id=_get_project_id(),
152-
private_key=_get_private_key_path(),
153+
credentials=_get_credentials(),
153154
dialect="standard")
154155
assert result['num_rows'][0] == test_size

0 commit comments

Comments
 (0)