Skip to content
Draft
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
11 changes: 10 additions & 1 deletion python/pyspark/pandas/numpy_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from pyspark.sql.types import DoubleType, LongType, BooleanType
from pyspark.pandas.base import IndexOpsMixin


unary_np_spark_mappings = {
"abs": F.abs,
"absolute": F.abs,
Expand Down Expand Up @@ -78,7 +79,15 @@
"square": lambda c: c.cast("double") * c,
"tan": F.tan,
"tanh": F.tanh,
"trunc": pandas_udf(lambda s: np.trunc(s), DoubleType()), # type: ignore[call-overload]
"trunc": lambda c: F.when(
c.cast("double").isNull()
| F.isnan(c.cast("double"))
| c.cast("double").isin(float("-inf"), float("inf")),
c.cast("double"),
).otherwise(
F.signum(c.cast("double"))
* (F.abs(c.cast("double")) - (F.abs(c.cast("double")) % F.lit(1.0)))
),
}

binary_np_spark_mappings = {
Expand Down
18 changes: 18 additions & 0 deletions python/pyspark/pandas/tests/test_numpy_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,24 @@ def test_np_math_functions(self):
(np.sinh, [-np.inf, -64.0, -2.0, 0.0, 2.0, 64.0, np.inf, np.nan]),
(np.square, [-np.inf, -64.0, -2.0, 0.0, 2.0, 64.0, np.inf, np.nan]),
(np.tanh, [-np.inf, -64.0, -2.0, 0.0, 2.0, 64.0, np.inf, np.nan]),
(
np.trunc,
[
-np.inf,
-64.0,
-2.0,
-1.5,
-0.5,
-0.0,
0.0,
0.5,
1.5,
2.0,
64.0,
np.inf,
np.nan,
],
),
):
with self.subTest(name=np_func.__name__, values=values):
pdf = pd.DataFrame({"a": values})
Expand Down