-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-33073][PYTHON] Improve error handling on Pandas to Arrow conversion failures #29951
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
Changes from all commits
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 |
|---|---|---|
|
|
@@ -153,13 +153,16 @@ def create_array(s, t): | |
| 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: | ||
| error_msg = "Exception thrown when converting pandas.Series (%s) to Arrow " + \ | ||
| "Array (%s). It can be caused by overflows or other unsafe " + \ | ||
| "conversions warned by Arrow. Arrow safe type check can be " + \ | ||
| "disabled by using SQL config " + \ | ||
| "`spark.sql.execution.pandas.convertToArrowArraySafely`." | ||
| raise RuntimeError(error_msg % (s.dtype, t), e) | ||
| except ValueError as e: | ||
| if self._safecheck: | ||
| error_msg = "Exception thrown when converting pandas.Series (%s) to " + \ | ||
| "Arrow Array (%s). It can be caused by overflows or other " + \ | ||
| "unsafe conversions warned by Arrow. Arrow safe type check " + \ | ||
| "can be disabled by using SQL config " + \ | ||
| "`spark.sql.execution.pandas.convertToArrowArraySafely`." | ||
| raise ValueError(error_msg % (s.dtype, t)) from e | ||
|
Member
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. Now that we dropped Python 2, this seems more appropriate
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. In |
||
| else: | ||
| raise e | ||
| return array | ||
|
|
||
| arrs = [] | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -264,11 +264,12 @@ def test_createDataFrame_with_schema(self): | |
| def test_createDataFrame_with_incorrect_schema(self): | ||
| pdf = self.create_pandas_data_frame() | ||
| fields = list(self.schema) | ||
| fields[0], fields[1] = fields[1], fields[0] # swap str with int | ||
| fields[5], fields[6] = fields[6], fields[5] # swap decimal with date | ||
| wrong_schema = StructType(fields) | ||
| with QuietTest(self.sc): | ||
| with self.assertRaisesRegexp(Exception, "integer.*required"): | ||
| self.spark.createDataFrame(pdf, schema=wrong_schema) | ||
| with self.sql_conf({"spark.sql.execution.pandas.convertToArrowArraySafely": False}): | ||
|
Member
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. The error here is a |
||
| with QuietTest(self.sc): | ||
| with self.assertRaisesRegexp(Exception, "[D|d]ecimal.*got.*date"): | ||
| self.spark.createDataFrame(pdf, schema=wrong_schema) | ||
|
|
||
| def test_createDataFrame_with_names(self): | ||
| pdf = self.create_pandas_data_frame() | ||
|
|
||
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.
errors during safe conversion will be
ArrowInvalid, which subclasses ValueError