Repository: modin-project/modin

Issue:
`df.to_csv` drops index column from CSV entirely
### System information
- **OS Platform and Distribution (e.g., Linux Ubuntu 16.04)**:
- **Modin version** (`modin.__version__`):
- **Python version**:
- **Code we can use to reproduce**:
```python
import modin.pandas as pd
cols = 100
arows = 20000
brows = 20000
keyrange = 100
a = pd.DataFrame(np.vstack([np.random.choice(keyrange,size=(arows)),np.random.normal(size=(cols,arows))]).transpose(),columns = ['key'] + ['avalue' + str(i) for i in range(1,1+cols)]).set_index('key')
b = pd.DataFrame(np.vstack([np.random.choice(keyrange,size=(brows)),np.random.normal(size=(cols,brows))]).transpose(),columns = ['key'] + ['bvalue' + str(i) for i in range(1,1+cols)]).set_index('key')
a.to_csv("a-big.csv")
b.to_csv("b-big.csv")
pd.read_csv("a-big.csv") # Has no key column
pd.read_csv("a-big.csv").set_index("key") # Errors
```
When using modin to write a Dataframe with a custom index to a csv file, the index is dropped entirely, and a range index is used when reading the Dataframe. Additionally, this may have been a transient error, but when I wrote the df using pandas, and then read it using modin, and called `set_index('key')` where `'key'` was the original index, the output displayed correctly, but I got the following `KeyError`:
```python
2022-01-20 13:23:49,267	ERROR worker.py:79 -- Unhandled error (suppress with RAY_IGNORE_UNHANDLED_ERRORS=1): ray::deploy_ray_func() (pid=78818, ip=127.0.0.1)
  File "/Users/rehandurrani/Documents/modin/modin/core/execution/ray/implementations/pandas_on_ray/partitioning/axis_partition.py", line 211, in deploy_ray_func
    result = func(*args)
  File "/Users/rehandurrani/Documents/modin/modin/core/dataframe/pandas/partitioning/axis_partition.py", line 160, in deploy_axis_func
    result = func(dataframe, **kwargs)
  File "/Users/rehandurrani/Documents/modin/modin/core/dataframe/pandas/dataframe/dataframe.py", line 1310, in _tree_reduce_func
    series_result = func(df, *args, **kwargs)
  File "/Users/rehandurrani/Documents/modin/modin/core/storage_formats/pandas/query_compiler.py", line 441, in map_func
    return pandas.merge(left, right, **kwargs)
  File "/Users/rehandurrani/.miniconda3/envs/modin/lib/python3.8/site-packages/pandas/core/reshape/merge.py", line 106, in merge
    op = _MergeOperation(
  File "/Users/rehandurrani/.miniconda3/envs/modin/lib/python3.8/site-packages/pandas/core/reshape/merge.py", line 699, in __init__
    ) = self._get_merge_keys()
  File "/Users/rehandurrani/.miniconda3/envs/modin/lib/python3.8/site-packages/pandas/core/reshape/merge.py", line 1096, in _get_merge_keys
    right_keys.append(right._get_label_or_level_values(rk))
  File "/Users/rehandurrani/.miniconda3/envs/modin/lib/python3.8/site-packages/pandas/core/generic.py", line 1779, in _get_label_or_level_values
    raise KeyError(key)
KeyError: 'key'
```

The code for the replication of this bug was borrowed from marsupialtail's [question](https://discuss.modin.org/t/modin-is-slower-than-pandas/255) on the [modin discourse](discuss.modin.org).
<!--
You can obtain the Modin version with

python -c "import modin; print(modin.__version__)"
-->

### Describe the problem
<!-- Describe the problem clearly here. -->

### 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. -->



Discussion:
The problem is not reproducible for 0.12.1 release, hence appeared later.
Found a commit that introduces the regression -https://github.com/modin-project/modin/commit/3c740dbfcdd69ddc3ab45a42be996e5c61104342.

@naren-ponder can you look at the problem?

Resolving patch:
diff --git a/modin/core/execution/ray/implementations/pandas_on_ray/io/io.py b/modin/core/execution/ray/implementations/pandas_on_ray/io/io.py
--- a/modin/core/execution/ray/implementations/pandas_on_ray/io/io.py
+++ b/modin/core/execution/ray/implementations/pandas_on_ray/io/io.py
@@ -111,6 +111,8 @@ def func(df):
             df.to_sql(**kwargs)
             return pandas.DataFrame()
 
+        # Ensure that the metadata is syncrhonized
+        qc._modin_frame._propagate_index_objs(axis=None)
         result = qc._modin_frame.apply_full_axis(1, func, new_index=[], new_columns=[])
         # FIXME: we should be waiting for completion less expensievely, maybe use _modin_frame.materialize()?
         result.to_pandas()  # blocking operation
@@ -225,6 +227,8 @@ def func(df, **kw):
 
         # signaling that the partition with id==0 can be written to the file
         ray.get(signals.send.remote(0))
+        # Ensure that the metadata is syncrhonized
+        qc._modin_frame._propagate_index_objs(axis=None)
         result = qc._modin_frame._partition_mgr_cls.map_axis_partitions(
             axis=1,
             partitions=qc._modin_frame._partitions,


Test patch:
diff --git a/modin/pandas/test/test_io.py b/modin/pandas/test/test_io.py
--- a/modin/pandas/test/test_io.py
+++ b/modin/pandas/test/test_io.py
@@ -1166,6 +1166,26 @@ def test_read_csv_skiprows_corner_cases(self, skiprows):
             skiprows=skiprows,
         )
 
+    def test_to_csv_with_index(self):
+        cols = 100
+        arows = 20000
+        keyrange = 100
+        values = np.vstack(
+            [
+                np.random.choice(keyrange, size=(arows)),
+                np.random.normal(size=(cols, arows)),
+            ]
+        ).transpose()
+        modin_df = pd.DataFrame(
+            values,
+            columns=["key"] + ["avalue" + str(i) for i in range(1, 1 + cols)],
+        ).set_index("key")
+        pandas_df = pandas.DataFrame(
+            values,
+            columns=["key"] + ["avalue" + str(i) for i in range(1, 1 + cols)],
+        ).set_index("key")
+        eval_to_file(modin_df, pandas_df, "to_csv", "csv")
+
 
 class TestTable:
     def test_read_table(self, make_csv_file):