-
Notifications
You must be signed in to change notification settings - Fork 28.9k
[SPARK-54182][SQL][PYTHON] Optimize non-arrow conversion of df.toPandas
#52897
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
Open
Yicong-Huang
wants to merge
5
commits into
apache:master
Choose a base branch
from
Yicong-Huang:SPARK-54182/refactor/avoid-intermedia-df-in-non-arrow-toPandas
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
[SPARK-54182][SQL][PYTHON] Optimize non-arrow conversion of df.toPandas
#52897
Yicong-Huang
wants to merge
5
commits into
apache:master
from
Yicong-Huang:SPARK-54182/refactor/avoid-intermedia-df-in-non-arrow-toPandas
+16
−12
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
df.toPandasdf.toPandas
df.toPandasdf.toPandas
df.toPandasdf.toPandas
df.toPandasdf.toPandas
zhengruifeng
approved these changes
Nov 11, 2025
| timestamp_utc_localized=False, | ||
| )(pser) | ||
| for (_, pser), field in zip(pdf.items(), self.schema.fields) | ||
| )(pd.Series(col_data, dtype=object)) |
Contributor
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why is dtype=object necessary?
| )(pser) | ||
| for (_, pser), field in zip(pdf.items(), self.schema.fields) | ||
| )(pd.Series(col_data, dtype=object)) | ||
| for col_data, field in zip(columns_data, self.schema.fields) |
Contributor
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we avoid creating columns_data: list[list] ?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
What changes were proposed in this pull request?
Following up with #52680, this PR optimizes the non-Arrow path of
toPandas()to eliminate intermediate DataFrame creation.Key optimizations:
Avoid intermediate DataFrame copy
pd.DataFrame.from_records(rows)→ Direct column extraction viazip(*rows)Optimize column-by-column conversion (especially for wide tables)
dtype=objectpd.concat(axis="columns")+ column rename →pd.concat(axis=1, keys=columns)Why are the changes needed?
Problem: Current flow creates DataFrame twice:
rows→pd.DataFrame.from_records()→ temporary DataFrame →pd.concat()→ final DataFrameThe intermediate DataFrame is immediately discarded, wasting memory. This is especially inefficient for wide tables where column-by-column overhead is significant.
Does this PR introduce any user-facing change?
No. This is a pure performance optimization with no API or behavior changes.
How was this patch tested?
Benchmark setup:
spark.sql.execution.arrow.pyspark.enabled=false(testing non-Arrow path)Performance Results
General Benchmark (10 iterations):
Column Width Benchmark (100K rows, 10 iterations):
Was this patch authored or co-authored using generative AI tooling?
Yes. Co-Generated-by Cursor