diff --git a/quasardb/pandas/__init__.py b/quasardb/pandas/__init__.py index 031e2d42..224d0a9f 100644 --- a/quasardb/pandas/__init__.py +++ b/quasardb/pandas/__init__.py @@ -349,7 +349,17 @@ def read_dataframe(conn: quasardb.Cluster, table, **kwargs): # dataframes. dfs = stream_dataframe(conn, table, **kwargs) - return pd.concat(dfs) + # if result of stream_dataframe is empty this could result in ValueError on pd.concat() + # as stream_dataframe is a generator there is no easy way to check for this condition without evaluation + # the most simple way is to catch the ValueError and return an empty DataFrame + try: + return pd.concat(dfs, copy=False) + except ValueError as e: + logger.error( + "Error while concatenating dataframes. This can happen if result set is empty. Returning empty dataframe. Error: %s", + e, + ) + return pd.DataFrame() def _extract_columns(df, cinfos): diff --git a/tests/test_pandas.py b/tests/test_pandas.py index f9a2b0d2..25c505f6 100644 --- a/tests/test_pandas.py +++ b/tests/test_pandas.py @@ -665,3 +665,22 @@ def test_retries( ) assert seen == True + + +def test_read_dataframe_empty_table_sc16881(qdbd_connection, table_name): + """ + Ensures qdbpd.read_dataframe returns an empty DataFrame when the table exists but contains no data. + + Previously this raised ValueError due to pd.concat() on an empty result set. + """ + table = qdbd_connection.ts(table_name) + + table_config = [ + quasardb.ColumnInfo(quasardb.ColumnType.Double, "d"), + ] + + table.create(table_config) + + df = qdbpd.read_dataframe(qdbd_connection, table) + + assert df.empty