Uh oh!
There was an error while loading. Please reload this page.
[SPARK-47365][PYTHON] Add toArrow() DataFrame method to PySpark - #45481
[SPARK-47365][PYTHON] Add toArrow() DataFrame method to PySpark#45481ianmcook wants to merge 28 commits into
Conversation
Uh oh!
There was an error while loading. Please reload this page.
ianmcook
commented
Mar 26, 2024
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
HyukjinKwon
commented
May 7, 2024
Okay I'm fine with this |
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 also added Please let me know if that all looks OK. |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Co-authored-by: Hyukjin Kwon <gurwls223@gmail.com>
HyukjinKwon
commented
May 9, 2024
Merged to master. |
### 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>
What changes were proposed in this pull request?
toArrow()which returns the contents of the DataFrame as a PyArrow Table, for both local Spark and Spark Connect.toArrow()method._collect_as_arrow()to provide more useful output when there are zero records returned. (This keeps the implementation oftoArrow()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.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 whenspark.sql.execution.arrow.pyspark.enabledistrue.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 methodtoArrow()which returns the more user-friendly PyArrow Table object.This PR also adds a new argument
empty_list_if_zero_recordsto the experimental method_collect_as_arrow()to control what the method returns in the case when the result data has zero rows. If set toTrue(the default), the existing behavior is preserved, and the method returns an empty Python list. If set toFalse, the method returns returns a length-one list containing an empty Arrow RecordBatch which includes the schema. This is used bytoArrow()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 exposetoArrow()for Spark Connect.Does this PR introduce any user-facing change?
toArrow()to the PySpark SQL DataFrame API.empty_list_if_zero_recordsto the experimental DataFrame method_collect_as_arrow()with a default value which preserves the method's existing behavior.toArrow()for Spark Connect, via the existingSparkSession.client.to_table()method.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