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
6 changes: 6 additions & 0 deletions python/datafusion/expr.py
Original file line number Diff line number Diff line change
Expand Up @@ -1226,6 +1226,12 @@ def isnan(self) -> Expr:

return F.isnan(self)

def is_nan(self) -> Expr:
"""Returns true if a given number is +NaN or -NaN otherwise returns false."""
from . import functions as F

return F.is_nan(self)

def degrees(self) -> Expr:
"""Converts the argument from radians to degrees."""
from . import functions as F
Expand Down
6 changes: 6 additions & 0 deletions python/datafusion/functions/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ def _warn_if_expr_for_literal_arg(
"initcap",
"inner_product",
"instr",
"is_nan",
"isnan",
"iszero",
"lag",
Expand Down Expand Up @@ -413,6 +414,11 @@ def isnan(expr: Expr) -> Expr:
return Expr(f.isnan(expr.expr))


def is_nan(expr: Expr) -> Expr:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice addition! Since is_nan is a direct alias of isnan, it might be helpful to make that relationship a little more obvious in the docstring. A short See Also reference to isnan, or reusing the existing isnan example, would make the generated help a bit easier to navigate.

"""Returns true if a given number is +NaN or -NaN otherwise returns false."""
return isnan(expr)


def nullif(expr1: Expr, expr2: Expr) -> Expr:
"""Returns NULL if expr1 equals expr2; otherwise it returns expr1.

Expand Down
5 changes: 5 additions & 0 deletions python/tests/test_expr.py
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,11 @@ def test_alias_with_metadata(df):
pa.array([False, True, False, None], type=pa.bool_()),
id="isnan",
),
pytest.param(
col("e").is_nan(),
pa.array([False, True, False, None], type=pa.bool_()),
id="is_nan",
),
pytest.param(
functions.round(col("a").degrees(), lit(4)),
pa.array([-42.9718, 28.6479, 0.0, None], type=pa.float64()),
Expand Down
9 changes: 9 additions & 0 deletions python/tests/test_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,15 @@ def test_math_functions():
)


def test_is_nan_alias():
ctx = SessionContext()
df = ctx.from_pydict({"value": [1.0, np.nan, None]})

result = df.select(f.is_nan(column("value")).alias("is_nan")).to_pydict()

assert result == {"is_nan": [False, True, None]}


def py_indexof(arr, v):
try:
return arr.index(v) + 1
Expand Down