Skip to content

[SPARK-58548][PS] Use native Spark function for NumPy heaviside - #57749

Closed
zhengruifeng wants to merge 3 commits into
apache:masterfrom
zhengruifeng:pandas-native-heaviside-dev2
Closed

[SPARK-58548][PS] Use native Spark function for NumPy heaviside#57749
zhengruifeng wants to merge 3 commits into
apache:masterfrom
zhengruifeng:pandas-native-heaviside-dev2

Conversation

@zhengruifeng

Copy link
Copy Markdown
Contributor

What changes were proposed in this pull request?

This PR replaces the pandas UDF implementation of np.heaviside in pandas API on Spark with a native Spark when expression. It returns 0.0 for negative values, the second argument at either signed zero, and 1.0 for positive values; null and NaN first arguments propagate.

The new test covers integral inputs, signed zero, NaN, infinities, and NaN as the second argument at zero.

Why are the changes needed?

Using native Spark expressions avoids pandas UDF and Arrow overhead while preserving NumPy heaviside semantics.

Does this PR introduce any user-facing change?

No.

How was this patch tested?

  • Added NumPyCompatTests.test_np_heaviside.
  • ruff check python/pyspark/pandas/numpy_compat.py python/pyspark/pandas/tests/test_numpy_compat.py
  • ruff format --check python/pyspark/pandas/numpy_compat.py python/pyspark/pandas/tests/test_numpy_compat.py
  • python -m unittest pyspark.pandas.tests.test_numpy_compat.NumPyCompatTests.test_np_heaviside

Was this patch authored or co-authored using generative AI tooling?

Generated-by: Codex GPT-5

@uros-b

uros-b commented Aug 4, 2026

Copy link
Copy Markdown
Member

Thank you @zhengruifeng, just please resolve conflicts

@zhengruifeng
zhengruifeng force-pushed the pandas-native-heaviside-dev2 branch 2 times, most recently from 5af3a3d to df13ed2 Compare August 5, 2026 01:22
@zhengruifeng
zhengruifeng force-pushed the pandas-native-heaviside-dev2 branch from df13ed2 to 5fa0b3e Compare August 5, 2026 02:53
zhengruifeng added a commit that referenced this pull request Aug 5, 2026
### What changes were proposed in this pull request?

This PR replaces the pandas UDF implementation of `np.heaviside` in pandas API on Spark with a native Spark `when` expression. It returns `0.0` for negative values, the second argument at either signed zero, and `1.0` for positive values; null and NaN first arguments propagate.

The new test covers integral inputs, signed zero, NaN, infinities, and NaN as the second argument at zero.

### Why are the changes needed?

Using native Spark expressions avoids pandas UDF and Arrow overhead while preserving NumPy `heaviside` semantics.

### Does this PR introduce _any_ user-facing change?

No.

### How was this patch tested?

- Added `NumPyCompatTests.test_np_heaviside`.
- `ruff check python/pyspark/pandas/numpy_compat.py python/pyspark/pandas/tests/test_numpy_compat.py`
- `ruff format --check python/pyspark/pandas/numpy_compat.py python/pyspark/pandas/tests/test_numpy_compat.py`
- `python -m unittest pyspark.pandas.tests.test_numpy_compat.NumPyCompatTests.test_np_heaviside`

### Was this patch authored or co-authored using generative AI tooling?

Generated-by: Codex GPT-5

Closes #57749 from zhengruifeng/pandas-native-heaviside-dev2.

Authored-by: Ruifeng Zheng <ruifengz@apache.org>
Signed-off-by: Ruifeng Zheng <ruifengz@foxmail.com>
(cherry picked from commit 781cc20)
Signed-off-by: Ruifeng Zheng <ruifengz@foxmail.com>
@zhengruifeng

Copy link
Copy Markdown
Contributor Author

Merge Summary:

Posted by merge_spark_pr.py

@zhengruifeng
zhengruifeng deleted the pandas-native-heaviside-dev2 branch August 5, 2026 05:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants