Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
wants to merge 28 commits into from

Conversation

ianmcook
Copy link
Member

@ianmcook ianmcook commented Mar 12, 2024

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

@ianmcook ianmcook changed the title [WIP][SPARK-47365] Add toArrow() DataFrame method to PySpark [WIP][SPARK-47365] Add _toArrow() DataFrame method to PySpark Mar 19, 2024
@ianmcook ianmcook marked this pull request as ready for review March 19, 2024 17:31
@ianmcook ianmcook requested a review from HyukjinKwon March 19, 2024 17:38
@ianmcook ianmcook changed the title [WIP][SPARK-47365] Add _toArrow() DataFrame method to PySpark [SPARK-47365] Add _toArrow() DataFrame method to PySpark Mar 21, 2024
@ianmcook ianmcook changed the title [SPARK-47365] Add _toArrow() DataFrame method to PySpark [SPARK-47365][PYTHON] Add _toArrow() DataFrame method to PySpark Mar 21, 2024
@ianmcook
Copy link
Member Author

@ianmcook ianmcook changed the title [SPARK-47365][PYTHON] Add _toArrow() DataFrame method to PySpark [SPARK-47365][PYTHON] Add toArrowTable() DataFrame method to PySpark May 7, 2024
@HyukjinKwon
Copy link
Member

Okay I'm fine with this

@ianmcook
Copy link
Member Author

ianmcook commented May 9, 2024

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.

@ianmcook ianmcook changed the title [SPARK-47365][PYTHON] Add toArrowTable() DataFrame method to PySpark [SPARK-47365][PYTHON] Add toArrow() DataFrame method to PySpark May 9, 2024
@HyukjinKwon
Copy link
Member

Merged to master.

JacobZheng0927 pushed a commit to JacobZheng0927/spark that referenced this pull request May 11, 2024
### 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](https://arrow.apache.org/docs/python/generated/pyarrow.Table.html), 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.
```py
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

Closes apache#45481 from ianmcook/SPARK-47365.

Lead-authored-by: Ian Cook <ianmcook@gmail.com>
Co-authored-by: Hyukjin Kwon <gurwls223@gmail.com>
Signed-off-by: Hyukjin Kwon <gurwls223@apache.org>
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>
riyaverm-db pushed a commit to riyaverm-db/spark that referenced this pull request Jun 7, 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 apache#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 apache#46529 from ianmcook/SPARK-48220.

Authored-by: Ian Cook <ianmcook@gmail.com>
Signed-off-by: Hyukjin Kwon <gurwls223@apache.org>
Sign up for free to 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