Skip to content

Commit 50c353d

Browse files
committed
Add test for displaying empty DataFrame and improve empty check in print_dataframe
1 parent ed6bb75 commit 50c353d

2 files changed

Lines changed: 16 additions & 4 deletions

File tree

python/tests/test_dataframe.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1464,6 +1464,14 @@ def test_empty_to_pandas(df):
14641464
assert set(pandas_df.columns) == {"a", "b", "c"}
14651465

14661466

1467+
def test_show_empty_dataframe(df, capsys):
1468+
"""Ensure showing an empty DataFrame prints a helpful message."""
1469+
empty_df = df.limit(0)
1470+
empty_df.show()
1471+
captured = capsys.readouterr()
1472+
assert "Empty DataFrame" in captured.out
1473+
1474+
14671475
def test_to_polars(df):
14681476
# Skip test if polars is not installed
14691477
pl = pytest.importorskip("polars")

src/dataframe.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1046,10 +1046,14 @@ impl Iterator for ArrowStreamReader {
10461046
fn print_dataframe(py: Python, df: DataFrame) -> PyDataFusionResult<()> {
10471047
// Get string representation of record batches
10481048
let batches = wait_for_future(py, df.collect())??;
1049-
let batches_as_string = pretty::pretty_format_batches(&batches);
1050-
let result = match batches_as_string {
1051-
Ok(batch) => format!("DataFrame()\n{batch}"),
1052-
Err(err) => format!("Error: {:?}", err.to_string()),
1049+
let is_empty = batches.is_empty() || batches.iter().all(|b| b.num_rows() == 0);
1050+
let result = if is_empty {
1051+
"Empty DataFrame".to_string()
1052+
} else {
1053+
match pretty::pretty_format_batches(&batches) {
1054+
Ok(batch) => format!("DataFrame()\n{batch}"),
1055+
Err(err) => format!("Error: {:?}", err.to_string()),
1056+
}
10531057
};
10541058

10551059
// Import the Python 'builtins' module to access the print function

0 commit comments

Comments
 (0)