Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions python/tests/test_dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,13 @@ def test_filter(df):
assert result.column(2) == pa.array([5])


def test_show_empty(df, capsys):
df_empty = df.filter(column("a") > literal(3))
df_empty.show()
captured = capsys.readouterr()
assert "DataFrame has no rows" in captured.out


def test_sort(df):
df = df.sort(column("b").sort(ascending=False))

Expand Down Expand Up @@ -2657,3 +2664,19 @@ def trigger_interrupt():

# Make sure the interrupt thread has finished
interrupt_thread.join(timeout=1.0)


def test_show_select_where_no_rows(capsys) -> None:
ctx = SessionContext()
df = ctx.sql("SELECT 1 WHERE 1=0")
df.show()
out = capsys.readouterr().out
assert "DataFrame has no rows" in out


def test_show_from_empty_batch(capsys) -> None:
ctx = SessionContext()
batch = pa.record_batch([pa.array([], type=pa.int32())], names=["a"])
ctx.create_dataframe([[batch]]).show()
out = capsys.readouterr().out
assert "| a |" in out
11 changes: 7 additions & 4 deletions src/dataframe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -998,10 +998,13 @@ impl PyDataFrame {
fn print_dataframe(py: Python, df: DataFrame) -> PyDataFusionResult<()> {
// Get string representation of record batches
let batches = wait_for_future(py, df.collect())??;
let batches_as_string = pretty::pretty_format_batches(&batches);
let result = match batches_as_string {
Ok(batch) => format!("DataFrame()\n{batch}"),
Err(err) => format!("Error: {:?}", err.to_string()),
let result = if batches.is_empty() {
"DataFrame has no rows".to_string()
} else {
match pretty::pretty_format_batches(&batches) {
Ok(batch) => format!("DataFrame()\n{batch}"),
Err(err) => format!("Error: {:?}", err.to_string()),
}
};

// Import the Python 'builtins' module to access the print function
Expand Down
Loading