Repository: modin-project/modin

Issue:
Passing MultiIndex to loc leads to keyError
DataFrame result has colums, ['code_jq', 'time', 'factor', 'close'].

> result = result.set_index(['code_jq', 'time']).sort_index(ascending=False);
cut = result.groupby(['code_jq'])['factor'].apply(lambda x: len(x) >= total_days - 10);
result = result.loc[cut[cut].index]

When the program runs to this line of code, `result = result.loc[cut[cut].index]` ,it shows keyerror that means dataframe can't find keys. 

Is anyone know why and how to solve it?



Discussion:
Hi @513090336, thanks for the report. I was able to reproduce what you're seeing, it is an issue with `loc` and MultiIndex. We will get this fixed.

As a temporary workaround, you can use this code:

```python
result = result.set_index(['code_jq', 'time']).sort_index(ascending=False);
cut = result.groupby(['code_jq'])['factor'].apply(lambda x: len(x) >= total_days - 10)
indexer = cut.index.get_indexer_for(cut[cut].index)
result = result.iloc[indexer]
```
Thank you！you helps me a lot.

Resolving patch:
diff --git a/modin/pandas/indexing.py b/modin/pandas/indexing.py
--- a/modin/pandas/indexing.py
+++ b/modin/pandas/indexing.py
@@ -337,7 +337,10 @@ def _compute_lookup(self, row_loc, col_loc):
                 self.qc.index.to_series().loc[row_loc]
             )
         elif isinstance(self.qc.index, pandas.MultiIndex):
-            row_lookup = self.qc.index.get_locs(row_loc)
+            if isinstance(row_loc, pandas.MultiIndex):
+                row_lookup = self.qc.index.get_indexer_for(row_loc)
+            else:
+                row_lookup = self.qc.index.get_locs(row_loc)
         elif is_boolean_array(row_loc):
             # If passed in a list of booleans, we return the index of the true values
             row_lookup = [i for i, row_val in enumerate(row_loc) if row_val]
@@ -348,7 +351,10 @@ def _compute_lookup(self, row_loc, col_loc):
                 self.qc.columns.to_series().loc[col_loc]
             )
         elif isinstance(self.qc.columns, pandas.MultiIndex):
-            col_lookup = self.qc.columns.get_locs(col_loc)
+            if isinstance(col_loc, pandas.MultiIndex):
+                col_lookup = self.qc.columns.get_indexer_for(col_loc)
+            else:
+                col_lookup = self.qc.columns.get_locs(col_loc)
         elif is_boolean_array(col_loc):
             # If passed in a list of booleans, we return the index of the true values
             col_lookup = [i for i, col_val in enumerate(col_loc) if col_val]


Test patch:
diff --git a/modin/pandas/test/test_dataframe.py b/modin/pandas/test/test_dataframe.py
--- a/modin/pandas/test/test_dataframe.py
+++ b/modin/pandas/test/test_dataframe.py
@@ -4560,6 +4560,10 @@ def test_loc_multi_index(self):
             transposed_pandas.loc[transposed_pandas.index[:-2], :],
         )
 
+        # From issue #1610
+        df_equals(modin_df.loc[modin_df.index], pandas_df.loc[pandas_df.index])
+        df_equals(modin_df.loc[modin_df.index[:7]], pandas_df.loc[pandas_df.index[:7]])
+
     def test_loc_assignment(self):
         modin_df = pd.DataFrame(
             index=["row1", "row2", "row3"], columns=["col1", "col2"]