16
16
17
17
from pandas_gbq .exceptions import AccessDenied
18
18
from pandas_gbq .exceptions import PerformanceWarning
19
+ from pandas_gbq import features
20
+ from pandas_gbq .features import FEATURES
19
21
import pandas_gbq .schema
20
22
import pandas_gbq .timestamp
21
23
22
24
23
25
logger = logging .getLogger (__name__ )
24
26
25
- BIGQUERY_INSTALLED_VERSION = None
26
- BIGQUERY_CLIENT_INFO_VERSION = "1.12.0"
27
- BIGQUERY_BQSTORAGE_VERSION = "1.24.0"
28
- HAS_CLIENT_INFO = False
29
- HAS_BQSTORAGE_SUPPORT = False
30
-
31
27
try :
32
28
import tqdm # noqa
33
29
except ImportError :
34
30
tqdm = None
35
31
36
32
37
- def _check_google_client_version ():
38
- global BIGQUERY_INSTALLED_VERSION , HAS_CLIENT_INFO , HAS_BQSTORAGE_SUPPORT , SHOW_VERBOSE_DEPRECATION
39
-
40
- try :
41
- import pkg_resources
42
-
43
- except ImportError :
44
- raise ImportError ("Could not import pkg_resources (setuptools)." )
45
-
46
- # https://github.com/googleapis/python-bigquery/blob/master/CHANGELOG.md
47
- bigquery_minimum_version = pkg_resources .parse_version ("1.11.0" )
48
- bigquery_client_info_version = pkg_resources .parse_version (
49
- BIGQUERY_CLIENT_INFO_VERSION
50
- )
51
- bigquery_bqstorage_version = pkg_resources .parse_version (
52
- BIGQUERY_BQSTORAGE_VERSION
53
- )
54
- BIGQUERY_INSTALLED_VERSION = pkg_resources .get_distribution (
55
- "google-cloud-bigquery"
56
- ).parsed_version
57
-
58
- HAS_CLIENT_INFO = (
59
- BIGQUERY_INSTALLED_VERSION >= bigquery_client_info_version
60
- )
61
- HAS_BQSTORAGE_SUPPORT = (
62
- BIGQUERY_INSTALLED_VERSION >= bigquery_bqstorage_version
63
- )
64
-
65
- if BIGQUERY_INSTALLED_VERSION < bigquery_minimum_version :
66
- raise ImportError (
67
- "pandas-gbq requires google-cloud-bigquery >= {0}, "
68
- "current version {1}" .format (
69
- bigquery_minimum_version , BIGQUERY_INSTALLED_VERSION
70
- )
71
- )
72
-
73
- # Add check for Pandas version before showing deprecation warning.
74
- # https://github.com/pydata/pandas-gbq/issues/157
75
- pandas_installed_version = pkg_resources .get_distribution (
76
- "pandas"
77
- ).parsed_version
78
- pandas_version_wo_verbosity = pkg_resources .parse_version ("0.23.0" )
79
- SHOW_VERBOSE_DEPRECATION = (
80
- pandas_installed_version >= pandas_version_wo_verbosity
81
- )
82
-
83
-
84
33
def _test_google_api_imports ():
34
+ try :
35
+ import pkg_resources # noqa
36
+ except ImportError as ex :
37
+ raise ImportError ("pandas-gbq requires setuptools" ) from ex
85
38
86
39
try :
87
40
import pydata_google_auth # noqa
88
41
except ImportError as ex :
89
- raise ImportError (
90
- "pandas-gbq requires pydata-google-auth: {0}" .format (ex )
91
- )
42
+ raise ImportError ("pandas-gbq requires pydata-google-auth" ) from ex
92
43
93
44
try :
94
45
from google_auth_oauthlib .flow import InstalledAppFlow # noqa
95
46
except ImportError as ex :
96
- raise ImportError (
97
- "pandas-gbq requires google-auth-oauthlib: {0}" .format (ex )
98
- )
47
+ raise ImportError ("pandas-gbq requires google-auth-oauthlib" ) from ex
99
48
100
49
try :
101
50
import google .auth # noqa
102
51
except ImportError as ex :
103
- raise ImportError ("pandas-gbq requires google-auth: {0}" . format ( ex ))
52
+ raise ImportError ("pandas-gbq requires google-auth" ) from ex
104
53
105
54
try :
106
55
from google .cloud import bigquery # noqa
107
56
except ImportError as ex :
108
- raise ImportError (
109
- "pandas-gbq requires google-cloud-bigquery: {0}" .format (ex )
110
- )
111
-
112
- _check_google_client_version ()
57
+ raise ImportError ("pandas-gbq requires google-cloud-bigquery" ) from ex
113
58
114
59
115
60
class DatasetCreationError (ValueError ):
@@ -416,7 +361,7 @@ def get_client(self):
416
361
# In addition to new enough version of google-api-core, a new enough
417
362
# version of google-cloud-bigquery is required to populate the
418
363
# client_info.
419
- if HAS_CLIENT_INFO :
364
+ if FEATURES . bigquery_has_client_info :
420
365
return bigquery .Client (
421
366
project = self .project_id ,
422
367
credentials = self .credentials ,
@@ -550,14 +495,15 @@ def _download_results(
550
495
if user_dtypes is None :
551
496
user_dtypes = {}
552
497
553
- if self .use_bqstorage_api and not HAS_BQSTORAGE_SUPPORT :
498
+ if self .use_bqstorage_api and not FEATURES . bigquery_has_bqstorage :
554
499
warnings .warn (
555
500
(
556
501
"use_bqstorage_api was set, but have google-cloud-bigquery "
557
502
"version {}. Requires google-cloud-bigquery version "
558
503
"{} or later."
559
504
).format (
560
- BIGQUERY_INSTALLED_VERSION , BIGQUERY_BQSTORAGE_VERSION
505
+ FEATURES .bigquery_installed_version ,
506
+ features .BIGQUERY_BQSTORAGE_VERSION ,
561
507
),
562
508
PerformanceWarning ,
563
509
stacklevel = 4 ,
@@ -568,7 +514,7 @@ def _download_results(
568
514
create_bqstorage_client = False
569
515
570
516
to_dataframe_kwargs = {}
571
- if HAS_BQSTORAGE_SUPPORT :
517
+ if FEATURES . bigquery_has_bqstorage :
572
518
to_dataframe_kwargs [
573
519
"create_bqstorage_client"
574
520
] = create_bqstorage_client
@@ -880,7 +826,7 @@ def read_gbq(
880
826
881
827
_test_google_api_imports ()
882
828
883
- if verbose is not None and SHOW_VERBOSE_DEPRECATION :
829
+ if verbose is not None and FEATURES . pandas_has_deprecated_verbose :
884
830
warnings .warn (
885
831
"verbose is deprecated and will be removed in "
886
832
"a future version. Set logging level in order to vary "
@@ -1054,7 +1000,7 @@ def to_gbq(
1054
1000
1055
1001
_test_google_api_imports ()
1056
1002
1057
- if verbose is not None and SHOW_VERBOSE_DEPRECATION :
1003
+ if verbose is not None and FEATURES . pandas_has_deprecated_verbose :
1058
1004
warnings .warn (
1059
1005
"verbose is deprecated and will be removed in "
1060
1006
"a future version. Set logging level in order to vary "
@@ -1133,8 +1079,8 @@ def to_gbq(
1133
1079
"schema of the destination table."
1134
1080
)
1135
1081
1136
- # Update the local `table_schema` so mode matches.
1137
- # See: https://github.com/pydata/pandas-gbq/issues/315
1082
+ # Update the local `table_schema` so mode (NULLABLE/REQUIRED)
1083
+ # matches. See: https://github.com/pydata/pandas-gbq/issues/315
1138
1084
table_schema = pandas_gbq .schema .update_schema (
1139
1085
table_schema , original_schema
1140
1086
)
@@ -1252,7 +1198,6 @@ def create(self, table_id, schema):
1252
1198
dataframe.
1253
1199
"""
1254
1200
from google .cloud .bigquery import DatasetReference
1255
- from google .cloud .bigquery import SchemaField
1256
1201
from google .cloud .bigquery import Table
1257
1202
from google .cloud .bigquery import TableReference
1258
1203
@@ -1274,12 +1219,7 @@ def create(self, table_id, schema):
1274
1219
DatasetReference (self .project_id , self .dataset_id ), table_id
1275
1220
)
1276
1221
table = Table (table_ref )
1277
-
1278
- schema = pandas_gbq .schema .add_default_nullable_mode (schema )
1279
-
1280
- table .schema = [
1281
- SchemaField .from_api_repr (field ) for field in schema ["fields" ]
1282
- ]
1222
+ table .schema = pandas_gbq .schema .to_google_cloud_bigquery (schema )
1283
1223
1284
1224
try :
1285
1225
self .client .create_table (table )
0 commit comments