From b28f9fdd9681b3c9783a6e52322b70093e0283ec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim=20Swe=C3=B1a=20=28Swast=29?= Date: Fri, 15 Mar 2024 16:11:38 -0500 Subject: [PATCH] fix: raise `ValueError` when `read_pandas()` receives a bigframes `DataFrame` (#447) * fix: raise `ValueError` when `read_pandas()` receives a bigframes `DataFrame` * make actually a unit test --- bigframes/session/__init__.py | 6 ++++++ tests/unit/session/test_io_pandas.py | 15 +++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/bigframes/session/__init__.py b/bigframes/session/__init__.py index 2bc612bdbe..40831292de 100644 --- a/bigframes/session/__init__.py +++ b/bigframes/session/__init__.py @@ -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. diff --git a/tests/unit/session/test_io_pandas.py b/tests/unit/session/test_io_pandas.py index 959cccd80e..81d02466ef 100644 --- a/tests/unit/session/test_io_pandas.py +++ b/tests/unit/session/test_io_pandas.py @@ -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 @@ -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], [], @@ -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)