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 14 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
26 changes: 24 additions & 2 deletions python/cudf/cudf/core/column/column.py
Original file line number Diff line number Diff line change
Expand Up @@ -1948,8 +1948,23 @@ def as_column(
raise TypeError(
f"Cannot convert a {inferred_dtype} of object type"
)
elif nan_as_null is False and (
pd.isna(arbitrary).any()
elif inferred_dtype == "boolean":
if cudf.get_option("mode.pandas_compatible"):
if not (
dtype is not None
and dtype == np.dtype("bool")
galipremsagar marked this conversation as resolved.
Show resolved Hide resolved
and not pd.isna(arbitrary).any()
):
galipremsagar marked this conversation as resolved.
Show resolved Hide resolved
raise MixedTypeError(
f"Cannot have mixed values with {inferred_dtype}"
)
elif nan_as_null is False and _has_any_nan(arbitrary):
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
elif nan_as_null is False and _has_any_nan(arbitrary):
elif not nan_as_null and _has_any_nan(arbitrary):

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We need this comparison because None is also a supported parameter and it is similar to True behavior.

raise MixedTypeError(
f"Cannot have mixed values with {inferred_dtype}"
)
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 _has_any_nan(arbitrary)
and inferred_dtype not in ("decimal", "empty")
galipremsagar marked this conversation as resolved.
Show resolved Hide resolved
):
# Decimal can hold float("nan")
Expand Down Expand Up @@ -2329,3 +2344,10 @@ def concat_columns(objs: "MutableSequence[ColumnBase]") -> ColumnBase:

# Filter out inputs that have 0 length, then concatenate.
return libcudf.concat.concat_columns([o for o in objs if len(o)])


def _has_any_nan(arbitrary):
mroeschke marked this conversation as resolved.
Show resolved Hide resolved
galipremsagar marked this conversation as resolved.
Show resolved Hide resolved
return any(
(isinstance(x, (np.floating, float)) and np.isnan(x))
galipremsagar marked this conversation as resolved.
Show resolved Hide resolved
for x in np.asarray(arbitrary)
)
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
23 changes: 18 additions & 5 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 @@ -2356,12 +2358,23 @@ def test_multi_dim_series_error():

def test_bool_series_mixed_dtype_error():
ps = pd.Series([True, False, None])
all_bool_ps = pd.Series([True, False, True], dtype="object")
# ps now has `object` dtype, which
# isn't supported by `cudf`.
with cudf.option_context("mode.pandas_compatible", True):
with pytest.raises(TypeError):
cudf.Series(ps)
with pytest.raises(TypeError):
cudf.from_pandas(ps)
with pytest.raises(TypeError):
cudf.Series(ps, dtype=bool)
expected = cudf.Series(all_bool_ps, dtype=bool)
assert_eq(expected, all_bool_ps.astype(bool))
nan_bools_mix = pd.Series([True, False, True, np.nan], dtype="object")
gs = cudf.Series(nan_bools_mix, nan_as_null=True)
assert_eq(gs.to_pandas(nullable=True), nan_bools_mix.astype("boolean"))
with pytest.raises(TypeError):
cudf.Series(ps, nan_as_null=False)
with pytest.raises(TypeError):
cudf.from_pandas(ps, nan_as_null=False)
cudf.Series(nan_bools_mix, nan_as_null=False)


@pytest.mark.parametrize(
Expand Down
Loading