Skip to content

Commit

Permalink
Allow fillna to validate for CategoricalColumn.fillna (#15683)
Browse files Browse the repository at this point in the history
Fixes: #15666 

This PR validates values passed to `fillna` even if there are no null values in a categorical column.

Forks from #14534

Authors:
  - GALI PREM SAGAR (https://github.com/galipremsagar)

Approvers:
  - Matthew Roeschke (https://github.com/mroeschke)

URL: #15683
  • Loading branch information
galipremsagar authored May 7, 2024
1 parent 0cfdbc1 commit e87a78d
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 7 deletions.
8 changes: 5 additions & 3 deletions python/cudf/cudf/core/column/categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -1045,9 +1045,6 @@ def fillna(
"""
Fill null values with *fill_value*
"""
if not self.nullable:
return self

if fill_value is not None:
fill_is_scalar = np.isscalar(fill_value)

Expand Down Expand Up @@ -1079,6 +1076,11 @@ def fillna(
self.codes.dtype
)

# Validation of `fill_value` will have to be performed
# before returning self.
if not self.nullable:
return self

return super().fillna(fill_value, method=method)

def indices_of(
Expand Down
15 changes: 11 additions & 4 deletions python/cudf/cudf/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -762,10 +762,17 @@ def fillna(
else:
replace_val = None
should_fill = (
col_name in value
and col.has_nulls(include_nan=True)
and not libcudf.scalar._is_null_host_scalar(replace_val)
) or method is not None
(
col_name in value
and col.has_nulls(include_nan=True)
and not libcudf.scalar._is_null_host_scalar(replace_val)
)
or method is not None
or (
isinstance(col, cudf.core.column.CategoricalColumn)
and not libcudf.scalar._is_null_host_scalar(replace_val)
)
)
if should_fill:
filled_data[col_name] = col.fillna(replace_val, method)
else:
Expand Down
16 changes: 16 additions & 0 deletions python/cudf/cudf/tests/test_categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -859,3 +859,19 @@ def test_cat_from_scalar(scalar):
gs = cudf.Series(scalar, dtype="category")

assert_eq(ps, gs)


def test_cat_groupby_fillna():
ps = pd.Series(["a", "b", "c"], dtype="category")
gs = cudf.from_pandas(ps)

with pytest.warns(FutureWarning):
pg = ps.groupby(ps)
gg = gs.groupby(gs)

assert_exceptions_equal(
lfunc=pg.fillna,
rfunc=gg.fillna,
lfunc_args_and_kwargs=(("d",), {}),
rfunc_args_and_kwargs=(("d",), {}),
)

0 comments on commit e87a78d

Please sign in to comment.