Repository: modin-project/modin

Issue:
`modin_df.reset_index` is failed with `ValueError: cannot insert colname, already exists` but pandas works
### System information
- **OS Platform and Distribution (e.g., Linux Ubuntu 16.04)**: Ubuntu 20.04
- **Modin version** (`modin.__version__`): b7027597985a442734a57949d72e3fa0625b4d43
- **Python version**: 3.8.11
- **Code we can use to reproduce**:

```python
# import pandas as pd
import modin.pandas as pd

df = pd.DataFrame({"col0": [0,1,2,3]}, index=pd.Index([11, 22, 33, 44], name="col0"))

df.columns = ["col0_1"]
df = df.reset_index()

print(df)
```

<!--
You can obtain the Modin version with

python -c "import modin; print(modin.__version__)"
-->

### Describe the problem
<!-- Describe the problem clearly here. -->

Modin throws an error but pandas doesn't. The issue is observed in Santander workload. A possible reason is not correct propagating of new names of columns to lowest levels of architecture.


### Source code / logs
<!-- 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. -->

Modin output:
```bash
During handling of the above exception, another exception occurred:

ray::apply_list_of_funcs() (pid=2800927, ip=10.241.129.69)
  File "/localdisk/aprutsko/modin/modin/core/execution/ray/implementations/pandas_on_ray/partitioning/partition.py", line 417, in apply_list_of_funcs
    partition = func(partition.copy(), *args, **kwargs)
  File "/localdisk/aprutsko/modin/modin/core/dataframe/pandas/dataframe/dataframe.py", line 827, in from_labels_executor
    return df.reset_index()
  File "/localdisk/aprutsko/miniconda/envs/modin/lib/python3.8/site-packages/pandas/util/_decorators.py", line 311, in wrapper
    return func(*args, **kwargs)
  File "/localdisk/aprutsko/miniconda/envs/modin/lib/python3.8/site-packages/pandas/core/frame.py", line 5841, in reset_index
    new_obj.insert(0, name, level_values)
  File "/localdisk/aprutsko/miniconda/envs/modin/lib/python3.8/site-packages/pandas/core/frame.py", line 4442, in insert
    raise ValueError(f"cannot insert {column}, already exists")
ValueError: cannot insert col0, already exists
```

Pandas output:
```bash
   col0  col0_1
0    11       0
1    22       1
2    33       2
3    44       3
```



Discussion:

Resolving patch:
diff --git a/modin/core/dataframe/pandas/dataframe/dataframe.py b/modin/core/dataframe/pandas/dataframe/dataframe.py
--- a/modin/core/dataframe/pandas/dataframe/dataframe.py
+++ b/modin/core/dataframe/pandas/dataframe/dataframe.py
@@ -828,7 +828,18 @@ def from_labels(self) -> "PandasDataframe":
         def from_labels_executor(df, **kwargs):
             # Setting the names here ensures that external and internal metadata always match.
             df.index.names = new_column_names
-            return df.reset_index()
+
+            # Handling of a case when columns have the same name as one of index levels names.
+            # In this case `df.reset_index` provides errors related to columns duplication.
+            # This case is possible because columns metadata updating is deffered. To workaround
+            # `df.reset_index` error we allow columns duplucation in "if" branch via `concat`.
+            if any(name_level in df.columns for name_level in df.index.names):
+                columns_to_add = df.index.to_frame()
+                columns_to_add.reset_index(drop=True, inplace=True)
+                df = df.reset_index(drop=True)
+                return pandas.concat([columns_to_add, df], axis=1, copy=False)
+            else:
+                return df.reset_index()
 
         new_parts = self._partition_mgr_cls.apply_func_to_select_indices(
             0,


Test patch:
diff --git a/modin/pandas/test/dataframe/test_indexing.py b/modin/pandas/test/dataframe/test_indexing.py
--- a/modin/pandas/test/dataframe/test_indexing.py
+++ b/modin/pandas/test/dataframe/test_indexing.py
@@ -1292,6 +1292,23 @@ def test_reset_index_with_named_index(index_levels_names_max_levels):
     df_equals(modin_df.reset_index(drop=False), pandas_df.reset_index(drop=False))
 
 
+@pytest.mark.parametrize(
+    "index",
+    [
+        pandas.Index([11, 22, 33, 44], name="col0"),
+        pandas.MultiIndex.from_product(
+            [[100, 200], [300, 400]], names=["level1", "col0"]
+        ),
+    ],
+    ids=["index", "multiindex"],
+)
+def test_reset_index_metadata_update(index):
+    modin_df, pandas_df = create_test_dfs({"col0": [0, 1, 2, 3]}, index=index)
+    modin_df.columns = pandas_df.columns = ["col1"]
+
+    eval_general(modin_df, pandas_df, lambda df: df.reset_index())
+
+
 @pytest.mark.parametrize("data", test_data_values, ids=test_data_keys)
 @pytest.mark.parametrize("axis", axis_values, ids=axis_keys)
 def test_sample(data, axis):