Skip to content

Commit 1c4d173

Browse files
authored
FIX-#7327: Use sort parameter of DataFrame.stack (#7396)
Signed-off-by: Jonathan Shi <[email protected]>
1 parent cc717a0 commit 1c4d173

File tree

4 files changed

+22
-6
lines changed

4 files changed

+22
-6
lines changed

modin/core/storage_formats/base/query_compiler.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1956,21 +1956,25 @@ def searchsorted(self, **kwargs): # noqa: PR02
19561956
# END Abstract map partitions operations
19571957

19581958
@doc_utils.add_refer_to("DataFrame.stack")
1959-
def stack(self, level, dropna):
1959+
def stack(self, level, dropna, sort):
19601960
"""
19611961
Stack the prescribed level(s) from columns to index.
19621962
19631963
Parameters
19641964
----------
19651965
level : int or label
19661966
dropna : bool
1967+
sort : bool
19671968
19681969
Returns
19691970
-------
19701971
BaseQueryCompiler
19711972
"""
19721973
return DataFrameDefault.register(pandas.DataFrame.stack)(
1973-
self, level=level, dropna=dropna
1974+
self,
1975+
level=level,
1976+
dropna=dropna,
1977+
sort=sort,
19741978
)
19751979

19761980
# Abstract map partitions across select indices

modin/core/storage_formats/pandas/query_compiler.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1962,7 +1962,7 @@ def get_unique_level_values(index):
19621962
result = result.reindex(0, new_index)
19631963
return result
19641964

1965-
def stack(self, level, dropna):
1965+
def stack(self, level, dropna, sort):
19661966
if not isinstance(self.columns, pandas.MultiIndex) or (
19671967
isinstance(self.columns, pandas.MultiIndex)
19681968
and is_list_like(level)
@@ -1974,7 +1974,9 @@ def stack(self, level, dropna):
19741974

19751975
new_modin_frame = self._modin_frame.apply_full_axis(
19761976
1,
1977-
lambda df: pandas.DataFrame(df.stack(level=level, dropna=dropna)),
1977+
lambda df: pandas.DataFrame(
1978+
df.stack(level=level, dropna=dropna, sort=sort)
1979+
),
19781980
new_columns=new_columns,
19791981
)
19801982
return self.__constructor__(new_modin_frame)

modin/pandas/dataframe.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2118,11 +2118,11 @@ def stack(
21182118
is_multiindex and is_list_like(level) and len(level) == self.columns.nlevels
21192119
):
21202120
return self._reduce_dimension(
2121-
query_compiler=self._query_compiler.stack(level, dropna)
2121+
query_compiler=self._query_compiler.stack(level, dropna, sort)
21222122
)
21232123
else:
21242124
return self.__constructor__(
2125-
query_compiler=self._query_compiler.stack(level, dropna)
2125+
query_compiler=self._query_compiler.stack(level, dropna, sort)
21262126
)
21272127

21282128
def sub(

modin/tests/pandas/dataframe/test_default.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1181,6 +1181,16 @@ def test_stack(data, is_multi_idx, is_multi_col):
11811181
df_equals(modin_df.stack(level=[0, 1, 2]), pandas_df.stack(level=[0, 1, 2]))
11821182

11831183

1184+
@pytest.mark.parametrize("sort", [True, False])
1185+
def test_stack_sort(sort):
1186+
# Example frame slightly modified from pandas docs to be unsorted
1187+
cols = pd.MultiIndex.from_tuples([("weight", "pounds"), ("weight", "kg")])
1188+
modin_df, pandas_df = create_test_dfs(
1189+
[[1, 2], [2, 4]], index=["cat", "dog"], columns=cols
1190+
)
1191+
df_equals(modin_df.stack(sort=sort), pandas_df.stack(sort=sort))
1192+
1193+
11841194
@pytest.mark.parametrize("data", test_data_values, ids=test_data_keys)
11851195
@pytest.mark.parametrize("axis1", [0, 1])
11861196
@pytest.mark.parametrize("axis2", [0, 1])

0 commit comments

Comments
 (0)