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

Enable codes dtype parity in pandas-compatibility mode for factorize API #13982

Merged
merged 1 commit into from
Aug 28, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
5 changes: 4 additions & 1 deletion python/cudf/cudf/core/algorithms.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from cudf.core.indexed_frame import IndexedFrame
from cudf.core.scalar import Scalar
from cudf.core.series import Series
from cudf.options import get_option


def factorize(
Expand Down Expand Up @@ -137,7 +138,9 @@ def factorize(
cats = cats.sort_values()

labels = values._column._label_encoding(
cats=cats, na_sentinel=Scalar(na_sentinel)
cats=cats,
na_sentinel=Scalar(na_sentinel),
dtype="int64" if get_option("mode.pandas_compatible") else None,
).values

return labels, cats.values if return_cupy_array else Index(cats)
Expand Down
19 changes: 18 additions & 1 deletion python/cudf/cudf/tests/test_factorize.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) 2018-2022, NVIDIA CORPORATION.
# Copyright (c) 2018-2023, NVIDIA CORPORATION.

import cupy as cp
import numpy as np
Expand Down Expand Up @@ -122,6 +122,23 @@ def test_cudf_factorize_array():
np.testing.assert_array_equal(expect[1], got[1].get())


@pytest.mark.parametrize("pandas_compatibility", [True, False])
def test_factorize_code_pandas_compatibility(pandas_compatibility):

psr = pd.Series([1, 2, 3, 4, 5])
gsr = cudf.from_pandas(psr)

expect = pd.factorize(psr)
with cudf.option_context("mode.pandas_compatible", pandas_compatibility):
got = cudf.factorize(gsr)
assert_eq(got[0], expect[0])
assert_eq(got[1], expect[1])
if pandas_compatibility:
assert got[0].dtype == expect[0].dtype
else:
assert got[0].dtype == cudf.dtype("int8")


def test_factorize_result_classes():
data = [1, 2, 3]

Expand Down