-
Notifications
You must be signed in to change notification settings - Fork 121
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add is_nan expression & series method (#1625)
--------- Co-authored-by: Marco Edward Gorelli <[email protected]>
- Loading branch information
1 parent
6b823f3
commit 1b3196b
Showing
12 changed files
with
294 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,6 +32,7 @@ | |
- is_first_distinct | ||
- is_in | ||
- is_last_distinct | ||
- is_nan | ||
- is_null | ||
- is_unique | ||
- len | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -38,6 +38,7 @@ | |
- is_first_distinct | ||
- is_in | ||
- is_last_distinct | ||
- is_nan | ||
- is_null | ||
- is_sorted | ||
- is_unique | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
from __future__ import annotations | ||
|
||
from typing import Any | ||
|
||
import pytest | ||
|
||
import narwhals.stable.v1 as nw | ||
from tests.conftest import dask_lazy_p1_constructor | ||
from tests.conftest import dask_lazy_p2_constructor | ||
from tests.conftest import modin_constructor | ||
from tests.conftest import pandas_constructor | ||
from tests.utils import Constructor | ||
from tests.utils import ConstructorEager | ||
from tests.utils import assert_equal_data | ||
|
||
NON_NULLABLE_CONSTRUCTORS = [ | ||
pandas_constructor, | ||
dask_lazy_p1_constructor, | ||
dask_lazy_p2_constructor, | ||
modin_constructor, | ||
] | ||
|
||
|
||
def test_nan(constructor: Constructor) -> None: | ||
data_na = {"int": [0, 1, None]} | ||
df = nw.from_native(constructor(data_na)).with_columns( | ||
float=nw.col("int").cast(nw.Float64), float_na=nw.col("int") / nw.col("int") | ||
) | ||
result = df.select( | ||
int=nw.col("int").is_nan(), | ||
float=nw.col("float").is_nan(), | ||
float_na=nw.col("float_na").is_nan(), | ||
) | ||
|
||
expected: dict[str, list[Any]] | ||
if any(constructor is c for c in NON_NULLABLE_CONSTRUCTORS): | ||
# Null values are coerced to NaN for non-nullable datatypes | ||
expected = { | ||
"int": [False, False, True], | ||
"float": [False, False, True], | ||
"float_na": [True, False, True], | ||
} | ||
else: | ||
# Null are preserved and should be differentiated for nullable datatypes | ||
expected = { | ||
"int": [False, False, None], | ||
"float": [False, False, None], | ||
"float_na": [True, False, None], | ||
} | ||
|
||
assert_equal_data(result, expected) | ||
|
||
|
||
def test_nan_series(constructor_eager: ConstructorEager) -> None: | ||
data_na = {"int": [0, 1, None]} | ||
df = nw.from_native(constructor_eager(data_na), eager_only=True).with_columns( | ||
float=nw.col("int").cast(nw.Float64), float_na=nw.col("int") / nw.col("int") | ||
) | ||
|
||
result = { | ||
"int": df["int"].is_nan(), | ||
"float": df["float"].is_nan(), | ||
"float_na": df["float_na"].is_nan(), | ||
} | ||
expected: dict[str, list[Any]] | ||
if any(constructor_eager is c for c in NON_NULLABLE_CONSTRUCTORS): | ||
# Null values are coerced to NaN for non-nullable datatypes | ||
expected = { | ||
"int": [False, False, True], | ||
"float": [False, False, True], | ||
"float_na": [True, False, True], | ||
} | ||
else: | ||
# Null are preserved and should be differentiated for nullable datatypes | ||
expected = { | ||
"int": [False, False, None], | ||
"float": [False, False, None], | ||
"float_na": [True, False, None], | ||
} | ||
|
||
assert_equal_data(result, expected) | ||
|
||
|
||
def test_nan_non_float(constructor: Constructor) -> None: | ||
from polars.exceptions import InvalidOperationError as PlInvalidOperationError | ||
from pyarrow.lib import ArrowNotImplementedError | ||
|
||
from narwhals.exceptions import InvalidOperationError as NwInvalidOperationError | ||
|
||
data = {"a": ["x", "y"]} | ||
df = nw.from_native(constructor(data)) | ||
|
||
exc = NwInvalidOperationError | ||
if "polars" in str(constructor): | ||
exc = PlInvalidOperationError | ||
elif "pyarrow_table" in str(constructor): | ||
exc = ArrowNotImplementedError | ||
|
||
with pytest.raises(exc): | ||
df.select(nw.col("a").is_nan()).lazy().collect() | ||
|
||
|
||
def test_nan_non_float_series(constructor_eager: ConstructorEager) -> None: | ||
from polars.exceptions import InvalidOperationError as PlInvalidOperationError | ||
from pyarrow.lib import ArrowNotImplementedError | ||
|
||
from narwhals.exceptions import InvalidOperationError as NwInvalidOperationError | ||
|
||
data = {"a": ["x", "y"]} | ||
df = nw.from_native(constructor_eager(data), eager_only=True) | ||
|
||
exc = NwInvalidOperationError | ||
if "polars" in str(constructor_eager): | ||
exc = PlInvalidOperationError | ||
elif "pyarrow_table" in str(constructor_eager): | ||
exc = ArrowNotImplementedError | ||
|
||
with pytest.raises(exc): | ||
df["a"].is_nan() |