Skip to content

Commit 22a6064

Browse files
authored
FIX: remove references to deprecated client.dataset method (#355)
* FIX: remove references to deprecated client.dataset method * fix lint * fix constraints file
1 parent d35334e commit 22a6064

File tree

3 files changed

+136
-102
lines changed

3 files changed

+136
-102
lines changed

ci/constraints-3.7.pip

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@ pandas==0.23.2
33
google-auth==1.4.1
44
google-auth-oauthlib==0.0.1
55
google-cloud-bigquery==1.11.1
6-
google-cloud-bigquery[bqstorage,pandas]==1.11.1
6+
google-cloud-bigquery-storage==1.1.0
77
pydata-google-auth==0.1.2
88
tqdm==4.23.0

pandas_gbq/gbq.py

+19-72
Original file line numberDiff line numberDiff line change
@@ -633,70 +633,6 @@ def load_data(
633633
except self.http_error as ex:
634634
self.process_http_error(ex)
635635

636-
def schema(self, dataset_id, table_id):
637-
"""Retrieve the schema of the table
638-
639-
Obtain from BigQuery the field names and field types
640-
for the table defined by the parameters
641-
642-
Parameters
643-
----------
644-
dataset_id : str
645-
Name of the BigQuery dataset for the table
646-
table_id : str
647-
Name of the BigQuery table
648-
649-
Returns
650-
-------
651-
list of dicts
652-
Fields representing the schema
653-
"""
654-
table_ref = self.client.dataset(dataset_id).table(table_id)
655-
656-
try:
657-
table = self.client.get_table(table_ref)
658-
remote_schema = table.schema
659-
660-
remote_fields = [
661-
field_remote.to_api_repr() for field_remote in remote_schema
662-
]
663-
for field in remote_fields:
664-
field["type"] = field["type"].upper()
665-
field["mode"] = field["mode"].upper()
666-
667-
return remote_fields
668-
except self.http_error as ex:
669-
self.process_http_error(ex)
670-
671-
def verify_schema(self, dataset_id, table_id, schema):
672-
"""Indicate whether schemas match exactly
673-
674-
Compare the BigQuery table identified in the parameters with
675-
the schema passed in and indicate whether all fields in the former
676-
are present in the latter. Order is not considered.
677-
678-
Parameters
679-
----------
680-
dataset_id :str
681-
Name of the BigQuery dataset for the table
682-
table_id : str
683-
Name of the BigQuery table
684-
schema : list(dict)
685-
Schema for comparison. Each item should have
686-
a 'name' and a 'type'
687-
688-
Returns
689-
-------
690-
bool
691-
Whether the schemas match
692-
"""
693-
694-
fields_remote = pandas_gbq.schema._clean_schema_fields(
695-
self.schema(dataset_id, table_id)
696-
)
697-
fields_local = pandas_gbq.schema._clean_schema_fields(schema["fields"])
698-
return fields_remote == fields_local
699-
700636
def delete_and_recreate_table(self, dataset_id, table_id, table_schema):
701637
table = _Table(
702638
self.project_id, dataset_id, credentials=self.credentials
@@ -1271,6 +1207,15 @@ def __init__(
12711207
private_key=private_key,
12721208
)
12731209

1210+
def _table_ref(self, table_id):
1211+
"""Return a BigQuery client library table reference"""
1212+
from google.cloud.bigquery import DatasetReference
1213+
from google.cloud.bigquery import TableReference
1214+
1215+
return TableReference(
1216+
DatasetReference(self.project_id, self.dataset_id), table_id
1217+
)
1218+
12741219
def exists(self, table_id):
12751220
"""Check if a table exists in Google BigQuery
12761221
@@ -1285,12 +1230,8 @@ def exists(self, table_id):
12851230
true if table exists, otherwise false
12861231
"""
12871232
from google.api_core.exceptions import NotFound
1288-
from google.cloud.bigquery import DatasetReference
1289-
from google.cloud.bigquery import TableReference
12901233

1291-
table_ref = TableReference(
1292-
DatasetReference(self.project_id, self.dataset_id), table_id
1293-
)
1234+
table_ref = self._table_ref(table_id)
12941235
try:
12951236
self.client.get_table(table_ref)
12961237
return True
@@ -1358,7 +1299,7 @@ def delete(self, table_id):
13581299
if not self.exists(table_id):
13591300
raise NotFoundException("Table does not exist")
13601301

1361-
table_ref = self.client.dataset(self.dataset_id).table(table_id)
1302+
table_ref = self._table_ref(table_id)
13621303
try:
13631304
self.client.delete_table(table_ref)
13641305
except NotFound:
@@ -1385,6 +1326,12 @@ def __init__(
13851326
private_key=private_key,
13861327
)
13871328

1329+
def _dataset_ref(self, dataset_id):
1330+
"""Return a BigQuery client library dataset reference"""
1331+
from google.cloud.bigquery import DatasetReference
1332+
1333+
return DatasetReference(self.project_id, dataset_id)
1334+
13881335
def exists(self, dataset_id):
13891336
"""Check if a dataset exists in Google BigQuery
13901337
@@ -1401,7 +1348,7 @@ def exists(self, dataset_id):
14011348
from google.api_core.exceptions import NotFound
14021349

14031350
try:
1404-
self.client.get_dataset(self.client.dataset(dataset_id))
1351+
self.client.get_dataset(self._dataset_ref(dataset_id))
14051352
return True
14061353
except NotFound:
14071354
return False
@@ -1423,7 +1370,7 @@ def create(self, dataset_id):
14231370
"Dataset {0} already " "exists".format(dataset_id)
14241371
)
14251372

1426-
dataset = Dataset(self.client.dataset(dataset_id))
1373+
dataset = Dataset(self._dataset_ref(dataset_id))
14271374

14281375
if self.location is not None:
14291376
dataset.location = self.location

0 commit comments

Comments
 (0)