Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
wiseplat
GitHub Repository: wiseplat/python-code
Path: blob/master/ invest-robot-contest_TinkoffBotTwitch-main/venv/lib/python3.8/site-packages/pandas/io/gbq.py
7815 views
1
""" Google BigQuery support """
2
from __future__ import annotations
3
4
from typing import (
5
TYPE_CHECKING,
6
Any,
7
)
8
9
from pandas.compat._optional import import_optional_dependency
10
11
if TYPE_CHECKING:
12
from pandas import DataFrame
13
14
15
def _try_import():
16
# since pandas is a dependency of pandas-gbq
17
# we need to import on first use
18
msg = (
19
"pandas-gbq is required to load data from Google BigQuery. "
20
"See the docs: https://pandas-gbq.readthedocs.io."
21
)
22
pandas_gbq = import_optional_dependency("pandas_gbq", extra=msg)
23
return pandas_gbq
24
25
26
def read_gbq(
27
query: str,
28
project_id: str | None = None,
29
index_col: str | None = None,
30
col_order: list[str] | None = None,
31
reauth: bool = False,
32
auth_local_webserver: bool = False,
33
dialect: str | None = None,
34
location: str | None = None,
35
configuration: dict[str, Any] | None = None,
36
credentials=None,
37
use_bqstorage_api: bool | None = None,
38
max_results: int | None = None,
39
progress_bar_type: str | None = None,
40
) -> DataFrame:
41
"""
42
Load data from Google BigQuery.
43
44
This function requires the `pandas-gbq package
45
<https://pandas-gbq.readthedocs.io>`__.
46
47
See the `How to authenticate with Google BigQuery
48
<https://pandas-gbq.readthedocs.io/en/latest/howto/authentication.html>`__
49
guide for authentication instructions.
50
51
Parameters
52
----------
53
query : str
54
SQL-Like Query to return data values.
55
project_id : str, optional
56
Google BigQuery Account project ID. Optional when available from
57
the environment.
58
index_col : str, optional
59
Name of result column to use for index in results DataFrame.
60
col_order : list(str), optional
61
List of BigQuery column names in the desired order for results
62
DataFrame.
63
reauth : bool, default False
64
Force Google BigQuery to re-authenticate the user. This is useful
65
if multiple accounts are used.
66
auth_local_webserver : bool, default False
67
Use the `local webserver flow`_ instead of the `console flow`_
68
when getting user credentials.
69
70
.. _local webserver flow:
71
https://google-auth-oauthlib.readthedocs.io/en/latest/reference/google_auth_oauthlib.flow.html#google_auth_oauthlib.flow.InstalledAppFlow.run_local_server
72
.. _console flow:
73
https://google-auth-oauthlib.readthedocs.io/en/latest/reference/google_auth_oauthlib.flow.html#google_auth_oauthlib.flow.InstalledAppFlow.run_console
74
75
*New in version 0.2.0 of pandas-gbq*.
76
dialect : str, default 'legacy'
77
Note: The default value is changing to 'standard' in a future version.
78
79
SQL syntax dialect to use. Value can be one of:
80
81
``'legacy'``
82
Use BigQuery's legacy SQL dialect. For more information see
83
`BigQuery Legacy SQL Reference
84
<https://cloud.google.com/bigquery/docs/reference/legacy-sql>`__.
85
``'standard'``
86
Use BigQuery's standard SQL, which is
87
compliant with the SQL 2011 standard. For more information
88
see `BigQuery Standard SQL Reference
89
<https://cloud.google.com/bigquery/docs/reference/standard-sql/>`__.
90
location : str, optional
91
Location where the query job should run. See the `BigQuery locations
92
documentation
93
<https://cloud.google.com/bigquery/docs/dataset-locations>`__ for a
94
list of available locations. The location must match that of any
95
datasets used in the query.
96
97
*New in version 0.5.0 of pandas-gbq*.
98
configuration : dict, optional
99
Query config parameters for job processing.
100
For example:
101
102
configuration = {'query': {'useQueryCache': False}}
103
104
For more information see `BigQuery REST API Reference
105
<https://cloud.google.com/bigquery/docs/reference/rest/v2/jobs#configuration.query>`__.
106
credentials : google.auth.credentials.Credentials, optional
107
Credentials for accessing Google APIs. Use this parameter to override
108
default credentials, such as to use Compute Engine
109
:class:`google.auth.compute_engine.Credentials` or Service Account
110
:class:`google.oauth2.service_account.Credentials` directly.
111
112
*New in version 0.8.0 of pandas-gbq*.
113
use_bqstorage_api : bool, default False
114
Use the `BigQuery Storage API
115
<https://cloud.google.com/bigquery/docs/reference/storage/>`__ to
116
download query results quickly, but at an increased cost. To use this
117
API, first `enable it in the Cloud Console
118
<https://console.cloud.google.com/apis/library/bigquerystorage.googleapis.com>`__.
119
You must also have the `bigquery.readsessions.create
120
<https://cloud.google.com/bigquery/docs/access-control#roles>`__
121
permission on the project you are billing queries to.
122
123
This feature requires version 0.10.0 or later of the ``pandas-gbq``
124
package. It also requires the ``google-cloud-bigquery-storage`` and
125
``fastavro`` packages.
126
127
.. versionadded:: 0.25.0
128
max_results : int, optional
129
If set, limit the maximum number of rows to fetch from the query
130
results.
131
132
*New in version 0.12.0 of pandas-gbq*.
133
134
.. versionadded:: 1.1.0
135
progress_bar_type : Optional, str
136
If set, use the `tqdm <https://tqdm.github.io/>`__ library to
137
display a progress bar while the data downloads. Install the
138
``tqdm`` package to use this feature.
139
140
Possible values of ``progress_bar_type`` include:
141
142
``None``
143
No progress bar.
144
``'tqdm'``
145
Use the :func:`tqdm.tqdm` function to print a progress bar
146
to :data:`sys.stderr`.
147
``'tqdm_notebook'``
148
Use the :func:`tqdm.tqdm_notebook` function to display a
149
progress bar as a Jupyter notebook widget.
150
``'tqdm_gui'``
151
Use the :func:`tqdm.tqdm_gui` function to display a
152
progress bar as a graphical dialog box.
153
154
Note that this feature requires version 0.12.0 or later of the
155
``pandas-gbq`` package. And it requires the ``tqdm`` package. Slightly
156
different than ``pandas-gbq``, here the default is ``None``.
157
158
.. versionadded:: 1.0.0
159
160
Returns
161
-------
162
df: DataFrame
163
DataFrame representing results of query.
164
165
See Also
166
--------
167
pandas_gbq.read_gbq : This function in the pandas-gbq library.
168
DataFrame.to_gbq : Write a DataFrame to Google BigQuery.
169
"""
170
pandas_gbq = _try_import()
171
172
kwargs: dict[str, str | bool | int | None] = {}
173
174
# START: new kwargs. Don't populate unless explicitly set.
175
if use_bqstorage_api is not None:
176
kwargs["use_bqstorage_api"] = use_bqstorage_api
177
if max_results is not None:
178
kwargs["max_results"] = max_results
179
180
kwargs["progress_bar_type"] = progress_bar_type
181
# END: new kwargs
182
183
return pandas_gbq.read_gbq(
184
query,
185
project_id=project_id,
186
index_col=index_col,
187
col_order=col_order,
188
reauth=reauth,
189
auth_local_webserver=auth_local_webserver,
190
dialect=dialect,
191
location=location,
192
configuration=configuration,
193
credentials=credentials,
194
**kwargs,
195
)
196
197
198
def to_gbq(
199
dataframe: DataFrame,
200
destination_table: str,
201
project_id: str | None = None,
202
chunksize: int | None = None,
203
reauth: bool = False,
204
if_exists: str = "fail",
205
auth_local_webserver: bool = False,
206
table_schema: list[dict[str, str]] | None = None,
207
location: str | None = None,
208
progress_bar: bool = True,
209
credentials=None,
210
) -> None:
211
pandas_gbq = _try_import()
212
pandas_gbq.to_gbq(
213
dataframe,
214
destination_table,
215
project_id=project_id,
216
chunksize=chunksize,
217
reauth=reauth,
218
if_exists=if_exists,
219
auth_local_webserver=auth_local_webserver,
220
table_schema=table_schema,
221
location=location,
222
progress_bar=progress_bar,
223
credentials=credentials,
224
)
225
226