forked from apache/spark
-
Notifications
You must be signed in to change notification settings - Fork 51
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
Infer Pandas string columns in Arrow conversion on Python 2 #679
Merged
Conversation
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
rshkv
force-pushed
the
wr/python2-arrow-strings
branch
from
May 2, 2020 17:08
2b34ede
to
1828aee
Compare
rshkv
changed the title
Infer Pandas string columns in conversion to Arrow under Python 2
Infer Pandas string columns in Arrow conversion on Python 2
May 2, 2020
rshkv
force-pushed
the
wr/python2-arrow-strings
branch
from
May 3, 2020 16:44
3b9148e
to
347a968
Compare
Merged
I never looked into this issue but the code looks reasonable to me 👍 |
rshkv
added a commit
that referenced
this pull request
Feb 26, 2021
When serializing a Pandas dataframe using Arrow under Python 2, Arrow can't tell if string columns should be serialized as string type or as binary (due to how Python 2 stores strings). The result is that Arrow serializes string columns in Pandas dataframes to binary ones. We can remove this when we discontinue support for Python 2. See original PR [1] and follow-up for 'mixed' type columns [2]. [1] #679 [2] #702
rshkv
added a commit
that referenced
this pull request
Mar 2, 2021
When serializing a Pandas dataframe using Arrow under Python 2, Arrow can't tell if string columns should be serialized as string type or as binary (due to how Python 2 stores strings). The result is that Arrow serializes string columns in Pandas dataframes to binary ones. We can remove this when we discontinue support for Python 2. See original PR [1] and follow-up for 'mixed' type columns [2]. [1] #679 [2] #702
jdcasale
pushed a commit
that referenced
this pull request
Mar 3, 2021
When serializing a Pandas dataframe using Arrow under Python 2, Arrow can't tell if string columns should be serialized as string type or as binary (due to how Python 2 stores strings). The result is that Arrow serializes string columns in Pandas dataframes to binary ones. We can remove this when we discontinue support for Python 2. See original PR [1] and follow-up for 'mixed' type columns [2]. [1] #679 [2] #702
rshkv
added a commit
that referenced
this pull request
Mar 4, 2021
When serializing a Pandas dataframe using Arrow under Python 2, Arrow can't tell if string columns should be serialized as string type or as binary (due to how Python 2 stores strings). The result is that Arrow serializes string columns in Pandas dataframes to binary ones. We can remove this when we discontinue support for Python 2. See original PR [1] and follow-up for 'mixed' type columns [2]. [1] #679 [2] #702
rshkv
added a commit
that referenced
this pull request
Mar 9, 2021
When serializing a Pandas dataframe using Arrow under Python 2, Arrow can't tell if string columns should be serialized as string type or as binary (due to how Python 2 stores strings). The result is that Arrow serializes string columns in Pandas dataframes to binary ones. We can remove this when we discontinue support for Python 2. See original PR [1] and follow-up for 'mixed' type columns [2]. [1] #679 [2] #702
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.
Problem
Python 2 strings are stored as binary (as opposed to unicode in Python 3). That means when serializing a Pandas dataframe, Arrow can't tell if columns are string or actually binary, so it assumes binary.
Why We Care
This is only an issue when using
createDataFrame(pandas_df)
with thespark.sql.execution.arrow.enabled
flag. We have two internal products that serialize Pandas data frames. And the one with the arrows enables this flag by default.If used with pyarrow <0.10, Arrow serialization fails anyway courtesy of this check (GHE). This is why unexpected binary columns haven't surprised us until recently, when we upgraded to >0.10.
Note that this is irrelevant for
@pandas_udf
which requires you to provide a schema. If the schema saysstring
, Arrow serialization will do the right thing and not give you binary.Proposed Changes
We give pyarrow a chance to infer the right column type, and then we nudge it a little depending on what it thinks the type is. There are three different cases:
binary
, we use Pandas' type inference to check if it isn't actuallystring
instead. And if is, we tell Arrow to serialize asstring
type. We only do this for Python 2.array<binary>
, we throw an error falling back to non-Arrow serialization which doesn't confuse binary and string. Again, only Python 2.struct
containingbinary
, we don't do anything because Arrow doesn't support it anyway. This will change when we bump to 0.15.1. I added a test to remind us.Why Not Take This From Upstream
Upstream upgraded their arrow / pyarrow for the Spark 3 release which won't support Python 2. They don't have to deal with it. But we want a higher pyarrow version before we can move off Python 2.
How This Patch Was Tested
I added three unit tests for
createDataFrame
withstring
,array<string>
, andstruct<string, string>
as input. Each verifies that the schema is the same regardless of whether Arrow serialization was used. I also verified manually thatpandas_udf
still works forarray<string>
andarray<binary>
outputs for pyarrow 0.8.0 and 0.12.1.