Skip to content

feat: to_gbq fails with TypeError if passing in a bigframes DataFrame object #833

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Dec 12, 2024
Merged
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
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
Loading