Skip to content

Commit 36eb530

Browse files
refactor: break down gbq.py file to several smaller ones (#909)
* refactor: break down gbq.py file to several smaller ones * fix test failures * fix more test import failures * fix prerelease tests * fix system tests * restore imports * restore imports * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * add noqa for backward compatibility * fix lint * fix test failures by ignoring timestamp granularity: --------- Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
1 parent d4d85ce commit 36eb530

File tree

6 files changed

+736
-693
lines changed

6 files changed

+736
-693
lines changed

pandas_gbq/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@
55
import warnings
66

77
from pandas_gbq import version as pandas_gbq_version
8+
from pandas_gbq.contexts import Context, context
89

910
from . import _versions_helpers
10-
from .gbq import Context, context, read_gbq, to_gbq # noqa
11+
from .gbq import read_gbq, to_gbq # noqa
1112

1213
sys_major, sys_minor, sys_micro = _versions_helpers.extract_runtime_version()
1314
if sys_major == 3 and sys_minor in (7, 8):

pandas_gbq/contexts.py

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
# Copyright (c) 2025 pandas-gbq Authors All rights reserved.
2+
# Use of this source code is governed by a BSD-style
3+
# license that can be found in the LICENSE file.
4+
5+
6+
class Context(object):
7+
"""Storage for objects to be used throughout a session.
8+
9+
A Context object is initialized when the ``pandas_gbq`` module is
10+
imported, and can be found at :attr:`pandas_gbq.context`.
11+
"""
12+
13+
def __init__(self):
14+
self._credentials = None
15+
self._project = None
16+
# dialect defaults to None so that read_gbq can stop warning if set.
17+
self._dialect = None
18+
19+
@property
20+
def credentials(self):
21+
"""
22+
Credentials to use for Google APIs.
23+
24+
These credentials are automatically cached in memory by calls to
25+
:func:`pandas_gbq.read_gbq` and :func:`pandas_gbq.to_gbq`. To
26+
manually set the credentials, construct an
27+
:class:`google.auth.credentials.Credentials` object and set it as
28+
the context credentials as demonstrated in the example below. See
29+
`auth docs`_ for more information on obtaining credentials.
30+
31+
.. _auth docs: http://google-auth.readthedocs.io
32+
/en/latest/user-guide.html#obtaining-credentials
33+
34+
Returns
35+
-------
36+
google.auth.credentials.Credentials
37+
38+
Examples
39+
--------
40+
41+
Manually setting the context credentials:
42+
43+
>>> import pandas_gbq
44+
>>> from google.oauth2 import service_account
45+
>>> credentials = service_account.Credentials.from_service_account_file(
46+
... '/path/to/key.json',
47+
... )
48+
>>> pandas_gbq.context.credentials = credentials
49+
"""
50+
return self._credentials
51+
52+
@credentials.setter
53+
def credentials(self, value):
54+
self._credentials = value
55+
56+
@property
57+
def project(self):
58+
"""Default project to use for calls to Google APIs.
59+
60+
Returns
61+
-------
62+
str
63+
64+
Examples
65+
--------
66+
67+
Manually setting the context project:
68+
69+
>>> import pandas_gbq
70+
>>> pandas_gbq.context.project = 'my-project'
71+
"""
72+
return self._project
73+
74+
@project.setter
75+
def project(self, value):
76+
self._project = value
77+
78+
@property
79+
def dialect(self):
80+
"""
81+
Default dialect to use in :func:`pandas_gbq.read_gbq`.
82+
83+
Allowed values for the BigQuery SQL syntax dialect:
84+
85+
``'legacy'``
86+
Use BigQuery's legacy SQL dialect. For more information see
87+
`BigQuery Legacy SQL Reference
88+
<https://cloud.google.com/bigquery/docs/reference/legacy-sql>`__.
89+
``'standard'``
90+
Use BigQuery's standard SQL, which is
91+
compliant with the SQL 2011 standard. For more information
92+
see `BigQuery Standard SQL Reference
93+
<https://cloud.google.com/bigquery/docs/reference/standard-sql/>`__.
94+
95+
Returns
96+
-------
97+
str
98+
99+
Examples
100+
--------
101+
102+
Setting the default syntax to standard:
103+
104+
>>> import pandas_gbq
105+
>>> pandas_gbq.context.dialect = 'standard'
106+
"""
107+
return self._dialect
108+
109+
@dialect.setter
110+
def dialect(self, value):
111+
self._dialect = value
112+
113+
114+
# Create an empty context, used to cache credentials.
115+
context = Context()
116+
"""A :class:`pandas_gbq.Context` object used to cache credentials.
117+
118+
Credentials automatically are cached in-memory by :func:`pandas_gbq.read_gbq`
119+
and :func:`pandas_gbq.to_gbq`.
120+
"""

pandas_gbq/exceptions.py

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,70 @@
33
# license that can be found in the LICENSE file.
44

55

6+
class DatasetCreationError(ValueError):
7+
"""
8+
Raised when the create dataset method fails
9+
"""
10+
11+
12+
class InvalidColumnOrder(ValueError):
13+
"""
14+
Raised when the provided column order for output
15+
results DataFrame does not match the schema
16+
returned by BigQuery.
17+
"""
18+
19+
20+
class InvalidIndexColumn(ValueError):
21+
"""
22+
Raised when the provided index column for output
23+
results DataFrame does not match the schema
24+
returned by BigQuery.
25+
"""
26+
27+
28+
class InvalidPageToken(ValueError):
29+
"""
30+
Raised when Google BigQuery fails to return,
31+
or returns a duplicate page token.
32+
"""
33+
34+
35+
class InvalidSchema(ValueError):
36+
"""
37+
Raised when the provided DataFrame does
38+
not match the schema of the destination
39+
table in BigQuery.
40+
"""
41+
42+
def __init__(self, message: str):
43+
self._message = message
44+
45+
@property
46+
def message(self) -> str:
47+
return self._message
48+
49+
50+
class NotFoundException(ValueError):
51+
"""
52+
Raised when the project_id, table or dataset provided in the query could
53+
not be found.
54+
"""
55+
56+
57+
class TableCreationError(ValueError):
58+
"""
59+
Raised when the create table method fails
60+
"""
61+
62+
def __init__(self, message: str):
63+
self._message = message
64+
65+
@property
66+
def message(self) -> str:
67+
return self._message
68+
69+
670
class GenericGBQException(ValueError):
771
"""
872
Raised when an unrecognized Google API Error occurs.

0 commit comments

Comments
 (0)