Skip to content
Closed
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
23 changes: 21 additions & 2 deletions python/pyspark/pandas/numpy_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

import numpy as np

from pyspark.sql import functions as F
from pyspark.sql import Column, functions as F
from pyspark.sql.pandas.functions import pandas_udf
from pyspark.sql.types import DoubleType, BooleanType
from pyspark.pandas.base import IndexOpsMixin
Expand Down Expand Up @@ -99,6 +99,25 @@
),
}


def _fmod_func(c1: Column, c2: Column) -> Column:
c1_double = c1.cast("double")
c2_double = c2.cast("double")

return F.when(
F.typeof(c1).isin("float", "double") | F.typeof(c2).isin("float", "double"),
F.when(c1.isNull() | F.isnan(c1), c1_double)
.when(c2.isNull() | F.isnan(c2), c2_double)
.when(c2_double == 0, F.lit(float("nan")))
.otherwise(F.try_mod(c1_double, c2_double)),
).otherwise(
F.when(c1.isNull() | F.isnan(c1), c1_double)
.when(c2.isNull() | F.isnan(c2), c2_double)
.when(c2_double == 0, F.lit(0.0))
.otherwise(F.try_mod(c1_double, c2_double))
)


binary_np_spark_mappings = {
"arctan2": F.atan2,
"bitwise_and": lambda c1, c2: c1.bitwiseAND(c2),
Expand All @@ -116,7 +135,7 @@
.otherwise(F.greatest(c1, c2))
.cast("double"),
"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]
"fmod": _fmod_func,
"gcd": pandas_udf(lambda s1, s2: np.gcd(s1, s2), DoubleType()), # type: ignore[call-overload]
"heaviside": lambda c1, c2: F.when(
c1.isNull() | F.isnan(c1.cast("double")),
Expand Down
25 changes: 25 additions & 0 deletions python/pyspark/pandas/tests/test_numpy_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,31 @@ def test_np_ldexp(self):

self.assert_eq(np.ldexp(psdf.x, psdf.exp), np.ldexp(pdf.x, pdf.exp), almost=True)

def test_np_fmod(self):
for pdf in (
pd.DataFrame(
{
"x1": [-64, -2, -1, 0, 1, 2, 64],
"x2": [2, 3, -2, -3, -3, 0, 2],
}
),
pd.DataFrame(
{
"x1": [-np.inf, -64.0, -2.0, -0.0, 0.0, 2.0, 64.0, np.inf, np.nan, 1.0],
"x2": [2.0, 3.0, -2.0, -3.0, -3.0, 0.0, -np.inf, np.inf, 2.0, 0.0],
}
),
pd.DataFrame(
{
"x1": pd.array([1, 2, None, None], dtype="Int64"),
"x2": pd.array([2, None, 2, 0], dtype="Int64"),
}
),
):
psdf = ps.from_pandas(pdf)

self.assert_eq(np.fmod(psdf.x1, psdf.x2), np.fmod(pdf.x1, pdf.x2), almost=True)

def test_np_fmax_fmin(self):
for pdf in (
pd.DataFrame({"x1": [-2, -1, 0, 1, 2], "x2": [2, 1, 0, -1, -2]}),
Expand Down