Skip to content

Commit 5004d08

Browse files
feat: to_gbq fails with TypeError if passing in a bigframes DataFrame object (#833)
* feat: `to_gbq` fails with `TypeError` if passing in a bigframes DataFrame object * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md --------- Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
1 parent cc90edd commit 5004d08

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

pandas_gbq/gbq.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1091,6 +1091,14 @@ def to_gbq(
10911091
.. versionadded:: 0.23.3
10921092
"""
10931093

1094+
# If we get a bigframes.pandas.DataFrame object, it may be possible to use
1095+
# the code paths here, but it could potentially be quite expensive because
1096+
# of the queries involved in type detection. It would be safer just to
1097+
# fail early if there are bigframes-y methods available.
1098+
# https://github.com/googleapis/python-bigquery-pandas/issues/824
1099+
if hasattr(dataframe, "to_pandas") and hasattr(dataframe, "to_gbq"):
1100+
raise TypeError(f"Expected a pandas.DataFrame, but got {repr(type(dataframe))}")
1101+
10941102
_test_google_api_imports()
10951103

10961104
from google.api_core import exceptions as google_exceptions

tests/unit/test_to_gbq.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,16 @@
1111
from pandas_gbq import gbq
1212

1313

14+
class FakeDataFrame:
15+
"""A fake bigframes DataFrame to avoid depending on bigframes."""
16+
17+
def to_gbq(self):
18+
"""Fake to_gbq() to mimic a bigframes object."""
19+
20+
def to_pandas(self):
21+
"""Fake to_pandas() to mimic a bigframes object."""
22+
23+
1424
@pytest.fixture
1525
def expected_load_method(mock_bigquery_client):
1626
return mock_bigquery_client.load_table_from_dataframe
@@ -66,6 +76,15 @@ def test_to_gbq_load_method_translates_exception(
6676
expected_load_method.assert_called_once()
6777

6878

79+
def test_to_gbq_with_bigframes_raises_typeerror():
80+
dataframe = FakeDataFrame()
81+
82+
with pytest.raises(
83+
TypeError, match=r"Expected a pandas.DataFrame, but got .+FakeDataFrame"
84+
):
85+
gbq.to_gbq(dataframe, "my_dataset.my_table", project_id="myproj")
86+
87+
6988
def test_to_gbq_with_if_exists_append(mock_bigquery_client, expected_load_method):
7089
from google.cloud.bigquery import SchemaField
7190

0 commit comments

Comments
 (0)