You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@spark.apache.org by "arminnh (via GitHub)" <gi...@apache.org> on 2024/06/02 14:23:07 UTC
[PR] [WIP][PYTHON] Fix `assertDataFrameEqual` behavior with mixed DataFrame types [spark]
arminnh opened a new pull request, #46836:
URL: https://github.com/apache/spark/pull/46836
### What changes were proposed in this pull request?
* Avoid `AttributeError` (see examples below) when mixing Spark DataFrame & Pandas or Pandas-on-Spark DataFrame in `assertDataFrameEqual` by not using non-existent functions `assertAlmostEqual` & `assertEqual` in `PandasOnSparkTestUtils.assert_eq`
* In `PandasOnSparkTestUtils.assert_eq`, applied the Pandas-on-Spark flow for both params `left` and `right`, instead of only `left`, and clarified the error to specify that a Pandas or Pandas-on-Spark object is expected, since which is not immediately obvious from the current error: `DataFrame, DataFrame, Series, Series, IndexIndex`
### Why are the changes needed?
* `assertDataFrameEqual` results in `AttributeError` when providing a Spark DataFrame as the first argument and a Pandas DataFrame or a Pandas-on-Spark DataFrame as the second argument.
### Does this PR introduce _any_ user-facing change?
* Better errors will be raised in the situation described above:
* `PySparkAssertionError` with a message is raised instead of `AttributeError`.
* `PySparkAssertionError` error when mixing Spark & Pandas-on-Spark DataFrames is consistently raised in `PandasOnSparkTestUtils.assert_eq`, regardless of which one is left or right.
* Clarified error message `Expected type DataFrame, DataFrame, Series, Series, IndexIndex, for ...` -> `Expected type Pandas or Pandas-on-Spark DataFrame, Series, or Index for ...`
#### Setup:
```
import pandas as pd
import pyspark.pandas as ps
from pyspark.testing import assertDataFrameEqual
df1 = spark.createDataFrame([(10,), (20,), (30,)], ["Numbers"])
df2 = pd.DataFrame(data=[10, 11, 13], columns=["Numbers"])
df3 = ps.DataFrame(data=[10, 11, 13], columns=["Numbers"])
```
#### Before:
```
>>> assertDataFrameEqual(df1, df2, ignoreColumnType=True)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "...spark/python/pyspark/testing/utils.py", line 828, in assertDataFrameEqual
return PandasOnSparkTestUtils().assert_eq(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "...spark/python/pyspark/testing/pandasutils.py", line 483, in assert_eq
self.assertAlmostEqual(lobj, robj)
^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'PandasOnSparkTestUtils' object has no attribute 'assertAlmostEqual'. Did you mean: 'assertPandasEqual'?
>>> assertDataFrameEqual(df2, df1, ignoreColumnType=True)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "...spark/python/pyspark/testing/utils.py", line 828, in assertDataFrameEqual
return PandasOnSparkTestUtils().assert_eq(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "...spark/python/pyspark/testing/pandasutils.py", line 472, in assert_eq
_assert_pandas_almost_equal(lobj, robj, rtol=rtol, atol=atol)
File "...spark/python/pyspark/testing/pandasutils.py", line 314, in _assert_pandas_almost_equal
raise PySparkAssertionError(
pyspark.errors.exceptions.base.PySparkAssertionError: [INVALID_TYPE_DF_EQUALITY_ARG] Expected type DataFrame, Series, Index, for `right` but got type <class 'pyspark.sql.classic.dataframe.DataFrame'>.
>>>
>>> assertDataFrameEqual(df1, df3, ignoreColumnType=True)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "...spark/python/pyspark/testing/utils.py", line 828, in assertDataFrameEqual
return PandasOnSparkTestUtils().assert_eq(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "...spark/python/pyspark/testing/pandasutils.py", line 483, in assert_eq
self.assertAlmostEqual(lobj, robj)
^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'PandasOnSparkTestUtils' object has no attribute 'assertAlmostEqual'. Did you mean: 'assertPandasEqual'?
>>> assertDataFrameEqual(df3, df1, ignoreColumnType=True)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "...spark/python/pyspark/testing/utils.py", line 828, in assertDataFrameEqual
return PandasOnSparkTestUtils().assert_eq(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "...spark/python/pyspark/testing/pandasutils.py", line 438, in assert_eq
raise PySparkAssertionError(
pyspark.errors.exceptions.base.PySparkAssertionError: [INVALID_TYPE_DF_EQUALITY_ARG] Expected type DataFrame, DataFrame, Series, Series, IndexIndex, for `expected` but got type <class 'pyspark.sql.classic.dataframe.DataFrame'>.
```
#### After
```
>>> assertDataFrameEqual(df1, df2)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "...spark/python/pyspark/testing/utils.py", line 828, in assertDataFrameEqual
return PandasOnSparkTestUtils().assert_eq(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "...spark/python/pyspark/testing/pandasutils.py", line 476, in assert_eq
_assert_pandas_almost_equal(lobj, robj, rtol=rtol, atol=atol)
File "...spark/python/pyspark/testing/pandasutils.py", line 300, in _assert_pandas_almost_equal
raise PySparkAssertionError(
**pyspark.errors.exceptions.base.PySparkAssertionError: [INVALID_TYPE_DF_EQUALITY_ARG] Expected type DataFrame, Series, Index, for `left` but got type <class 'pyspark.sql.classic.dataframe.DataFrame'>.**
>>> assertDataFrameEqual(df2, df1)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "...spark/python/pyspark/testing/utils.py", line 828, in assertDataFrameEqual
return PandasOnSparkTestUtils().assert_eq(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "...spark/python/pyspark/testing/pandasutils.py", line 465, in assert_eq
_assert_pandas_almost_equal(lobj, robj, rtol=rtol, atol=atol)
File "...spark/python/pyspark/testing/pandasutils.py", line 311, in _assert_pandas_almost_equal
raise PySparkAssertionError(
pyspark.errors.exceptions.base.PySparkAssertionError: [INVALID_TYPE_DF_EQUALITY_ARG] Expected type DataFrame, Series, Index, for `right` but got type <class 'pyspark.sql.classic.dataframe.DataFrame'>.
>>> assertDataFrameEqual(df1, df3)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "...spark/python/pyspark/testing/utils.py", line 828, in assertDataFrameEqual
return PandasOnSparkTestUtils().assert_eq(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "...spark/python/pyspark/testing/pandasutils.py", line 426, in assert_eq
raise PySparkAssertionError(
pyspark.errors.exceptions.base.PySparkAssertionError: [INVALID_TYPE_DF_EQUALITY_ARG] Expected type Pandas or Pandas-on-Spark DataFrame, Series, or Index for `left` but got type <class 'pyspark.sql.classic.dataframe.DataFrame'>.
>>> assertDataFrameEqual(df3, df1)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "...spark/python/pyspark/testing/utils.py", line 828, in assertDataFrameEqual
return PandasOnSparkTestUtils().assert_eq(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "...spark/python/pyspark/testing/pandasutils.py", line 438, in assert_eq
raise PySparkAssertionError(
pyspark.errors.exceptions.base.PySparkAssertionError: [INVALID_TYPE_DF_EQUALITY_ARG] Expected type Pandas or Pandas-on-Spark DataFrame, Series, or Index for `right` but got type <class 'pyspark.sql.classic.dataframe.DataFrame'>.
```
### How was this patch tested?
* Manually tested new behavior in local SparkSession.
* Extended existing test case with Pandas-on-Spark DataFrame to confirm the correct error is raised when the parameters are flipped.
* Added test case with Spark DataFrame & Pandas DataFrame.
### Was this patch authored or co-authored using generative AI tooling?
No
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [PYTHON] Fix `assertDataFrameEqual` behavior with mixed DataFrame types [spark]
Posted by "github-actions[bot] (via GitHub)" <gi...@apache.org>.
github-actions[bot] commented on PR #46836:
URL: https://github.com/apache/spark/pull/46836#issuecomment-2347522856
We're closing this PR because it hasn't been updated in a while. This isn't a judgement on the merit of the PR in any way. It's just a way of keeping the PR queue manageable.
If you'd like to revive this PR, please reopen it and ask a committer to remove the Stale tag!
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [WIP][PYTHON] Fix `assertDataFrameEqual` behavior with mixed DataFrame types [spark]
Posted by "HyukjinKwon (via GitHub)" <gi...@apache.org>.
HyukjinKwon commented on PR #46836:
URL: https://github.com/apache/spark/pull/46836#issuecomment-2144132021
cc @itholic
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [PYTHON] Fix `assertDataFrameEqual` behavior with mixed DataFrame types [spark]
Posted by "HyukjinKwon (via GitHub)" <gi...@apache.org>.
HyukjinKwon commented on PR #46836:
URL: https://github.com/apache/spark/pull/46836#issuecomment-2144536574
you cab create an account and create a JIRA ticket.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [PYTHON] Fix `assertDataFrameEqual` behavior with mixed DataFrame types [spark]
Posted by "github-actions[bot] (via GitHub)" <gi...@apache.org>.
github-actions[bot] closed pull request #46836: [PYTHON] Fix `assertDataFrameEqual` behavior with mixed DataFrame types
URL: https://github.com/apache/spark/pull/46836
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [PYTHON] Fix `assertDataFrameEqual` behavior with mixed DataFrame types [spark]
Posted by "itholic (via GitHub)" <gi...@apache.org>.
itholic commented on code in PR #46836:
URL: https://github.com/apache/spark/pull/46836#discussion_r1625228304
##########
python/pyspark/sql/tests/test_utils.py:
##########
@@ -955,13 +954,8 @@ def test_assert_error_pandas_pyspark_df(self):
exception=pe.exception,
error_class="INVALID_TYPE_DF_EQUALITY_ARG",
message_parameters={
- "expected_type": f"{ps.DataFrame.__name__}, "
- f"{pd.DataFrame.__name__}, "
- f"{ps.Series.__name__}, "
- f"{pd.Series.__name__}, "
- f"{ps.Index.__name__}"
- f"{pd.Index.__name__}, ",
- "arg_name": "expected",
+ "expected_type": "Pandas or Pandas-on-Spark DataFrame, Series, or Index",
Review Comment:
+1 This one looks more clearer to me.
##########
python/pyspark/pandas/tests/indexes/test_indexing_adv.py:
##########
@@ -69,7 +69,7 @@ def test_at(self):
self.assertEqual(psdf.at[3, "b"], 6)
self.assertEqual(psdf.at[3, "b"], pdf.at[3, "b"])
self.assert_eq(psdf.at[9, "b"], np.array([0, 0, 0]))
- self.assert_eq(psdf.at[9, "b"], pdf.at[9, "b"])
+ self.assert_eq(psdf.at[9, "b"], pdf.at[9, "b"].to_numpy())
Review Comment:
I believe the previous one looks more reasonable.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [PYTHON] Fix `assertDataFrameEqual` behavior with mixed DataFrame types [spark]
Posted by "arminnh (via GitHub)" <gi...@apache.org>.
arminnh commented on PR #46836:
URL: https://github.com/apache/spark/pull/46836#issuecomment-2144452439
@itholic @HyukjinKwon I didn't find an issue about this on Jira but I hope it's fine to propose a solution. It's a minor bug I encountered while setting up automated testing on Databricks at work.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org