[SPARK-54665][PS] Fix comparison between boolean Series and string literals #54628
Open
singhpraveen2010 wants to merge 2 commits intoapache:masterfrom
Open
[SPARK-54665][PS] Fix comparison between boolean Series and string literals #54628singhpraveen2010 wants to merge 2 commits intoapache:masterfrom
singhpraveen2010 wants to merge 2 commits intoapache:masterfrom
Conversation
This file contains hidden or 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
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.
What changes were proposed in this pull request?
This resolves the open issue:
https://issues.apache.org/jira/browse/SPARK-54665 (https://issues.apache.org/jira/browse/SPARK-54665)
This PR resolves an inconsistency in pandas-on-Spark (
pyspark.pandas) where comparing a booleanSeriesto a string literal returnsTruefor equality (==) andFalsefor inequality (!=).This PR explicitly overrides the
eqandnemethods inBooleanOps(python/pyspark/pandas/data_type_ops/boolean_ops.py) to correctly handle string comparisons by returning a boolean series filled withFalse(foreq) andTrue(forne), aligning the behavior with native pandas semantics.Why are the changes needed?
This change is needed to maintain strict API and behavioral parity with native pandas.
Currently, in
pyspark.pandas4.0.1:ps.Series([True, False]) == 'True'incorrectly yields[True, True].In native
pandas:pd.Series([True, False]) == 'True'correctly yields[False, False].This divergence causes silent logical errors when migrating existing pandas workloads to Spark. By fixing this, we ensure predictable and correct comparison logic for end-users.
Does this PR introduce any user-facing change?
Yes, this fixes a bug in behavioral semantics.
Previous Behavior (pandas-on-Spark):
New Behavior (Matching native pandas):
How was this patch tested?
This patch was tested by adding explicit unit tests verifying equality (
==) and inequality (!=) comparisons between boolean series and string literals.Added tests in
python/pyspark/pandas/tests/data_type_ops/test_boolean_ops.py:BooleanOpsTests.test_eq_ne_with_string(indirectly viatest_eqandtest_neupdates)BooleanExtensionOpsTestsMixinupdates to ensure extension types are also covered.Tests can be run locally via:
./python/run-tests --modules pyspark-pandas-slow --testnames "pyspark.pandas.tests.data_type_ops.test_boolean_ops"Was this patch authored or co-authored using generative AI tooling?
co-authored using: Gemini CLI (Model: Gemini 2.5 Pro)