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

Fix id_vars and value_vars not accepting string scalars in melt #15765

Merged
merged 2 commits into from
May 16, 2024
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
27 changes: 13 additions & 14 deletions python/cudf/cudf/core/reshape.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@

import itertools
import warnings
from collections import abc
from typing import Dict, Optional

import cupy
import numpy as np
import pandas as pd

Expand Down Expand Up @@ -590,7 +588,7 @@ def melt(

# id_vars
if id_vars is not None:
if not isinstance(id_vars, abc.Sequence):
if cudf.api.types.is_scalar(id_vars):
id_vars = [id_vars]
id_vars = list(id_vars)
missing = set(id_vars) - set(frame._column_names)
Expand All @@ -604,7 +602,7 @@ def melt(

# value_vars
if value_vars is not None:
if not isinstance(value_vars, abc.Sequence):
if cudf.api.types.is_scalar(value_vars):
value_vars = [value_vars]
value_vars = list(value_vars)
missing = set(value_vars) - set(frame._column_names)
Expand Down Expand Up @@ -658,21 +656,22 @@ def _tile(A, reps):
# Step 2: add variable
nval = len(value_vars)
dtype = min_unsigned_type(nval)
temp = cudf.Series(cupy.repeat(cupy.arange(nval, dtype=dtype), N))

if not var_name:
var_name = "variable"

mdata[var_name] = cudf.Series(
cudf.core.column.build_categorical_column(
categories=value_vars,
codes=temp._column,
mask=temp._column.base_mask,
size=temp._column.size,
offset=temp._column.offset,
ordered=False,
if not value_vars:
# TODO: Use frame._data.label_dtype when it's more consistently set
var_data = cudf.Series(
value_vars, dtype=frame._data.to_pandas_index().dtype
)
)
else:
var_data = (
cudf.Series(value_vars)
.take(np.repeat(np.arange(nval, dtype=dtype), N))
.reset_index(drop=True)
)
mdata[var_name] = var_data

# Step 3: add values
mdata[value_name] = cudf.Series._concat(
Expand Down
27 changes: 19 additions & 8 deletions python/cudf/cudf/tests/test_reshape.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import pytest

import cudf
from cudf import melt as cudf_melt
from cudf.core._compat import PANDAS_CURRENT_SUPPORTED_VERSION, PANDAS_VERSION
from cudf.core.buffer.spill_manager import get_global_manager
from cudf.testing._utils import (
Expand Down Expand Up @@ -71,15 +70,10 @@ def test_melt(nulls, num_id_vars, num_value_vars, num_rows, dtype):

gdf = cudf.from_pandas(pdf)

got = cudf_melt(frame=gdf, id_vars=id_vars, value_vars=value_vars)
got = cudf.melt(frame=gdf, id_vars=id_vars, value_vars=value_vars)
got_from_melt_method = gdf.melt(id_vars=id_vars, value_vars=value_vars)

expect = pd.melt(frame=pdf, id_vars=id_vars, value_vars=value_vars)
# pandas' melt makes the 'variable' column of 'object' type (string)
# cuDF's melt makes it Categorical because it doesn't support strings
expect["variable"] = expect["variable"].astype(
got["variable"].dtype.to_pandas()
)

assert_eq(expect, got)

Expand All @@ -98,11 +92,28 @@ def test_melt_many_columns():
grid_df_d = cudf.melt(
df_d, id_vars=["id"], var_name="d", value_name="sales"
)
grid_df_d["d"] = grid_df_d["d"].astype("str")
grid_df_d["d"] = grid_df_d["d"]

assert_eq(grid_df, grid_df_d)


def test_melt_str_scalar_id_var():
data = {"index": [1, 2], "id": [1, 2], "d0": [10, 20], "d1": [30, 40]}
result = cudf.melt(
cudf.DataFrame(data),
id_vars="index",
var_name="column",
value_name="value",
)
expected = pd.melt(
pd.DataFrame(data),
id_vars="index",
var_name="column",
value_name="value",
)
assert_eq(result, expected)


@pytest.mark.parametrize("num_cols", [1, 2, 10])
@pytest.mark.parametrize("num_rows", [1, 2, 1000])
@pytest.mark.parametrize(
Expand Down
Loading