Skip to content

Commit

Permalink
Fixed MultiIndex.from_frame implementation (#2587)
Browse files Browse the repository at this point in the history
Signed-off-by: Gregory Shimansky <gregory.shimansky@intel.com>
  • Loading branch information
gshimansky authored Jan 13, 2021
1 parent 7cfc85c commit d663730
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
14 changes: 14 additions & 0 deletions modin/pandas/test/dataframe/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -1325,3 +1325,17 @@ def test_index_order():
getattr(df_modin, func)(level=0).index,
getattr(df_pandas, func)(level=0).index,
)


@pytest.mark.parametrize("data", test_data_values, ids=test_data_keys)
@pytest.mark.parametrize("sortorder", [0, 3, 5])
def test_multiindex_from_frame(data, sortorder):
modin_df, pandas_df = create_test_dfs(data)

def call_from_frame(df):
if type(df).__module__.startswith("pandas"):
return pandas.MultiIndex.from_frame(df, sortorder)
else:
return pd.MultiIndex.from_frame(df, sortorder)

eval_general(modin_df, pandas_df, call_from_frame, comparator=assert_index_equal)
37 changes: 37 additions & 0 deletions modin/pandas/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

"""Implement utils for pandas component."""

from pandas import MultiIndex


def from_non_pandas(df, index, columns, dtype):
"""
Expand Down Expand Up @@ -105,3 +107,38 @@ def is_scalar(obj):
from .base import BasePandasDataset

return not isinstance(obj, BasePandasDataset) and pandas_is_scalar(obj)


def from_modin_frame_to_mi(df, sortorder=None, names=None):
"""
Make a pandas.MultiIndex from a DataFrame.
Parameters
----------
df : DataFrame
DataFrame to be converted to pandas.MultiIndex.
sortorder : int, optional
Level of sortedness (must be lexicographically sorted by that
level).
names : list-like, optional
If no names are provided, use the column names, or tuple of column
names if the columns is a MultiIndex. If a sequence, overwrite
names with the given sequence.
Returns
-------
pandas.MultiIndex
The pandas.MultiIndex representation of the given DataFrame.
"""
from .dataframe import DataFrame

if isinstance(df, DataFrame):
from modin.error_message import ErrorMessage

ErrorMessage.default_to_pandas("`MultiIndex.from_frame`")
df = df._to_pandas()
return _original_pandas_MultiIndex_from_frame(df, sortorder, names)


_original_pandas_MultiIndex_from_frame = MultiIndex.from_frame
MultiIndex.from_frame = from_modin_frame_to_mi

0 comments on commit d663730

Please sign in to comment.