Skip to content

Commit 5a7e82e

Browse files
plamutemar-kar
authored andcommitted
Deprecate automatic schema conversion (googleapis#9176)
1 parent a0b365c commit 5a7e82e

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

bigquery/google/cloud/bigquery/client.py

+10
Original file line numberDiff line numberDiff line change
@@ -1572,6 +1572,16 @@ def load_table_from_dataframe(
15721572
dataframe, job_config.schema
15731573
)
15741574

1575+
if not job_config.schema:
1576+
# the schema could not be fully detected
1577+
warnings.warn(
1578+
"Schema could not be detected for all columns. Loading from a "
1579+
"dataframe without a schema will be deprecated in the future, "
1580+
"please provide a schema.",
1581+
PendingDeprecationWarning,
1582+
stacklevel=2,
1583+
)
1584+
15751585
tmpfd, tmppath = tempfile.mkstemp(suffix="_job_{}.parquet".format(job_id[:8]))
15761586
os.close(tmpfd)
15771587

bigquery/tests/unit/test_client.py

+35
Original file line numberDiff line numberDiff line change
@@ -5579,6 +5579,41 @@ def test_load_table_from_dataframe_unknown_table(self):
55795579
job_config=mock.ANY,
55805580
)
55815581

5582+
@unittest.skipIf(pandas is None, "Requires `pandas`")
5583+
@unittest.skipIf(pyarrow is None, "Requires `pyarrow`")
5584+
def test_load_table_from_dataframe_no_schema_warning(self):
5585+
client = self._make_client()
5586+
5587+
# Pick at least one column type that translates to Pandas dtype
5588+
# "object". A string column matches that.
5589+
records = [{"name": "Monty", "age": 100}, {"name": "Python", "age": 60}]
5590+
dataframe = pandas.DataFrame(records)
5591+
5592+
get_table_patch = mock.patch(
5593+
"google.cloud.bigquery.client.Client.get_table",
5594+
autospec=True,
5595+
side_effect=google.api_core.exceptions.NotFound("Table not found"),
5596+
)
5597+
load_patch = mock.patch(
5598+
"google.cloud.bigquery.client.Client.load_table_from_file", autospec=True
5599+
)
5600+
pyarrow_patch = mock.patch("google.cloud.bigquery.client.pyarrow", None)
5601+
catch_warnings = warnings.catch_warnings(record=True)
5602+
5603+
with get_table_patch, load_patch, pyarrow_patch, catch_warnings as warned:
5604+
client.load_table_from_dataframe(
5605+
dataframe, self.TABLE_REF, location=self.LOCATION
5606+
)
5607+
5608+
matches = [
5609+
warning
5610+
for warning in warned
5611+
if warning.category in (DeprecationWarning, PendingDeprecationWarning)
5612+
and "could not be detected" in str(warning)
5613+
and "please provide a schema" in str(warning)
5614+
]
5615+
assert matches, "A missing schema deprecation warning was not raised."
5616+
55825617
@unittest.skipIf(pandas is None, "Requires `pandas`")
55835618
@unittest.skipIf(pyarrow is None, "Requires `pyarrow`")
55845619
def test_load_table_from_dataframe_struct_fields_error(self):

0 commit comments

Comments
 (0)