feat(bigquery): add PendingDeprecationWarning for from_dataframe methods - #18048
Conversation
There was a problem hiding this comment.
Code Review
This pull request deprecates loading and inserting rows from DataFrames via google-cloud-bigquery by introducing PendingDeprecationWarning warnings and adding corresponding unit tests. Feedback on the changes highlights two issues: first, assertions for an existing test (test_insert_rows_from_dataframe_w_explicit_none_insert_ids) were accidentally deleted and need to be restored; second, the use of parenthesized context managers in the tests breaks compatibility with Python 3.8 and should be replaced with standard multi-context with statements.
| def test_insert_rows_from_dataframe_emits_pending_deprecation_warning(self): | ||
| pandas = pytest.importorskip("pandas") | ||
| from google.cloud.bigquery.schema import SchemaField | ||
| from google.cloud.bigquery.table import Table | ||
|
|
||
| actual_calls = conn.api_request.call_args_list | ||
| assert len(actual_calls) == 1 | ||
| assert actual_calls[0] == mock.call( | ||
| method="POST", | ||
| path=API_PATH, | ||
| data=EXPECTED_SENT_DATA, | ||
| timeout=DEFAULT_TIMEOUT, | ||
| ) | ||
| creds = _make_credentials() | ||
| http = object() | ||
| client = self._make_one(project=self.PROJECT, credentials=creds, _http=http) | ||
| client._connection = make_connection({}, {}) | ||
|
|
||
| schema = [SchemaField("name", "STRING", mode="REQUIRED")] | ||
| table = Table(self.TABLE_REF, schema=schema) | ||
| dataframe = pandas.DataFrame([{"name": "Alice"}]) | ||
|
|
||
| with pytest.warns( | ||
| PendingDeprecationWarning, | ||
| match="Inserting rows from DataFrames via google-cloud-bigquery is deprecated", | ||
| ): | ||
| client.insert_rows_from_dataframe(table, dataframe) |
There was a problem hiding this comment.
The assertions for test_insert_rows_from_dataframe_w_explicit_none_insert_ids were accidentally deleted when adding the new test test_insert_rows_from_dataframe_emits_pending_deprecation_warning. Please restore the deleted assertions and append the new test case.
EXPECTED_SENT_DATA = {
"rows": [
{"insertId": None, "json": {"name": "Little One", "adult": "false"}},
{"insertId": None, "json": {"name": "Young Gun", "adult": "true"}},
]
}
actual_calls = conn.api_request.call_args_list
assert len(actual_calls) == 1
assert actual_calls[0] == mock.call(
method="POST",
path=API_PATH,
data=EXPECTED_SENT_DATA,
timeout=DEFAULT_TIMEOUT,
)
def test_insert_rows_from_dataframe_emits_pending_deprecation_warning(self):
pandas = pytest.importorskip("pandas")
from google.cloud.bigquery.schema import SchemaField
from google.cloud.bigquery.table import Table
creds = _make_credentials()
http = object()
client = self._make_one(project=self.PROJECT, credentials=creds, _http=http)
client._connection = make_connection({}, {})
schema = [SchemaField("name", "STRING", mode="REQUIRED")]
table = Table(self.TABLE_REF, schema=schema)
dataframe = pandas.DataFrame([{"name": "Alice"}])
with pytest.warns(
PendingDeprecationWarning,
match="Inserting rows from DataFrames via google-cloud-bigquery is deprecated",
):
client.insert_rows_from_dataframe(table, dataframe)| with ( | ||
| load_patch, | ||
| get_table_patch, | ||
| pytest.warns( | ||
| PendingDeprecationWarning, | ||
| match="Loading DataFrames via google-cloud-bigquery is deprecated", | ||
| ), | ||
| ): |
There was a problem hiding this comment.
Parenthesized context managers are only supported in Python 3.9+. To maintain compatibility with Python 3.8 (which is still supported by this package), use a standard multi-context with statement without parentheses.
with load_patch, get_table_patch, pytest.warns(
PendingDeprecationWarning,
match="Loading DataFrames via google-cloud-bigquery is deprecated",
):
Adds
PendingDeprecationWarningtoClient.load_table_from_dataframe()andClient.insert_rows_from_dataframe()ingoogle-cloud-bigquery.This alerts users to adopt direct
pandas_gbq.to_gbq()entry points ahead of future deprecation phases per thepandas-gbqmigration design.PendingDeprecationWarningtoload_table_from_dataframeandinsert_rows_from_dataframeingoogle/cloud/bigquery/client.py.tests/unit/test_client.py.Fixes #<526614511 🦕