Skip to content

Commit d663730

Browse files
authored
Fixed MultiIndex.from_frame implementation (#2587)
Signed-off-by: Gregory Shimansky <[email protected]>
1 parent 7cfc85c commit d663730

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed

modin/pandas/test/dataframe/test_indexing.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1325,3 +1325,17 @@ def test_index_order():
13251325
getattr(df_modin, func)(level=0).index,
13261326
getattr(df_pandas, func)(level=0).index,
13271327
)
1328+
1329+
1330+
@pytest.mark.parametrize("data", test_data_values, ids=test_data_keys)
1331+
@pytest.mark.parametrize("sortorder", [0, 3, 5])
1332+
def test_multiindex_from_frame(data, sortorder):
1333+
modin_df, pandas_df = create_test_dfs(data)
1334+
1335+
def call_from_frame(df):
1336+
if type(df).__module__.startswith("pandas"):
1337+
return pandas.MultiIndex.from_frame(df, sortorder)
1338+
else:
1339+
return pd.MultiIndex.from_frame(df, sortorder)
1340+
1341+
eval_general(modin_df, pandas_df, call_from_frame, comparator=assert_index_equal)

modin/pandas/utils.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313

1414
"""Implement utils for pandas component."""
1515

16+
from pandas import MultiIndex
17+
1618

1719
def from_non_pandas(df, index, columns, dtype):
1820
"""
@@ -105,3 +107,38 @@ def is_scalar(obj):
105107
from .base import BasePandasDataset
106108

107109
return not isinstance(obj, BasePandasDataset) and pandas_is_scalar(obj)
110+
111+
112+
def from_modin_frame_to_mi(df, sortorder=None, names=None):
113+
"""
114+
Make a pandas.MultiIndex from a DataFrame.
115+
116+
Parameters
117+
----------
118+
df : DataFrame
119+
DataFrame to be converted to pandas.MultiIndex.
120+
sortorder : int, optional
121+
Level of sortedness (must be lexicographically sorted by that
122+
level).
123+
names : list-like, optional
124+
If no names are provided, use the column names, or tuple of column
125+
names if the columns is a MultiIndex. If a sequence, overwrite
126+
names with the given sequence.
127+
128+
Returns
129+
-------
130+
pandas.MultiIndex
131+
The pandas.MultiIndex representation of the given DataFrame.
132+
"""
133+
from .dataframe import DataFrame
134+
135+
if isinstance(df, DataFrame):
136+
from modin.error_message import ErrorMessage
137+
138+
ErrorMessage.default_to_pandas("`MultiIndex.from_frame`")
139+
df = df._to_pandas()
140+
return _original_pandas_MultiIndex_from_frame(df, sortorder, names)
141+
142+
143+
_original_pandas_MultiIndex_from_frame = MultiIndex.from_frame
144+
MultiIndex.from_frame = from_modin_frame_to_mi

0 commit comments

Comments
 (0)