Skip to content

[SPARK-47365][PYTHON] Add toArrow() DataFrame method to PySpark - #45481

Closed
ianmcook wants to merge 28 commits into
apache:masterfrom
ianmcook:SPARK-47365
Closed

[SPARK-47365][PYTHON] Add toArrow() DataFrame method to PySpark#45481
ianmcook wants to merge 28 commits into
apache:masterfrom
ianmcook:SPARK-47365

Conversation

@ianmcook

@ianmcookianmcook commented Mar 12, 2024

Copy link
Copy Markdown
Member

What changes were proposed in this pull request?

  • Add a PySpark DataFrame method toArrow() which returns the contents of the DataFrame as a PyArrow Table, for both local Spark and Spark Connect.
  • Add a new entry to the Apache Arrow in PySpark user guide page describing usage of the toArrow() method.
  • Add a new option to the method _collect_as_arrow() to provide more useful output when there are zero records returned. (This keeps the implementation of toArrow() simpler.)

Why are the changes needed?

In the Apache Arrow community, we hear from a lot of users who want to return the contents of a PySpark DataFrame as a PyArrow Table. Currently the only documented way to do this is to return the contents as a pandas DataFrame, then use PyArrow (pa) to convert that to a PyArrow Table.

pa.Table.from_pandas(df.toPandas())

But going through pandas adds significant overhead which is easily avoided since internally toPandas() already converts the contents of Spark DataFrame to Arrow format as an intermediate step when spark.sql.execution.arrow.pyspark.enabled is true.

Currently it is also possible to use the experimental _collect_as_arrow() method to return the contents of a PySpark DataFrame as a list of PyArrow RecordBatches. This PR adds a new non-experimental method toArrow() which returns the more user-friendly PyArrow Table object.

This PR also adds a new argument empty_list_if_zero_records to the experimental method _collect_as_arrow() to control what the method returns in the case when the result data has zero rows. If set to True (the default), the existing behavior is preserved, and the method returns an empty Python list. If set to False, the method returns returns a length-one list containing an empty Arrow RecordBatch which includes the schema. This is used by toArrow() which requires the schema even if the data has zero rows.

For Spark Connect, there is already a SparkSession.client.to_table() method that returns a PyArrow table. This PR uses that to expose toArrow() for Spark Connect.

Does this PR introduce any user-facing change?

  • It adds a DataFrame method toArrow() to the PySpark SQL DataFrame API.
  • It adds a new argument empty_list_if_zero_records to the experimental DataFrame method _collect_as_arrow() with a default value which preserves the method's existing behavior.
  • It exposes toArrow() for Spark Connect, via the existing SparkSession.client.to_table() method.
  • It does not introduce any other user-facing changes.

How was this patch tested?

This adds a new test and a new helper function for the test in pyspark/sql/tests/test_arrow.py.

Was this patch authored or co-authored using generative AI tooling?

No

Comment threadpython/pyspark/sql/pandas/conversion.py Outdated
@ianmcookianmcook changed the title [WIP][SPARK-47365] Add toArrow() DataFrame method to PySpark[WIP][SPARK-47365] Add _toArrow() DataFrame method to PySparkMar 19, 2024
@ianmcook
ianmcook marked this pull request as ready for review March 19, 2024 17:31
@ianmcookianmcook changed the title [WIP][SPARK-47365] Add _toArrow() DataFrame method to PySpark[SPARK-47365] Add _toArrow() DataFrame method to PySparkMar 21, 2024
@ianmcookianmcook changed the title [SPARK-47365] Add _toArrow() DataFrame method to PySpark[SPARK-47365][PYTHON] Add _toArrow() DataFrame method to PySparkMar 21, 2024
@ianmcook

Copy link
Copy Markdown
MemberAuthor

Comment threadpython/pyspark/sql/connect/dataframe.py Outdated
Comment threadpython/pyspark/sql/connect/dataframe.py Outdated
Comment threadpython/pyspark/sql/connect/dataframe.py Outdated
@ianmcookianmcook changed the title [SPARK-47365][PYTHON] Add _toArrow() DataFrame method to PySpark[SPARK-47365][PYTHON] Add toArrowTable() DataFrame method to PySparkMay 7, 2024
Comment threadpython/pyspark/sql/pandas/conversion.py Outdated
@HyukjinKwon

Copy link
Copy Markdown
Member

Okay I'm fine with this

@ianmcook

ianmcook commented May 9, 2024

Copy link
Copy Markdown
MemberAuthor

Thanks. I rebased. I also took a closer look at your changes in #46129 and made a few changes for consistency with the new structure you introduced there:

  • I added a definition for toArrow in python/pyspark/sql/classic/dataframe.py.
  • I added a placeholder definition for toArrow in python/pyspark/sql/dataframe.py
  • I moved the docstring to there from python/pyspark/sql/pandas/conversion.py.

I also added .. versionadded:: 4.0.0 in the docstring.

Please let me know if that all looks OK.

Comment threadpython/pyspark/sql/dataframe.py
Comment threadpython/docs/source/reference/pyspark.sql/dataframe.rst Outdated
Comment threadpython/pyspark/sql/dataframe.py
@ianmcookianmcook changed the title [SPARK-47365][PYTHON] Add toArrowTable() DataFrame method to PySpark[SPARK-47365][PYTHON] Add toArrow() DataFrame method to PySparkMay 9, 2024
@HyukjinKwon

Copy link
Copy Markdown
Member

Merged to master.

HyukjinKwon pushed a commit that referenced this pull request Jun 2, 2024
### What changes were proposed in this pull request?
- Add support for passing a PyArrow Table to `createDataFrame()`.
- Document this on the **Apache Arrow in PySpark** user guide page.
- Fix an issue with timestamp and struct columns in `toArrow()`.
### Why are the changes needed?
This seems like a logical next step after the addition of a `toArrow()` DataFrame method in #45481.
### Does this PR introduce _any_ user-facing change?
Users will have the ability to pass PyArrow Tables to `createDataFrame()`. There are no changes to the parameters of `createDataFrame()`. The only difference is that `data` can now be a PyArrow Table.
### How was this patch tested?
Many tests were added, for Spark Classic and Spark Connect. I ran the tests locally with older versions of PyArrow installed (going back to 10.0).
### Was this patch authored or co-authored using generative AI tooling?
No
Closes#46529 from ianmcook/SPARK-48220.
Authored-by: Ian Cook <ianmcook@gmail.com>
Signed-off-by: Hyukjin Kwon <gurwls223@apache.org>
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants

@ianmcook@HyukjinKwon@grundprinzip@xinrong-meng