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

Preserve column ordering in DataFrame.stack #14626

Merged
Merged
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
22 changes: 15 additions & 7 deletions python/cudf/cudf/core/dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -6749,11 +6749,11 @@ def stack(self, level=-1, dropna=True):
cat 1.0 2.0
dog 3.0 4.0
>>> df_multi_level_cols2.stack()
height weight
cat kg <NA> 1.0
m 2.0 <NA>
dog kg <NA> 3.0
m 4.0 <NA>
weight height
cat kg 1.0 <NA>
m <NA> 2.0
dog kg 3.0 <NA>
m <NA> 4.0

**Prescribing the level(s) to be stacked**

Expand Down Expand Up @@ -6925,10 +6925,18 @@ def unnamed_group_generator():
else:
if unnamed_level_values.nlevels == 1:
unnamed_level_values = unnamed_level_values.get_level_values(0)
unnamed_level_values = unnamed_level_values.unique().sort_values()
unnamed_level_values = unnamed_level_values.unique()

data = ColumnAccessor(
dict(zip(unnamed_level_values, stacked)),
dict(
zip(
unnamed_level_values,
[
stacked[i]
for i in unnamed_level_values.argsort().argsort()
],
)
),
isinstance(unnamed_level_values, pd.MultiIndex),
unnamed_level_values.names,
)
Expand Down
Loading