Repository: modin-project/modin

Issue:
New API: `swaplevel` for Series and DataFrame

### Describe the problem
<!-- Describe the problem clearly here. -->
`swaplevel` only affects the index, so implementing it for `Series` and `DataFrame` should be trivial.



Discussion:

Resolving patch:
diff --git a/modin/pandas/base.py b/modin/pandas/base.py
--- a/modin/pandas/base.py
+++ b/modin/pandas/base.py
@@ -2809,7 +2809,9 @@ def swapaxes(self, axis1, axis2, copy=True):
         return self
 
     def swaplevel(self, i=-2, j=-1, axis=0):
-        return self._default_to_pandas("swaplevel", i=i, j=j, axis=axis)
+        axis = self._get_axis_number(axis)
+        idx = self.index if axis == 0 else self.columns
+        return self.set_axis(idx.swaplevel(i, j), axis=axis, inplace=False)
 
     def take(self, indices, axis=0, is_copy=True, **kwargs):
         return self._default_to_pandas(


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
@@ -2548,7 +2548,9 @@ def test_swapaxes(self, data, axis1, axis2):
             df_equals(modin_result, pandas_result)
 
     def test_swaplevel(self):
-        df = pd.DataFrame(
+        data = np.random.randint(1, 100, 12)
+        modin_df = pd.DataFrame(
+            data,
             index=pd.MultiIndex.from_tuples(
                 [
                     (num, letter, color)
@@ -2557,11 +2559,26 @@ def test_swaplevel(self):
                     for color in ["Red", "Green"]
                 ],
                 names=["Number", "Letter", "Color"],
-            )
+            ),
         )
-        df["Value"] = np.random.randint(1, 100, len(df))
-        with pytest.warns(UserWarning):
-            df.swaplevel("Number", "Color")
+        pandas_df = pandas.DataFrame(
+            data,
+            index=pandas.MultiIndex.from_tuples(
+                [
+                    (num, letter, color)
+                    for num in range(1, 3)
+                    for letter in ["a", "b", "c"]
+                    for color in ["Red", "Green"]
+                ],
+                names=["Number", "Letter", "Color"],
+            ),
+        )
+        df_equals(
+            modin_df.swaplevel("Number", "Color"),
+            pandas_df.swaplevel("Number", "Color"),
+        )
+        df_equals(modin_df.swaplevel(), pandas_df.swaplevel())
+        df_equals(modin_df.swaplevel(0, 1), pandas_df.swaplevel(0, 1))
 
     def test_take(self):
         df = pd.DataFrame(
diff --git a/modin/pandas/test/test_series.py b/modin/pandas/test/test_series.py
--- a/modin/pandas/test/test_series.py
+++ b/modin/pandas/test/test_series.py
@@ -2474,8 +2474,9 @@ def test_swapaxes(data, axis1, axis2):
 
 
 def test_swaplevel():
-    s = pd.Series(
-        np.random.randint(1, 100, 12),
+    data = np.random.randint(1, 100, 12)
+    modin_s = pd.Series(
+        data,
         index=pd.MultiIndex.from_tuples(
             [
                 (num, letter, color)
@@ -2486,8 +2487,23 @@ def test_swaplevel():
             names=["Number", "Letter", "Color"],
         ),
     )
-    with pytest.warns(UserWarning):
-        s.swaplevel("Number", "Color")
+    pandas_s = pandas.Series(
+        data,
+        index=pandas.MultiIndex.from_tuples(
+            [
+                (num, letter, color)
+                for num in range(1, 3)
+                for letter in ["a", "b", "c"]
+                for color in ["Red", "Green"]
+            ],
+            names=["Number", "Letter", "Color"],
+        ),
+    )
+    df_equals(
+        modin_s.swaplevel("Number", "Color"), pandas_s.swaplevel("Number", "Color")
+    )
+    df_equals(modin_s.swaplevel(), pandas_s.swaplevel())
+    df_equals(modin_s.swaplevel(1, 0), pandas_s.swaplevel(1, 0))
 
 
 @pytest.mark.parametrize("data", test_data_values, ids=test_data_keys)