Skip to content

Commit

Permalink
fix: raise ValueError when read_pandas() receives a bigframes `Da…
Browse files Browse the repository at this point in the history
…taFrame` (#447)

* fix: raise `ValueError` when `read_pandas()` receives a bigframes `DataFrame`

* make actually a unit test
  • Loading branch information
tswast authored Mar 15, 2024
1 parent 40ddb69 commit b28f9fd
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
6 changes: 6 additions & 0 deletions bigframes/session/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -916,6 +916,12 @@ def read_pandas(self, pandas_dataframe: pandas.DataFrame) -> dataframe.DataFrame
def _read_pandas(
self, pandas_dataframe: pandas.DataFrame, api_name: str
) -> dataframe.DataFrame:
if isinstance(pandas_dataframe, dataframe.DataFrame):
raise ValueError(
"read_pandas() expects a pandas.DataFrame, but got a "
"bigframes.pandas.DataFrame."
)

if (
pandas_dataframe.size < MAX_INLINE_DF_SIZE
# TODO(swast): Workaround data types limitation in inline data.
Expand Down
15 changes: 15 additions & 0 deletions tests/unit/session/test_io_pandas.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
# limitations under the License.

import datetime
import re
from typing import Dict, Union
import unittest.mock as mock

import geopandas # type: ignore
import numpy
Expand All @@ -24,8 +26,11 @@
import pytest

import bigframes.features
import bigframes.pandas
import bigframes.session._io.pandas

from .. import resources

_LIST_OF_SCALARS = [
[1, 2, 3],
[],
Expand Down Expand Up @@ -475,3 +480,13 @@ def test_arrow_to_pandas_wrong_size_dtypes(
):
with pytest.raises(ValueError, match=f"Number of types {len(dtypes)}"):
bigframes.session._io.pandas.arrow_to_pandas(arrow_table, dtypes)


def test_read_pandas_with_bigframes_dataframe():
session = resources.create_bigquery_session()
df = mock.create_autospec(bigframes.pandas.DataFrame, instance=True)

with pytest.raises(
ValueError, match=re.escape("read_pandas() expects a pandas.DataFrame")
):
session.read_pandas(df)

0 comments on commit b28f9fd

Please sign in to comment.