Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions packages/google-cloud-bigquery/google/cloud/bigquery/client.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -163,6 +163,16 @@
# https://github.com/googleapis/python-bigquery/issues/438
_MIN_GET_QUERY_RESULTS_TIMEOUT = 120

_LOAD_TABLE_FROM_DATAFRAME_DEPRECATED = (
"Loading DataFrames via google-cloud-bigquery is deprecated. "
"For direct, optimized loading, please call 'pandas_gbq.to_gbq()' directly."
)

_INSERT_ROWS_FROM_DATAFRAME_DEPRECATED = (
"Inserting rows from DataFrames via google-cloud-bigquery is deprecated. "
"For direct, optimized access, please call 'pandas_gbq.to_gbq()' directly."
)

TIMEOUT_HEADER = "X-Server-Timeout"


Expand DownExpand Up@@ -2830,6 +2840,12 @@ def load_table_from_dataframe(
If ``job_config`` is not an instance of
:class:`~google.cloud.bigquery.job.LoadJobConfig` class.
"""
warnings.warn(
_LOAD_TABLE_FROM_DATAFRAME_DEPRECATED,
PendingDeprecationWarning,
stacklevel=2,
)

job_id = _make_job_id(job_id, job_id_prefix)

if job_config is not None:
Expand DownExpand Up@@ -3900,6 +3916,12 @@ def insert_rows_from_dataframe(
Raises:
ValueError: if table's schema is not set
"""
warnings.warn(
_INSERT_ROWS_FROM_DATAFRAME_DEPRECATED,
PendingDeprecationWarning,
stacklevel=2,
)

insert_results = []

chunk_count = int(math.ceil(len(dataframe) / chunk_size))
Expand Down
57 changes: 42 additions & 15 deletions packages/google-cloud-bigquery/tests/unit/test_client.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -6563,21 +6563,25 @@ def test_insert_rows_from_dataframe_w_explicit_none_insert_ids(self):
self.assertEqual(len(error_info), 1)
assert error_info[0] == [] # no chunk errors

EXPECTED_SENT_DATA = {
"rows": [
{"insertId": None, "json": {"name": "Little One", "adult": "false"}},
{"insertId": None, "json": {"name": "Young Gun", "adult": "true"}},
]
}
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)
Comment on lines +6566 to +6584

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

It looks like 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. This leaves the original test incomplete and without verification. Please restore the deleted assertions and append the new test properly.

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_listassertlen(actual_calls) ==1assertactual_calls[0] ==mock.call(
method="POST",
path=API_PATH,
data=EXPECTED_SENT_DATA,
timeout=DEFAULT_TIMEOUT,
)
deftest_insert_rows_from_dataframe_emits_pending_deprecation_warning(self):
pandas=pytest.importorskip("pandas")
fromgoogle.cloud.bigquery.schemaimportSchemaFieldfromgoogle.cloud.bigquery.tableimportTablecreds=_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"}])
withpytest.warns(
PendingDeprecationWarning,
match="Inserting rows from DataFrames via google-cloud-bigquery is deprecated",
):
client.insert_rows_from_dataframe(table, dataframe)


def test_insert_rows_json_default_behavior(self):
from google.cloud.bigquery.dataset import DatasetReference
Expand DownExpand Up@@ -6841,10 +6845,10 @@ def test_insert_rows_w_wrong_arg(self):
client.insert_rows_json(table, ROW)

def test_insert_rows_json_w_ssl_error(self):
import requests.exceptions
from google.cloud.bigquery.dataset import DatasetReference
from google.cloud.bigquery.schema import SchemaField
from google.cloud.bigquery.table import Table
import requests.exceptions

PROJECT = "PROJECT"
DS_ID = "DS_ID"
Expand DownExpand Up@@ -9390,6 +9394,29 @@ def test_load_table_from_dataframe_w_higher_scale_decimal128_datatype(self):
SchemaField("x", "BIGNUMERIC", "NULLABLE", None),
)

def test_load_table_from_dataframe_emits_pending_deprecation_warning(self):
pandas = pytest.importorskip("pandas")
pytest.importorskip("pyarrow")

client = self._make_client()
dataframe = pandas.DataFrame({"x": [1, 2, 3]})

load_patch = mock.patch(
"google.cloud.bigquery.client.Client.load_table_from_file", autospec=True
)
get_table_patch = mock.patch(
"google.cloud.bigquery.client.Client.get_table", autospec=True
)
with (
load_patch,
get_table_patch,
pytest.warns(
PendingDeprecationWarning,
match="Loading DataFrames via google-cloud-bigquery is deprecated",
),
):
client.load_table_from_dataframe(dataframe, self.TABLE_REF)

# With autodetect specified, we pass the value as is. For more info, see
# https://github.com/googleapis/python-bigquery/issues/1228#issuecomment-1910946297
def test_load_table_from_json_basic_use(self):
Expand Down
Loading