-
Notifications
You must be signed in to change notification settings - Fork 29.3k
[SPARK-25351][SQL][Python] Handle Pandas category type when converting from Python with Arrow #26585
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-25351][SQL][Python] Handle Pandas category type when converting from Python with Arrow #26585
Changes from 12 commits
6b409e6
5fb9de5
9733be2
d74389c
aa8c5f6
a8d3173
333f37c
1a2447b
24971d4
bcb877a
99d5026
e3246ad
9f04f1b
7aa9fcd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -142,6 +142,7 @@ def _create_batch(self, series): | |
| """ | ||
| import pandas as pd | ||
| import pyarrow as pa | ||
|
|
||
| from pyspark.sql.pandas.types import _check_series_convert_timestamps_internal | ||
| # Make input conform to [(series1, type1), (series2, type2), ...] | ||
| if not isinstance(series, (list, tuple)) or \ | ||
|
|
@@ -154,6 +155,9 @@ def create_array(s, t): | |
| # Ensure timestamp series are in expected form for Spark internal representation | ||
| if t is not None and pa.types.is_timestamp(t): | ||
| s = _check_series_convert_timestamps_internal(s, self._timezone) | ||
| elif type(s.dtype) == pd.CategoricalDtype: | ||
| # FIXME: This can be removed once minimum pyarrow version is >= 0.16.1 | ||
|
Member
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. please change
Author
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. Done |
||
| s = s.astype(s.dtypes.categories.dtype) | ||
| try: | ||
| array = pa.Array.from_pandas(s, mask=mask, type=t, safe=self._safecheck) | ||
| except pa.ArrowException as e: | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -415,6 +415,20 @@ def run_test(num_records, num_parts, max_records, use_delay=False): | |
| for case in cases: | ||
| run_test(*case) | ||
|
|
||
| def test_createDateFrame_with_category_type(self): | ||
| pdf = pd.DataFrame({"A": [u"a", u"b", u"c", u"a"]}) | ||
| pdf["B"] = pdf["A"].astype('category') | ||
|
|
||
| with self.sql_conf({"spark.sql.execution.arrow.pyspark.enabled": True}): | ||
| arrow_df = self.spark.createDataFrame(pdf) | ||
| result_arrow = arrow_df.toPandas() | ||
|
|
||
| with self.sql_conf({"spark.sql.execution.arrow.pyspark.enabled": False}): | ||
| df = self.spark.createDataFrame(pdf) | ||
| result_spark = df.toPandas() | ||
|
|
||
| assert_frame_equal(result_spark, result_arrow) | ||
|
|
||
|
Member
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. could you add an assert that the Spark DataFrame has column "B" as a string type?
Author
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. Done, move other test checks here too. |
||
|
|
||
| @unittest.skipIf( | ||
| not have_pandas or not have_pyarrow, | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -897,6 +897,30 @@ def test_timestamp_dst(self): | |
| result = df.withColumn('time', foo_udf(df.time)) | ||
| self.assertEquals(df.collect(), result.collect()) | ||
|
|
||
| def test_createDateFrame_with_category_type(self): | ||
|
Member
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. This test module is for @pandas_udf('string')
def f(x):
return x.astype('category')
pdf = pd.DataFrame({"A": [u"a", u"b", u"c", u"a"]})
df = spark.createDataFrame(pdf).withColumn("B", f(col("A")))
result = df.toPandas()
# Check result "B" is equal to "A"
Author
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. aha, got it. fixed |
||
| pdf = pd.DataFrame({"A": [u"a", u"b", u"c", u"a"]}) | ||
| pdf["B"] = pdf["A"].astype('category') | ||
| category_first_element = dict(enumerate(pdf['B'].cat.categories))[0] | ||
|
|
||
| with self.sql_conf({"spark.sql.execution.arrow.pyspark.enabled": True}): | ||
| arrow_df = self.spark.createDataFrame(pdf) | ||
| arrow_type = arrow_df.dtypes[1][1] | ||
| result_arrow = arrow_df.collect() | ||
| arrow_first_category_element = result_arrow[0][1] | ||
|
|
||
| with self.sql_conf({"spark.sql.execution.arrow.pyspark.enabled": False}): | ||
| df = self.spark.createDataFrame(pdf) | ||
| spark_type = df.dtypes[1][1] | ||
| result_spark = df.collect() | ||
| spark_first_category_element = result_spark[0][1] | ||
|
|
||
| # ensure original category elements are string | ||
| assert isinstance(category_first_element, str) | ||
| # spark dataframe and arrow execution mode enabled dataframe type must match padnads | ||
| assert spark_type == arrow_type == 'string' | ||
| assert isinstance(arrow_first_category_element, str) | ||
| assert isinstance(spark_first_category_element, str) | ||
|
Member
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. Oh yeah, move these to the other test please. |
||
|
|
||
| @unittest.skipIf(sys.version_info[:2] < (3, 5), "Type hints are supported from Python 3.5.") | ||
| def test_type_annotation(self): | ||
| # Regression test to check if type hints can be used. See SPARK-23569. | ||
|
|
||
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.
nit: remove newline
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.
Done