|
13 | 13 |
|
14 | 14 | """Implement utils for pandas component."""
|
15 | 15 |
|
| 16 | +from pandas import MultiIndex |
| 17 | + |
16 | 18 |
|
17 | 19 | def from_non_pandas(df, index, columns, dtype):
|
18 | 20 | """
|
@@ -105,3 +107,38 @@ def is_scalar(obj):
|
105 | 107 | from .base import BasePandasDataset
|
106 | 108 |
|
107 | 109 | 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