Repository: modin-project/modin

Issue:
BUG: df.set_index() cannot use Modin Series to set the index
### Modin version checks

- [X] I have checked that this issue has not already been reported.

- [X] I have confirmed this bug exists on the latest released version of Modin.

- [X] I have confirmed this bug exists on the main branch of Modin. (In order to do this you can follow [this guide](https://modin.readthedocs.io/en/stable/getting_started/installation.html#installing-from-the-github-master-branch).)


### Reproducible Example

```python
import modin.pandas as pd
d = {'col1': [1, 2, 3], 'col2': [1, 2, 3], 'col3': [1, 2, 3]}
df = pd.DataFrame(data=d)
s = pd.Series(["a", "b", "c"])
df = df.set_index(s)
```


### Issue Description

Modin DataFrame cannot receive a Modin/ pandas Series as an argument using df.set_index(). This results in `TypeError: The parameter "keys" may be a column key, one-dimensional array, or a list containing only valid column keys and one-dimensional arrays.. Received column of type <class 'modin.pandas.series.Series'>`

### Expected Behavior

<img width="531" alt="Screen Shot 2023-02-03 at 3 36 56 PM" src="https://user-images.githubusercontent.com/17665008/216730295-6c4bd02a-7d07-4ebe-8b56-7d40974d43ed.png">


### Error Logs

<details>

```python-traceback

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
~/opt/anaconda3/lib/python3.9/site-packages/pandas/core/frame.py in set_index(self, keys, drop, append, inplace, verify_integrity)
   6001                 try:
-> 6002                     found = col in self.columns
   6003                 except TypeError as err:

~/opt/anaconda3/lib/python3.9/site-packages/pandas/core/indexes/base.py in __contains__(self, key)
   5288         """
-> 5289         hash(key)
   5290         try:

TypeError: unhashable type: 'Series'

The above exception was the direct cause of the following exception:

TypeError                                 Traceback (most recent call last)
/var/folders/qj/jybppsbd2jl75s8y2q8s2xx80000gn/T/ipykernel_40368/2952750038.py in <module>
      2 df = pd.DataFrame(data=d)
      3 s = pd.Series(["a", "b", "c"])
----> 4 df = df.set_index(s)
      5 df

~/opt/anaconda3/lib/python3.9/site-packages/modin/logging/logger_decorator.py in run_and_log(*args, **kwargs)
    126             """
    127             if LogMode.get() == "disable":
--> 128                 return obj(*args, **kwargs)
    129 
    130             logger = get_logger()

~/opt/anaconda3/lib/python3.9/site-packages/modin/pandas/dataframe.py in set_index(self, keys, drop, append, inplace, verify_integrity)
   2091             # calculation so that it matches.
   2092             frame.index = (
-> 2093                 pandas.DataFrame(index=self.index)
   2094                 .set_index(keys, append=append, verify_integrity=verify_integrity)
   2095                 .index

~/opt/anaconda3/lib/python3.9/site-packages/pandas/util/_decorators.py in wrapper(*args, **kwargs)
    329                     stacklevel=find_stack_level(),
    330                 )
--> 331             return func(*args, **kwargs)
    332 
    333         # error: "Callable[[VarArg(Any), KwArg(Any)], Any]" has no

~/opt/anaconda3/lib/python3.9/site-packages/pandas/core/frame.py in set_index(self, keys, drop, append, inplace, verify_integrity)
   6002                     found = col in self.columns
   6003                 except TypeError as err:
-> 6004                     raise TypeError(
   6005                         f"{err_msg}. Received column of type {type(col)}"
   6006                     ) from err

TypeError: The parameter "keys" may be a column key, one-dimensional array, or a list containing only valid column keys and one-dimensional arrays.. Received column of type <class 'modin.pandas.series.Series'>

```

</details>


### Installed Versions

<details>

INSTALLED VERSIONS
------------------
commit           : 9068fbcfd7d4c169018ae706d6c96edea6db0cd1
python           : 3.9.7.final.0
python-bits      : 64
OS               : Darwin
OS-release       : 20.6.0
Version          : Darwin Kernel Version 20.6.0: Wed Jun 23 00:26:31 PDT 2021; root:xnu-7195.141.2~5/RELEASE_X86_64
machine          : x86_64
processor        : i386
byteorder        : little
LC_ALL           : None
LANG             : en_US.UTF-8
LOCALE           : en_US.UTF-8

Modin dependencies
------------------
modin            : 0.18.1
ray              : 2.2.0
dask             : 2021.10.0
distributed      : 2021.10.0
hdk              : None

pandas dependencies
-------------------
pandas           : 1.5.3
numpy            : 1.20.3
pytz             : 2021.3
dateutil         : 2.8.2
setuptools       : 58.0.4
pip              : 21.2.4
Cython           : 0.29.24
pytest           : 6.2.4
hypothesis       : None
sphinx           : 4.2.0
blosc            : None
feather          : None
xlsxwriter       : 3.0.1
lxml.etree       : 4.6.3
html5lib         : 1.1
pymysql          : None
psycopg2         : 2.9.3
jinja2           : 2.11.3
IPython          : 7.29.0
pandas_datareader: None
bs4              : 4.10.0
bottleneck       : 1.3.2
brotli           : 
fastparquet      : None
fsspec           : 2021.08.1
gcsfs            : None
matplotlib       : 3.6.3
numba            : 0.54.1
numexpr          : 2.7.3
odfpy            : None
openpyxl         : 3.0.9
pandas_gbq       : None
pyarrow          : 6.0.1
pyreadstat       : None
pyxlsb           : None
s3fs             : None
scipy            : 1.7.1
snappy           : None
sqlalchemy       : 1.4.22
tables           : 3.6.1
tabulate         : None
xarray           : None
xlrd             : 2.0.1
xlwt             : 1.3.0
zstandard        : None
tzdata           : None

</details>



Discussion:

Resolving patch:
diff --git a/modin/pandas/dataframe.py b/modin/pandas/dataframe.py
--- a/modin/pandas/dataframe.py
+++ b/modin/pandas/dataframe.py
@@ -50,6 +50,7 @@
     to_pandas,
     hashable,
     MODIN_UNNAMED_SERIES_LABEL,
+    try_cast_to_pandas,
 )
 from modin.config import Engine, IsExperimental, PersistentPickle
 from .utils import (
@@ -2079,13 +2080,9 @@ def set_index(
                 frame = self
             else:
                 frame = self.copy()
-            if not all(
-                isinstance(col, (pandas.Index, Series, np.ndarray, list, Iterator))
-                for col in keys
-            ):
-                if drop:
-                    keys = [frame.pop(k) if not is_list_like(k) else k for k in keys]
-                keys = [k._to_pandas() if isinstance(k, Series) else k for k in keys]
+            if drop:
+                keys = [k if is_list_like(k) else frame.pop(k) for k in keys]
+            keys = try_cast_to_pandas(keys)
             # These are single-threaded objects, so we might as well let pandas do the
             # calculation so that it matches.
             frame.index = (


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
@@ -294,16 +294,42 @@ def test_indexing_duplicate_axis(data):
 
 
 @pytest.mark.parametrize("data", test_data_values, ids=test_data_keys)
-def test_set_index(data):
-    modin_df = pd.DataFrame(data)
-    pandas_df = pandas.DataFrame(data)
-
-    modin_result = modin_df.set_index([modin_df.index, modin_df.columns[0]])
-    pandas_result = pandas_df.set_index([pandas_df.index, pandas_df.columns[0]])
-    df_equals(modin_result, pandas_result)
-
-    # test for the case from https://github.com/modin-project/modin/issues/4308
-    eval_general(modin_df, pandas_df, lambda df: df.set_index("inexistent_col"))
+@pytest.mark.parametrize(
+    "key_func",
+    [
+        # test for the case from https://github.com/modin-project/modin/issues/4308
+        "non_existing_column",
+        lambda df: df.columns[0],
+        lambda df: df.index,
+        lambda df: [df.index, df.columns[0]],
+        lambda df: pandas.Series(list(range(len(df.index))))
+        if isinstance(df, pandas.DataFrame)
+        else pd.Series(list(range(len(df)))),
+    ],
+    ids=[
+        "non_existing_column",
+        "first_column_name",
+        "original_index",
+        "list_of_index_and_first_column_name",
+        "series_of_integers",
+    ],
+)
+@pytest.mark.parametrize(
+    "drop_kwargs",
+    [{"drop": True}, {"drop": False}, {}],
+    ids=["drop_True", "drop_False", "no_drop_param"],
+)
+def test_set_index(data, key_func, drop_kwargs, request):
+    if (
+        "list_of_index_and_first_column_name" in request.node.name
+        and "drop_False" in request.node.name
+    ):
+        pytest.xfail(
+            reason="KeyError: https://github.com/modin-project/modin/issues/5636"
+        )
+    eval_general(
+        *create_test_dfs(data), lambda df: df.set_index(key_func(df), **drop_kwargs)
+    )
 
 
 @pytest.mark.parametrize("index", ["a", ["a", ("b", "")]])