Repository: databricks/koalas

Issue:
DataFrame.reindex does not support named columns index
Hi, it seems currently not possible to reindex a `DataFrame` with a named columns `Index`, the new name is missing.

```python
>>> pdf = pd.DataFrame({"values": [1, 2, 3]})
>>> pdf
   values
0       1
1       2
2       3
>>> kdf = ks.from_pandas(pdf)
>>> columns = pd.Index(["values"], name="name")
>>> kdf.reindex(columns=columns)
   values
0       1
1       2
2       3
```

But in pandas :

```python
>>> pdf.reindex(columns=columns)
name  values
0          1
1          2
2          3
```

I will submit a PR for this.


Discussion:

Resolving patch:
diff --git a/databricks/koalas/frame.py b/databricks/koalas/frame.py
--- a/databricks/koalas/frame.py
+++ b/databricks/koalas/frame.py
@@ -7987,7 +7987,17 @@ def _reindex_columns(self, columns, fill_value):
                 scols.append(F.lit(fill_value).alias(name_like_string(label)))
             labels.append(label)
 
-        return DataFrame(self._internal.with_new_columns(scols, column_labels=labels))
+        if isinstance(columns, pd.Index):
+            column_label_names = [
+                name if is_name_like_tuple(name) else (name,) for name in columns.names
+            ]
+            internal = self._internal.with_new_columns(
+                scols, column_labels=labels, column_label_names=column_label_names
+            )
+        else:
+            internal = self._internal.with_new_columns(scols, column_labels=labels)
+
+        return DataFrame(internal)
 
     def melt(self, id_vars=None, value_vars=None, var_name=None, value_name="value") -> "DataFrame":
         """


Test patch:
diff --git a/databricks/koalas/tests/test_dataframe.py b/databricks/koalas/tests/test_dataframe.py
--- a/databricks/koalas/tests/test_dataframe.py
+++ b/databricks/koalas/tests/test_dataframe.py
@@ -2923,10 +2923,19 @@ def test_drop_duplicates(self):
 
     def test_reindex(self):
         index = pd.Index(["A", "B", "C", "D", "E"])
-
-        pdf = pd.DataFrame({"numbers": [1.0, 2.0, 3.0, 4.0, None]}, index=index)
+        columns = pd.Index(["numbers"])
+        pdf = pd.DataFrame([1.0, 2.0, 3.0, 4.0, None], index=index, columns=columns)
         kdf = ks.from_pandas(pdf)
 
+        columns2 = pd.Index(["numbers", "2", "3"], name="cols2")
+        self.assert_eq(
+            pdf.reindex(columns=columns2).sort_index(), kdf.reindex(columns=columns2).sort_index(),
+        )
+
+        columns = pd.Index(["numbers"], name="cols")
+        pdf.columns = columns
+        kdf.columns = columns
+
         self.assert_eq(
             pdf.reindex(["A", "B", "C"], columns=["numbers", "2", "3"]).sort_index(),
             kdf.reindex(["A", "B", "C"], columns=["numbers", "2", "3"]).sort_index(),
@@ -2962,6 +2971,16 @@ def test_reindex(self):
             kdf.reindex(columns=["numbers", "2", "3"], fill_value=0.0).sort_index(),
         )
 
+        columns2 = pd.Index(["numbers", "2", "3"])
+        self.assert_eq(
+            pdf.reindex(columns=columns2).sort_index(), kdf.reindex(columns=columns2).sort_index(),
+        )
+
+        columns2 = pd.Index(["numbers", "2", "3"], name="cols2")
+        self.assert_eq(
+            pdf.reindex(columns=columns2).sort_index(), kdf.reindex(columns=columns2).sort_index(),
+        )
+
         # Reindexing single Index on single Index
         pindex2 = pd.Index(["A", "C", "D", "E", "0"], name="index2")
         kindex2 = ks.from_pandas(pindex2)
@@ -3024,7 +3043,7 @@ def test_reindex(self):
                 kdf.reindex(index=kindex2, fill_value=fill_value).sort_index(),
             )
 
-        columns = pd.MultiIndex.from_tuples([("X", "numbers")])
+        columns = pd.MultiIndex.from_tuples([("X", "numbers")], names=["cols1", "cols2"])
         pdf.columns = columns
         kdf.columns = columns
 
@@ -3058,6 +3077,13 @@ def test_reindex(self):
                 ).sort_index(),
             )
 
+        columns2 = pd.MultiIndex.from_tuples(
+            [("X", "numbers"), ("Y", "2"), ("Y", "3")], names=["cols3", "cols4"]
+        )
+        self.assert_eq(
+            pdf.reindex(columns=columns2).sort_index(), kdf.reindex(columns=columns2).sort_index(),
+        )
+
         self.assertRaises(TypeError, lambda: kdf.reindex(columns=["X"]))
         self.assertRaises(ValueError, lambda: kdf.reindex(columns=[("X",)]))