This is an automated email from the ASF dual-hosted git repository.
jorisvandenbossche pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/arrow.git
The following commit(s) were added to refs/heads/main by this push:
new 9afa848e64 GH-37050: [Python][Interchange protocol] Add a workaround
for empty dataframes (#38037)
9afa848e64 is described below
commit 9afa848e640fbd7539e7a188657fcf2ae6e45245
Author: Alenka Frim <[email protected]>
AuthorDate: Tue Oct 10 13:18:35 2023 +0200
GH-37050: [Python][Interchange protocol] Add a workaround for empty
dataframes (#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: #37050
Authored-by: AlenkaF <[email protected]>
Signed-off-by: Joris Van den Bossche <[email protected]>
---
python/pyarrow/interchange/from_dataframe.py | 4 ++++
python/pyarrow/tests/interchange/test_conversion.py | 7 +++++++
2 files changed, 11 insertions(+)
diff --git a/python/pyarrow/interchange/from_dataframe.py
b/python/pyarrow/interchange/from_dataframe.py
index e97e91e44f..3767b18f2a 100644
--- a/python/pyarrow/interchange/from_dataframe.py
+++ b/python/pyarrow/interchange/from_dataframe.py
@@ -136,6 +136,10 @@ def _from_dataframe(df: DataFrameObject, allow_copy=True):
batch = protocol_df_chunk_to_pyarrow(chunk, allow_copy)
batches.append(batch)
+ if not batches:
+ batch = protocol_df_chunk_to_pyarrow(df)
+ batches.append(batch)
+
return pa.Table.from_batches(batches)
diff --git a/python/pyarrow/tests/interchange/test_conversion.py
b/python/pyarrow/tests/interchange/test_conversion.py
index 225210d8c7..b1e0fa0d1c 100644
--- a/python/pyarrow/tests/interchange/test_conversion.py
+++ b/python/pyarrow/tests/interchange/test_conversion.py
@@ -513,3 +513,10 @@ def test_allow_copy_false_bool_categorical():
df = df.astype("category")
with pytest.raises(RuntimeError):
pi.from_dataframe(df, allow_copy=False)
+
+
+def test_empty_dataframe():
+ schema = pa.schema([('col1', pa.int8())])
+ df = pa.table([[]], schema=schema)
+ dfi = df.__dataframe__()
+ assert pi.from_dataframe(dfi) == df