Skip to content

Commit b84b754

Browse files
authored
feat(bigquery): add PendingDeprecationWarning for from_dataframe methods (#18048)
Adds `PendingDeprecationWarning` to `Client.load_table_from_dataframe()` and `Client.insert_rows_from_dataframe()` in `google-cloud-bigquery`. This alerts users to adopt direct `pandas_gbq.to_gbq()` entry points ahead of future deprecation phases per the `pandas-gbq` migration design. * Added `PendingDeprecationWarning` to `load_table_from_dataframe` and `insert_rows_from_dataframe` in `google/cloud/bigquery/client.py`. * Added corresponding unit tests in `tests/unit/test_client.py`. Fixes #<526614511 🦕
1 parent b9a1379 commit b84b754

2 files changed

Lines changed: 62 additions & 1 deletion

File tree

packages/google-cloud-bigquery/google/cloud/bigquery/client.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,16 @@
163163
# https://github.com/googleapis/python-bigquery/issues/438
164164
_MIN_GET_QUERY_RESULTS_TIMEOUT = 120
165165

166+
_LOAD_TABLE_FROM_DATAFRAME_DEPRECATED = (
167+
"Loading DataFrames via google-cloud-bigquery is deprecated. "
168+
"For direct, optimized loading, please call 'pandas_gbq.to_gbq()' directly."
169+
)
170+
171+
_INSERT_ROWS_FROM_DATAFRAME_DEPRECATED = (
172+
"Inserting rows from DataFrames via google-cloud-bigquery is deprecated. "
173+
"For direct, optimized access, please call 'pandas_gbq.to_gbq()' directly."
174+
)
175+
166176
TIMEOUT_HEADER = "X-Server-Timeout"
167177

168178

@@ -2830,6 +2840,12 @@ def load_table_from_dataframe(
28302840
If ``job_config`` is not an instance of
28312841
:class:`~google.cloud.bigquery.job.LoadJobConfig` class.
28322842
"""
2843+
warnings.warn(
2844+
_LOAD_TABLE_FROM_DATAFRAME_DEPRECATED,
2845+
PendingDeprecationWarning,
2846+
stacklevel=2,
2847+
)
2848+
28332849
job_id = _make_job_id(job_id, job_id_prefix)
28342850

28352851
if job_config is not None:
@@ -3900,6 +3916,12 @@ def insert_rows_from_dataframe(
39003916
Raises:
39013917
ValueError: if table's schema is not set
39023918
"""
3919+
warnings.warn(
3920+
_INSERT_ROWS_FROM_DATAFRAME_DEPRECATED,
3921+
PendingDeprecationWarning,
3922+
stacklevel=2,
3923+
)
3924+
39033925
insert_results = []
39043926

39053927
chunk_count = int(math.ceil(len(dataframe) / chunk_size))

packages/google-cloud-bigquery/tests/unit/test_client.py

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6579,6 +6579,26 @@ def test_insert_rows_from_dataframe_w_explicit_none_insert_ids(self):
65796579
timeout=DEFAULT_TIMEOUT,
65806580
)
65816581

6582+
def test_insert_rows_from_dataframe_emits_pending_deprecation_warning(self):
6583+
pandas = pytest.importorskip("pandas")
6584+
from google.cloud.bigquery.schema import SchemaField
6585+
from google.cloud.bigquery.table import Table
6586+
6587+
creds = _make_credentials()
6588+
http = object()
6589+
client = self._make_one(project=self.PROJECT, credentials=creds, _http=http)
6590+
client._connection = make_connection({}, {})
6591+
6592+
schema = [SchemaField("name", "STRING", mode="REQUIRED")]
6593+
table = Table(self.TABLE_REF, schema=schema)
6594+
dataframe = pandas.DataFrame([{"name": "Alice"}])
6595+
6596+
with pytest.warns(
6597+
PendingDeprecationWarning,
6598+
match="Inserting rows from DataFrames via google-cloud-bigquery is deprecated",
6599+
):
6600+
client.insert_rows_from_dataframe(table, dataframe)
6601+
65826602
def test_insert_rows_json_default_behavior(self):
65836603
from google.cloud.bigquery.dataset import DatasetReference
65846604
from google.cloud.bigquery.schema import SchemaField
@@ -6841,10 +6861,10 @@ def test_insert_rows_w_wrong_arg(self):
68416861
client.insert_rows_json(table, ROW)
68426862

68436863
def test_insert_rows_json_w_ssl_error(self):
6864+
import requests.exceptions
68446865
from google.cloud.bigquery.dataset import DatasetReference
68456866
from google.cloud.bigquery.schema import SchemaField
68466867
from google.cloud.bigquery.table import Table
6847-
import requests.exceptions
68486868

68496869
PROJECT = "PROJECT"
68506870
DS_ID = "DS_ID"
@@ -9390,6 +9410,25 @@ def test_load_table_from_dataframe_w_higher_scale_decimal128_datatype(self):
93909410
SchemaField("x", "BIGNUMERIC", "NULLABLE", None),
93919411
)
93929412

9413+
def test_load_table_from_dataframe_emits_pending_deprecation_warning(self):
9414+
pandas = pytest.importorskip("pandas")
9415+
pytest.importorskip("pyarrow")
9416+
9417+
client = self._make_client()
9418+
dataframe = pandas.DataFrame({"x": [1, 2, 3]})
9419+
9420+
load_patch = mock.patch(
9421+
"google.cloud.bigquery.client.Client.load_table_from_file", autospec=True
9422+
)
9423+
get_table_patch = mock.patch(
9424+
"google.cloud.bigquery.client.Client.get_table", autospec=True
9425+
)
9426+
with load_patch, get_table_patch, pytest.warns(
9427+
PendingDeprecationWarning,
9428+
match="Loading DataFrames via google-cloud-bigquery is deprecated",
9429+
):
9430+
client.load_table_from_dataframe(dataframe, self.TABLE_REF)
9431+
93939432
# With autodetect specified, we pass the value as is. For more info, see
93949433
# https://github.com/googleapis/python-bigquery/issues/1228#issuecomment-1910946297
93959434
def test_load_table_from_json_basic_use(self):

0 commit comments

Comments
 (0)