Repository: modin-project/modin

Issue:
Duplicated columns when looping through groupby object
<!--
General questions should be asked on the mailing list modin-dev@googlegroups.com.

Before submitting an issue, please fill out the following form.
-->

### System information
- **OS Platform and Distribution (e.g., Linux Ubuntu 16.04)**: OSX
- **Modin installed from (source or binary)**: current master
- **Modin version**: N/A
- **Python version**: 3.6
- **Exact command to reproduce**: `for k, v in df.groupby(df.columns, axis=1)`

<!--
You can obtain the Modin version with

python -c "import modin; print(modin.__version__)"
-->

### Describe the problem
Modin is replicating columns similar to in #458 for `axis=1`. The code fix should be similar to that in #459.
<!-- Describe the problem clearly here. -->

### Source code / logs

```python
import modin.pandas as pd
import numpy as np
import pandas

data = {"a": np.random.randint(0, 100, size=l), "b": np.random.randint(0, 100, size=l), "c": np.random.randint(0, 100, size=l)}
    modin_df = pd.DataFrame(data, index=idx, columns=list("aba"))
    pandas_df = pandas.DataFrame(data, index=idx, columns=list("aba"))
    modin_groupby_obj = modin_df.groupby(modin_df.columns, axis=1)
    pandas_groupby_obj = pandas_df.groupby(pandas_df.columns, axis=1)

    modin_dict = {k: v for k, v in modin_groupby_obj}
    pandas_dict = {k: v for k, v in pandas_groupby_obj}
    for k in modin_dict:
        ray_df_equals_pandas(modin_dict[k], pandas_dict[k])
```
<!-- Include any logs or source code that would be helpful to diagnose the problem. If including tracebacks, please include the full traceback. Large logs and files should be attached. Try to provide a reproducible test case that is the bare minimum necessary to generate the problem. -->



Discussion:

Resolving patch:
diff --git a/modin/pandas/groupby.py b/modin/pandas/groupby.py
--- a/modin/pandas/groupby.py
+++ b/modin/pandas/groupby.py
@@ -41,7 +41,7 @@ def __init__(
         self._columns = self._query_compiler.columns
         self._by = by
         # This tells us whether or not there are multiple columns/rows in the groupby
-        self._is_multi_by = all(obj in self._df for obj in self._by)
+        self._is_multi_by = all(obj in self._df for obj in self._by) and axis == 0
         self._level = level
         self._kwargs = {
             "sort": sort,
@@ -113,7 +113,7 @@ def _iter(self):
                     k,
                     DataFrame(
                         query_compiler=self._query_compiler.getitem_row_array(
-                            self._index.get_indexer_for(self._index_grouped[k])
+                            self._index.get_indexer_for(self._index_grouped[k].unique())
                         )
                     ),
                 )
@@ -125,7 +125,7 @@ def _iter(self):
                     k,
                     DataFrame(
                         query_compiler=self._query_compiler.getitem_column_array(
-                            self._index_grouped[k]
+                            self._index_grouped[k].unique()
                         )
                     ),
                 )


Test patch:
diff --git a/modin/pandas/test/test_groupby.py b/modin/pandas/test/test_groupby.py
--- a/modin/pandas/test/test_groupby.py
+++ b/modin/pandas/test/test_groupby.py
@@ -764,3 +764,32 @@ def test_groups(ray_groupby, pandas_groupby):
 @pytest.fixture
 def test_shift(ray_groupby, pandas_groupby):
     assert ray_groupby.groups == pandas_groupby.groups
+
+
+def test_groupby_on_index_values_with_loop():
+    length = 2 ** 6
+    data = {
+        "a": np.random.randint(0, 100, size=length),
+        "b": np.random.randint(0, 100, size=length),
+        "c": np.random.randint(0, 100, size=length),
+    }
+    idx = ["g1" if i % 3 != 0 else "g2" for i in range(length)]
+    modin_df = pd.DataFrame(data, index=idx, columns=list("aba"))
+    pandas_df = pandas.DataFrame(data, index=idx, columns=list("aba"))
+    modin_groupby_obj = modin_df.groupby(modin_df.index)
+    pandas_groupby_obj = pandas_df.groupby(pandas_df.index)
+
+    modin_dict = {k: v for k, v in modin_groupby_obj}
+    pandas_dict = {k: v for k, v in pandas_groupby_obj}
+
+    for k in modin_dict:
+        ray_df_equals_pandas(modin_dict[k], pandas_dict[k])
+
+    modin_groupby_obj = modin_df.groupby(modin_df.columns, axis=1)
+    pandas_groupby_obj = pandas_df.groupby(pandas_df.columns, axis=1)
+
+    modin_dict = {k: v for k, v in modin_groupby_obj}
+    pandas_dict = {k: v for k, v in pandas_groupby_obj}
+
+    for k in modin_dict:
+        ray_df_equals_pandas(modin_dict[k], pandas_dict[k])