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

Add handling for nulls in dask_cudf.sorting.quantile_divisions #9171

Merged
merged 4 commits into from
Sep 7, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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: 8 additions & 2 deletions python/dask_cudf/dask_cudf/sorting.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,14 @@ def quantile_divisions(df, by, npartitions):
dtype = df[columns[0]].dtype
divisions = divisions[columns[0]].astype("int64")
divisions.iloc[-1] += 1
divisions = sorted(
divisions.drop_duplicates().astype(dtype).values.tolist()
divisions = (
sorted(
divisions.dropna()
.drop_duplicates()
.astype(dtype)
.values.tolist()
)
+ [None] * divisions.null_count
)
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we use .to_arrow and sort, that way we are tampering the null ordering?

Copy link
Member Author

Choose a reason for hiding this comment

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

Were you thinking something like

        divisions = (
            sorted(
                divisions.drop_duplicates()
                .astype(dtype)
                .to_arrow().tolist()
            )

In that case, the null ordering is still being tampered by drop_duplicates, which places nulls first. From there we can't sort the resulting list as it contains None, unless you meant using an arrow sorting method before calling tolist()?

Copy link
Contributor

Choose a reason for hiding this comment

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

I see, didn't notice the drop_duplicates call, yea drop_duplicates results in non-deterministic ordering. But .to_arrow().tolist() seems cleaner to me.

Copy link
Member Author

Choose a reason for hiding this comment

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

Okay, in that case do you have any preference for how we handle sorting the list with nulls? Doing a quick search, this seems like a suitable solution:

# https://stackoverflow.com/questions/18411560/sort-list-while-pushing-none-values-to-the-end

sorted(..., key=lambda x: (x is None, x))

Copy link
Contributor

Choose a reason for hiding this comment

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

Try sort_indices of pyarrow, it seems to be doing what you want to do: https://arrow.apache.org/docs/python/generated/pyarrow.compute.sort_indices.html

If that's not possible the StackOverflow approach looks fine to me.

else:
for col in columns:
Expand Down
16 changes: 16 additions & 0 deletions python/dask_cudf/dask_cudf/tests/test_sort.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,19 @@ def test_sort_repartition():
new_ddf = ddf.shuffle(on="a", ignore_index=True, npartitions=3)

dd.assert_eq(len(new_ddf), len(ddf))


@pytest.mark.parametrize("by", ["a", "b", ["a", "b"]])
def test_sort_values_with_nulls(by):
df = cudf.DataFrame(
{
"a": list(range(50)) + [None] * 50 + list(range(50, 100)),
"b": [None] * 100 + list(range(100, 150)),
}
)
charlesbluca marked this conversation as resolved.
Show resolved Hide resolved
ddf = dd.from_pandas(df, npartitions=10)

got = ddf.sort_values(by=by)
expect = df.sort_values(by=by)

dd.assert_eq(got, expect)