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 future_stack to DataFrame.stack #15015

Merged
merged 4 commits into from
Feb 10, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
47 changes: 39 additions & 8 deletions python/cudf/cudf/core/dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -6711,7 +6711,7 @@ def to_orc(
)

@_cudf_nvtx_annotate
def stack(self, level=-1, dropna=True):
def stack(self, level=-1, dropna=no_default, future_stack=False):
"""Stack the prescribed level(s) from columns to index

Return a reshaped DataFrame or Series having a multi-level
Expand Down Expand Up @@ -6843,6 +6843,23 @@ def stack(self, level=-1, dropna=True):
weight kg 3.0
dtype: float64
"""
if future_stack:
if dropna is not no_default:
raise ValueError(
"dropna must be unspecified with future_stack=True as the new "
"implementation does not introduce rows of NA values. This "
"argument will be removed in a future version of cudf."
)
else:
if dropna is not no_default or self._data.nlevels > 1:
warnings.warn(
"The previous implementation of stack is deprecated and will be "
"removed in a future version of cudf. Specify future_stack=True "
"to adopt the new implementation and silence this warning.",
FutureWarning,
)
if dropna is no_default:
dropna = True

if isinstance(level, (int, str)):
level = [level]
Expand All @@ -6858,7 +6875,7 @@ def stack(self, level=-1, dropna=True):

level = [level] if not isinstance(level, list) else level

if len(level) > 1 and not dropna:
if not future_stack and len(level) > 1 and not dropna:
raise NotImplementedError(
"When stacking multiple levels, setting `dropna` to False "
"will generate new column combination that does not exist "
Expand Down Expand Up @@ -6900,7 +6917,9 @@ def stack(self, level=-1, dropna=True):
# Since `level` may only specify a subset of all levels, `unique()` is
# required to remove duplicates. In pandas, the order of the keys in
# the specified levels are always sorted.
unique_named_levels = named_levels.unique().sort_values()
unique_named_levels = named_levels.unique()
if not future_stack:
unique_named_levels = unique_named_levels.sort_values()

# Each index from the original dataframe should repeat by the number
# of unique values in the named_levels
Expand Down Expand Up @@ -6949,11 +6968,19 @@ def unnamed_group_generator():
# `unique_named_levels` assigns -1 to these key
# combinations, representing an all-null column that
# is used in the subsequent libcudf call.
yield grpdf.reindex(
unique_named_levels, axis=0, fill_value=-1
).sort_index().values
if future_stack:
yield grpdf.reindex(
unique_named_levels, axis=0, fill_value=-1
).values
else:
yield grpdf.reindex(
unique_named_levels, axis=0, fill_value=-1
).sort_index().values
else:
yield column_idx_df.sort_index().values
if future_stack:
yield column_idx_df.values
else:
yield column_idx_df.sort_index().values

column_indices = list(unnamed_group_generator())

Expand Down Expand Up @@ -7004,6 +7031,10 @@ def unnamed_group_generator():
[
stacked[i]
for i in unnamed_level_values.argsort().argsort()
]
if not future_stack
else [
stacked[i] for i in unnamed_level_values.argsort()
],
)
),
Expand All @@ -7013,7 +7044,7 @@ def unnamed_group_generator():

result = DataFrame._from_data(data, index=new_index)

if dropna:
if not future_stack and dropna:
return result.dropna(how="all")
else:
return result
Expand Down
2 changes: 1 addition & 1 deletion python/cudf/cudf/core/reshape.py
Original file line number Diff line number Diff line change
Expand Up @@ -1120,7 +1120,7 @@ def unstack(df, level, fill_value=None):
"Calling unstack() on single index dataframe"
" with different column datatype is not supported."
)
res = df.T.stack(dropna=False)
res = df.T.stack(future_stack=False)
# Result's index is a multiindex
res.index.names = (
tuple(df._data.to_pandas_index().names) + df.index.names
Expand Down
25 changes: 21 additions & 4 deletions python/cudf/cudf/tests/test_reshape.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@

import cudf
from cudf import melt as cudf_melt
from cudf.core._compat import PANDAS_GE_220
from cudf.core.buffer.spill_manager import get_global_manager
from cudf.testing._utils import (
ALL_TYPES,
DATETIME_TYPES,
NUMERIC_TYPES,
assert_eq,
expect_warning_if,
)

pytest_xfail = pytest.mark.xfail
Expand Down Expand Up @@ -206,8 +208,15 @@ def test_df_stack_multiindex_column_axis(columns, index, level, dropna):
)
gdf = cudf.from_pandas(pdf)

got = gdf.stack(level=level, dropna=dropna)
expect = pdf.stack(level=level, dropna=dropna)
with pytest.warns(FutureWarning):
got = gdf.stack(level=level, dropna=dropna, future_stack=False)
with expect_warning_if(PANDAS_GE_220):
expect = pdf.stack(level=level, dropna=dropna, future_stack=False)
galipremsagar marked this conversation as resolved.
Show resolved Hide resolved

assert_eq(expect, got, check_dtype=False)

got = gdf.stack(level=level, future_stack=True)
expect = pdf.stack(level=level, future_stack=True)

assert_eq(expect, got, check_dtype=False)

Expand Down Expand Up @@ -242,8 +251,16 @@ def test_df_stack_multiindex_column_axis_pd_example(level):

df = pd.DataFrame(np.random.randn(4, 4), columns=columns)

expect = df.stack(level=level)
got = cudf.from_pandas(df).stack(level=level)
with expect_warning_if(PANDAS_GE_220):
expect = df.stack(level=level, future_stack=False)
gdf = cudf.from_pandas(df)
with pytest.warns(FutureWarning):
got = gdf.stack(level=level, future_stack=False)

assert_eq(expect, got)

expect = df.stack(level=level, future_stack=True)
got = gdf.stack(level=level, future_stack=True)

assert_eq(expect, got)

Expand Down
Loading