Skip to content

Commit 7643931

Browse files
committed
Refactor DataFrame printing to ensure headers are displayed for empty DataFrames
1 parent ab1dd7e commit 7643931

File tree

2 files changed

+20
-5
lines changed

2 files changed

+20
-5
lines changed

python/tests/test_dataframe.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1496,11 +1496,12 @@ def test_show_no_batches(capsys):
14961496

14971497

14981498
def test_show_empty_dataframe(df, capsys):
1499-
"""Ensure showing an empty DataFrame prints a helpful message."""
1499+
"""Ensure showing an empty DataFrame still prints headers."""
15001500
empty_df = df.limit(0)
15011501
empty_df.show()
15021502
captured = capsys.readouterr()
1503-
assert "Empty DataFrame" in captured.out
1503+
assert "| a | b | c |" in captured.out
1504+
assert "Empty DataFrame" not in captured.out
15041505

15051506

15061507
def test_to_polars(df):

src/dataframe.rs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1097,10 +1097,24 @@ impl Iterator for ArrowStreamReader {
10971097

10981098
/// Print DataFrame
10991099
fn print_dataframe(py: Python, df: DataFrame) -> PyDataFusionResult<()> {
1100+
// Get the schema before consuming the DataFrame
1101+
let schema: SchemaRef = Arc::new(df.schema().clone().into());
1102+
11001103
// Get string representation of record batches
1101-
let batches = wait_for_future(py, df.collect())??;
1102-
let is_empty = batches.is_empty() || batches.iter().all(|b| b.num_rows() == 0);
1103-
let result = if is_empty {
1104+
let collected_batches = wait_for_future(py, df.collect())??;
1105+
1106+
let batches =
1107+
if collected_batches.is_empty() || collected_batches.iter().all(|b| b.num_rows() == 0) {
1108+
if schema.fields().is_empty() {
1109+
vec![]
1110+
} else {
1111+
vec![RecordBatch::new_empty(schema.clone())]
1112+
}
1113+
} else {
1114+
collected_batches
1115+
};
1116+
1117+
let result = if batches.is_empty() {
11041118
"Empty DataFrame".to_string()
11051119
} else {
11061120
match pretty::pretty_format_batches(&batches) {

0 commit comments

Comments
 (0)