CODERNSINGH opened a new pull request, #60298:
URL: https://github.com/apache/spark/pull/60298
### What changes were proposed in this pull request?
Make `<`, `<=`, `>` and `>=` (and `lt`/`le`/`gt`/`ge`) work between boolean
and numeric pandas-on-Spark Series/Index, in both directions:
- `BooleanOps.lt/le/gt/ge`: if the other operand is a non-boolean number or
a numeric Series/Index, cast the boolean operand to its numeric type first.
- `NumericOps.lt/le/gt/ge`: cast a boolean operand on the right to the
numeric type of the left operand.
Both use the existing `transform_boolean_operand_to_numeric`, as `==` and
the arithmetic operators already do.
### Why are the changes needed?
In pandas, `True`/`False` compare as `1`/`0`, but in pandas-on-Spark these
comparisons fail, with or without ANSI mode:
```python
import pandas as pd
import pyspark.pandas as ps
psdf = ps.from_pandas(pd.DataFrame({"c0": [1.0]}))
(psdf["c0"] != 0).gt(psdf["c0"])
# AnalysisException: [DATATYPE_MISMATCH.BINARY_OP_DIFF_TYPES] Cannot resolve
# "(... > c0)" due to data type mismatch: ... ("BOOLEAN" and "DOUBLE")
```
The same happens for `bool_ser < int_ser`, `bool_ser > 0.5`, `float_ser >
bool_ser`, etc.
### Does this PR introduce _any_ user-facing change?
Yes. Ordering comparisons between boolean and numeric pandas-on-Spark
objects now return the same result as pandas instead of raising
`AnalysisException`.
### How was this patch tested?
Added `test_compare_with_numeric` to `BooleanOpsTestsMixin`, which compares
the results with pandas for `<`, `<=`, `>`, `>=` between a boolean Series and
int/float Series (including NaN) in both directions, int/float scalars, a
numeric Series with a boolean scalar, and a nullable `boolean` Series. It fails
without this change and passes with it, in both classic and Spark Connect
(`test_parity_boolean_ops`).
Also ran `test_boolean_ops`, `test_num_ops`,
`test_num_ops_fractional_ext_astype_cmp`,
`test_num_ops_integral_ext_astype_cmp` and their Spark Connect parity tests,
and `./dev/lint-python --compile --ruff`.
### Was this patch authored or co-authored using generative AI tooling?
Generated-by: Claude Code (Claude Opus 5.5)
--
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: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]