Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow None when nan_as_null=False in column constructor #15709

Merged
merged 17 commits into from
May 15, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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: 9 additions & 1 deletion python/cudf/cudf/core/column/column.py
Original file line number Diff line number Diff line change
Expand Up @@ -1948,9 +1948,17 @@ def as_column(
raise TypeError(
f"Cannot convert a {inferred_dtype} of object type"
)
elif (
nan_as_null is False
Copy link
Contributor

Choose a reason for hiding this comment

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

Don't compare to booleans with is.

Suggested change
nan_as_null is False
not nan_as_null

Copy link
Contributor Author

Choose a reason for hiding this comment

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

and inferred_dtype in {"string"}
galipremsagar marked this conversation as resolved.
Show resolved Hide resolved
and pd.Series(arbitrary)
.apply(lambda x: isinstance(x, float) and np.isnan(x))
.any()
galipremsagar marked this conversation as resolved.
Show resolved Hide resolved
):
raise MixedTypeError(f"Cannot have NaN with {inferred_dtype}")
galipremsagar marked this conversation as resolved.
Show resolved Hide resolved
elif nan_as_null is False and (
pd.isna(arbitrary).any()
and inferred_dtype not in ("decimal", "empty")
and inferred_dtype not in ("decimal", "empty", "string")
):
# Decimal can hold float("nan")
Copy link
Contributor Author

@galipremsagar galipremsagar May 9, 2024

Choose a reason for hiding this comment

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

@mroeschke After the changes in this PR, do you think this block might be redundant or does it still capture some error scenarios?

# All np.nan is not restricted by type
Expand Down
38 changes: 11 additions & 27 deletions python/cudf/cudf/tests/test_dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -4008,44 +4008,28 @@ def test_diff(dtype, period, data_empty):

@pytest.mark.parametrize("df", _dataframe_na_data())
@pytest.mark.parametrize("nan_as_null", [True, False, None])
def test_dataframe_isnull_isna(df, nan_as_null):
if nan_as_null is False and (
df.select_dtypes(object).isna().any().any()
and not df.select_dtypes(object).isna().all().all()
):
with pytest.raises(MixedTypeError):
cudf.DataFrame.from_pandas(df, nan_as_null=nan_as_null)
else:
gdf = cudf.DataFrame.from_pandas(df, nan_as_null=nan_as_null)
@pytest.mark.parametrize("api_call", ["isnull", "isna", "notna", "notnull"])
def test_dataframe_isnull_isna_and_reverse(df, nan_as_null, api_call):
def detect_nan(x):
# Check if the input is a float and if it is nan
return x.apply(lambda v: isinstance(v, float) and np.isnan(v))

assert_eq(df.isnull(), gdf.isnull())
assert_eq(df.isna(), gdf.isna())

# Test individual columns
for col in df:
assert_eq(df[col].isnull(), gdf[col].isnull())
assert_eq(df[col].isna(), gdf[col].isna())


@pytest.mark.parametrize("df", _dataframe_na_data())
@pytest.mark.parametrize("nan_as_null", [True, False, None])
def test_dataframe_notna_notnull(df, nan_as_null):
nan_contains = df.select_dtypes(object).apply(detect_nan)
if nan_as_null is False and (
df.select_dtypes(object).isna().any().any()
and not df.select_dtypes(object).isna().all().all()
nan_contains.any().any() and not nan_contains.all().all()
):
with pytest.raises(MixedTypeError):
cudf.DataFrame.from_pandas(df, nan_as_null=nan_as_null)
else:
gdf = cudf.DataFrame.from_pandas(df, nan_as_null=nan_as_null)

assert_eq(df.notnull(), gdf.notnull())
assert_eq(df.notna(), gdf.notna())
assert_eq(getattr(df, api_call)(), getattr(gdf, api_call)())

# Test individual columns
for col in df:
assert_eq(df[col].notnull(), gdf[col].notnull())
assert_eq(df[col].notna(), gdf[col].notna())
assert_eq(
getattr(df[col], api_call)(), getattr(gdf[col], api_call)()
)


def test_ndim():
Expand Down
6 changes: 4 additions & 2 deletions python/cudf/cudf/tests/test_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -774,8 +774,9 @@ def test_round_nan_as_null_false(series, decimal):
@pytest.mark.parametrize("ps", _series_na_data())
@pytest.mark.parametrize("nan_as_null", [True, False, None])
def test_series_isnull_isna(ps, nan_as_null):
nan_contains = ps.apply(lambda x: isinstance(x, float) and np.isnan(x))
if nan_as_null is False and (
ps.isna().any() and not ps.isna().all() and ps.dtype == object
nan_contains.any() and not nan_contains.all() and ps.dtype == object
):
with pytest.raises(MixedTypeError):
cudf.Series.from_pandas(ps, nan_as_null=nan_as_null)
Expand All @@ -789,8 +790,9 @@ def test_series_isnull_isna(ps, nan_as_null):
@pytest.mark.parametrize("ps", _series_na_data())
@pytest.mark.parametrize("nan_as_null", [True, False, None])
def test_series_notnull_notna(ps, nan_as_null):
nan_contains = ps.apply(lambda x: isinstance(x, float) and np.isnan(x))
if nan_as_null is False and (
ps.isna().any() and not ps.isna().all() and ps.dtype == object
nan_contains.any() and not nan_contains.all() and ps.dtype == object
):
with pytest.raises(MixedTypeError):
cudf.Series.from_pandas(ps, nan_as_null=nan_as_null)
Expand Down
Loading