RonakkGovani opened a new pull request, #60613:
URL: https://github.com/apache/spark/pull/60613

   ### What changes were proposed in this pull request?
   
   Make `np.gcd` and `np.lcm` in pandas API on Spark return the integer dtype 
that NumPy gives the operands, instead of `double`.
   
   Both ufuncs run in a pandas UDF that declared a `DoubleType` result, 
although they take and return integers only. Now the UDF is built when the 
ufunc is called. Its result type is the dtype NumPy resolves for the operands, 
which `_integer_ufunc_dtype` finds by running the ufunc on one-element samples 
of the operand dtypes. The UDF casts the operands to the nullable counterpart 
of that dtype, such as `Int64`, so a null operand still gives a null result.
   
   ### Why are the changes needed?
   
   A `double` cannot hold every `int64` value, so a result above 2**53 fails to 
convert in the Python worker:
   
   ```python
   >>> import numpy as np
   >>> import pyspark.pandas as ps
   >>> np.lcm(ps.Series([2**53 + 1]), ps.Series([2**53 + 1]))
   ...
   pyspark.errors.exceptions.captured.PythonException: An exception was thrown 
from the Python worker: ...
   ```
   
   pandas returns `9007199254740993` with dtype `int64`.
   
   Also, an integer column with a null reaches the UDF as `float64`, which 
`np.gcd` and `np.lcm` do not support, so both failed for any operand with a 
null.
   
   ### Does this PR introduce _any_ user-facing change?
   
   Yes. `np.gcd` and `np.lcm` return the integer dtype pandas returns, for 
example `int64` for two `int64` Series and `int32` for two `int32` Series, 
instead of `float64`, and results above 2**53 are exact:
   
   ```python
   >>> np.lcm(ps.Series([2**53 + 1]), ps.Series([2**53 + 1]))
   0    9007199254740993
   dtype: int64
   ```
   
   A null operand now gives a null result instead of a `PythonException`. 
Operands NumPy does not support for these ufuncs, such as floats, now raise a 
`TypeError` when the ufunc is called, as in pandas, instead of a 
`PythonException` when the result is computed.
   
   ### How was this patch tested?
   
   Added `test_np_gcd_lcm` and `test_np_gcd_lcm_null` to 
`pyspark.pandas.tests.test_numpy_compat`, which the Spark Connect parity suite 
also runs. They compare with pandas for values above 2**53, `int32` and `int8` 
operands, a Python scalar and a boolean operand, check the `TypeError` for 
floats, and check that a null operand gives a null result. Also ran 
`test_numpy_compat` and its parity suite and 
`pyspark.tests.upstream.numpy.test_numpy_ufunc_type_coercion` locally.
   
   ### Was this patch authored or co-authored using generative AI tooling?
   
   Generated-by: Isaac (Claude Opus 5.5)
   
   This pull request and its description were written by Isaac.
   


-- 
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]

Reply via email to