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 9 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
13 changes: 12 additions & 1 deletion python/cudf/cudf/core/column/column.py
Original file line number Diff line number Diff line change
Expand Up @@ -1948,8 +1948,19 @@ def as_column(
raise TypeError(
f"Cannot convert a {inferred_dtype} of object type"
)
elif (
cudf.get_option("mode.pandas_compatible")
galipremsagar marked this conversation as resolved.
Show resolved Hide resolved
and inferred_dtype == "boolean"
):
raise MixedTypeError(
f"Cannot have mixed values with {inferred_dtype}"
)
elif nan_as_null is False and (
pd.isna(arbitrary).any()
any(
(isinstance(x, (np.floating, float)) and np.isnan(x))
or (inferred_dtype == "boolean" and pd.isna(arbitrary))
Copy link
Contributor

Choose a reason for hiding this comment

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

Would be good to have (inferred_dtype == "boolean" and pd.isna(arbitrary)) be evaluated outside the loop.

Also what case is this condition trying to catch?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

Also what case is this condition trying to catch?

It is trying to catch this case:

pd.Series(["a", "b", np.nan], dtype='object')

for x in np.asarray(arbitrary)
)
and inferred_dtype not in ("decimal", "empty")
):
# Decimal can hold float("nan")
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
15 changes: 9 additions & 6 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 Expand Up @@ -2358,10 +2360,11 @@ def test_bool_series_mixed_dtype_error():
ps = pd.Series([True, False, None])
# ps now has `object` dtype, which
# isn't supported by `cudf`.
with pytest.raises(TypeError):
cudf.Series(ps, nan_as_null=False)
with pytest.raises(TypeError):
cudf.from_pandas(ps, nan_as_null=False)
with cudf.option_context("mode.pandas_compatible", True):
with pytest.raises(TypeError):
cudf.Series(ps)
with pytest.raises(TypeError):
cudf.from_pandas(ps)


@pytest.mark.parametrize(
Expand Down
Loading