Skip to content

Commit a18259a

Browse files
AlenkaFdgreiss
authored andcommitted
apacheGH-37050: [Python][Interchange protocol] Add a workaround for empty dataframes (apache#38037)
### Rationale for this change The implementation of the DataFrame Interchange Protocol does not currently support consumption of dataframes with 0 number of chunks (empty dataframes). ### What changes are included in this PR? Add a workaround to not error in this case. ### Are these changes tested? Yes, added `test_empty_dataframe` in `python/pyarrow/tests/interchange/test_conversion.py`. ### Are there any user-facing changes? No. * Closes: apache#37050 Authored-by: AlenkaF <frim.alenka@gmail.com> Signed-off-by: Joris Van den Bossche <jorisvandenbossche@gmail.com>
1 parent 75510a2 commit a18259a

2 files changed

Lines changed: 11 additions & 0 deletions

File tree

python/pyarrow/interchange/from_dataframe.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,10 @@ def _from_dataframe(df: DataFrameObject, allow_copy=True):
136136
batch = protocol_df_chunk_to_pyarrow(chunk, allow_copy)
137137
batches.append(batch)
138138

139+
if not batches:
140+
batch = protocol_df_chunk_to_pyarrow(df)
141+
batches.append(batch)
142+
139143
return pa.Table.from_batches(batches)
140144

141145

python/pyarrow/tests/interchange/test_conversion.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -513,3 +513,10 @@ def test_allow_copy_false_bool_categorical():
513513
df = df.astype("category")
514514
with pytest.raises(RuntimeError):
515515
pi.from_dataframe(df, allow_copy=False)
516+
517+
518+
def test_empty_dataframe():
519+
schema = pa.schema([('col1', pa.int8())])
520+
df = pa.table([[]], schema=schema)
521+
dfi = df.__dataframe__()
522+
assert pi.from_dataframe(dfi) == df

0 commit comments

Comments
 (0)