Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions python/pyspark/pandas/numpy_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,13 @@
"fmin": lambda c1, c2: F.least(c1, c2).cast("double"),
"fmod": pandas_udf(lambda s1, s2: np.fmod(s1, s2), DoubleType()), # type: ignore[call-overload]
"gcd": pandas_udf(lambda s1, s2: np.gcd(s1, s2), DoubleType()), # type: ignore[call-overload]
"heaviside": pandas_udf( # type: ignore[call-overload]
lambda s1, s2: np.heaviside(s1, s2), DoubleType()
),
"heaviside": lambda c1, c2: F.when(
c1.isNull() | F.isnan(c1.cast("double")),
c1.cast("double"),
)
.when(c1 < 0, F.lit(0.0))
.when(c1 == 0, c2.cast("double"))
.otherwise(F.lit(1.0)),
"hypot": F.hypot,
"lcm": pandas_udf(lambda s1, s2: np.lcm(s1, s2), DoubleType()), # type: ignore[call-overload]
"ldexp": pandas_udf( # type: ignore[call-overload]
Expand Down
15 changes: 15 additions & 0 deletions python/pyspark/pandas/tests/test_numpy_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,21 @@ def test_np_fmax_fmin(self):
expected = np_func(pdf.x1, pdf.x2)
self.assert_eq(result, expected, almost=True)

def test_np_heaviside(self):
for pdf in (
pd.DataFrame({"x1": [-2, -1, 0, 1, 2], "x2": [-2, -1, 0, 1, 2]}),
pd.DataFrame(
{
"x1": [-np.inf, -2.0, -0.0, 0.0, 0.0, 2.0, np.inf, np.nan],
"x2": [2.0, -2.0, -0.0, 0.5, np.nan, np.nan, -0.0, 2.0],
}
),
):
psdf = ps.from_pandas(pdf)
self.assert_eq(
np.heaviside(psdf.x1, psdf.x2), np.heaviside(pdf.x1, pdf.x2), almost=True
)

def test_np_spark_compat_series(self):
from pyspark.pandas.numpy_compat import unary_np_spark_mappings, binary_np_spark_mappings

Expand Down