Skip to content

Commit 3676ac7

Browse files
QDB-16881 - Exception when reading empty table with qdbpd.read_dataframe
1 parent 2d72591 commit 3676ac7

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

quasardb/pandas/__init__.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,17 @@ def read_dataframe(conn: quasardb.Cluster, table, **kwargs):
349349
# dataframes.
350350
dfs = stream_dataframe(conn, table, **kwargs)
351351

352-
return pd.concat(dfs)
352+
# if result of stream_dataframe is empty this could result in ValueError on pd.concat()
353+
# as stream_dataframe is a generator there is no easy way to check for this condition without evaluation
354+
# the most simple way is to catch the ValueError and return an empty DataFrame
355+
try:
356+
return pd.concat(dfs, copy=False)
357+
except ValueError as e:
358+
logger.error(
359+
"Error while concatenating dataframes. This can happen if result set is empty. Returning empty dataframe. Error: %s",
360+
e,
361+
)
362+
return pd.DataFrame()
353363

354364

355365
def _extract_columns(df, cinfos):

tests/test_pandas.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -665,3 +665,22 @@ def test_retries(
665665
)
666666

667667
assert seen == True
668+
669+
670+
def test_read_dataframe_empty_table_sc16881(qdbd_connection, table_name):
671+
"""
672+
Ensures qdbpd.read_dataframe returns an empty DataFrame when the table exists but contains no data.
673+
674+
Previously this raised ValueError due to pd.concat() on an empty result set.
675+
"""
676+
table = qdbd_connection.ts(table_name)
677+
678+
table_config = [
679+
quasardb.ColumnInfo(quasardb.ColumnType.Double, "d"),
680+
]
681+
682+
table.create(table_config)
683+
684+
df = qdbpd.read_dataframe(qdbd_connection, table)
685+
686+
assert df.empty

0 commit comments

Comments
 (0)