Skip to content

Commit

Permalink
feat: to_gbq fails with TypeError if passing in a bigframes DataF…
Browse files Browse the repository at this point in the history
…rame 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>
  • Loading branch information
tswast and gcf-owl-bot[bot] authored Dec 12, 2024
1 parent cc90edd commit 5004d08
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
8 changes: 8 additions & 0 deletions pandas_gbq/gbq.py
Original file line number Diff line number Diff line change
Expand Up @@ -1091,6 +1091,14 @@ def to_gbq(
.. versionadded:: 0.23.3
"""

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

_test_google_api_imports()

from google.api_core import exceptions as google_exceptions
Expand Down
19 changes: 19 additions & 0 deletions tests/unit/test_to_gbq.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,16 @@
from pandas_gbq import gbq


class FakeDataFrame:
"""A fake bigframes DataFrame to avoid depending on bigframes."""

def to_gbq(self):
"""Fake to_gbq() to mimic a bigframes object."""

def to_pandas(self):
"""Fake to_pandas() to mimic a bigframes object."""


@pytest.fixture
def expected_load_method(mock_bigquery_client):
return mock_bigquery_client.load_table_from_dataframe
Expand Down Expand Up @@ -66,6 +76,15 @@ def test_to_gbq_load_method_translates_exception(
expected_load_method.assert_called_once()


def test_to_gbq_with_bigframes_raises_typeerror():
dataframe = FakeDataFrame()

with pytest.raises(
TypeError, match=r"Expected a pandas.DataFrame, but got .+FakeDataFrame"
):
gbq.to_gbq(dataframe, "my_dataset.my_table", project_id="myproj")


def test_to_gbq_with_if_exists_append(mock_bigquery_client, expected_load_method):
from google.cloud.bigquery import SchemaField

Expand Down

0 comments on commit 5004d08

Please sign in to comment.