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
Improved conversion between pyarrow
and pandas
in P2P shuffling
#7896
Merged
Merged
Changes from 5 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
87fef7c
Add types mapper
hendrikmakait 3f46581
large_string
hendrikmakait 21f129d
Minor
hendrikmakait 684d988
[skip-caching]
hendrikmakait 38898f7
Copy only if necessary
hendrikmakait c27be14
Merge branch 'main' into dont-convert-strings
hendrikmakait f793bcc
[skip-caching]
hendrikmakait 8e93913
[skip-caching]
hendrikmakait File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -46,6 +46,7 @@ | |
|
||
|
||
def convert_partition(data: bytes, meta: pd.DataFrame) -> pd.DataFrame: | ||
import pandas as pd | ||
import pyarrow as pa | ||
|
||
file = BytesIO(data) | ||
|
@@ -56,7 +57,19 @@ | |
shards.append(sr.read_all()) | ||
table = pa.concat_tables(shards) | ||
df = table.to_pandas(self_destruct=True) | ||
return df.astype(meta.dtypes) | ||
|
||
def default_types_mapper(pyarrow_dtype: pa.DataType) -> object: | ||
# Avoid converting strings from `string[pyarrow]` to `string[python]` | ||
# if we have *some* `string[pyarrow]` | ||
if ( | ||
pyarrow_dtype in {pa.large_string(), pa.string()} | ||
and pd.StringDtype("pyarrow") in meta.dtypes.values | ||
): | ||
return pd.StringDtype("pyarrow") | ||
return None | ||
|
||
df = table.to_pandas(self_destruct=True, types_mapper=default_types_mapper) | ||
return df.astype(meta.dtypes, copy=False) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
|
||
def list_of_buffers_to_table(data: list[bytes]) -> pa.Table: | ||
|
Oops, something went wrong.
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.
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.
There are actually two implementations of
pyarrow
-strings inpandas
. The one you have here and alsopd.ArrowDtype(pa.string())
. What you have here is fine for now, especially since it's just a performance optimization. Over indask/dask
we're also usingpd.StringDtype("pyarrow")
as it, historically, has been more feature complete thanpd.ArrowDtype(pa.string())
. That said, I think the situation has changed inpandas=2
, so we may switch topd.ArrowDtype(pa.string())
at some point in the future. This is mostly just an FYI in case we need to circle back to here in the future.