From b915c4a0ba4c8bee2f991f9290c33a9a85c913b7 Mon Sep 17 00:00:00 2001 From: HyukjinKwon Date: Mon, 13 Jan 2020 19:32:45 +0900 Subject: [PATCH] Fix column name as a tuple in multi column index --- databricks/koalas/internal.py | 7 ++++--- databricks/koalas/tests/test_indexes.py | 7 +++++++ 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/databricks/koalas/internal.py b/databricks/koalas/internal.py index 79b2970e82..8e256676c9 100644 --- a/databricks/koalas/internal.py +++ b/databricks/koalas/internal.py @@ -742,9 +742,10 @@ def from_pandas(pdf: pd.DataFrame) -> '_InternalFrame': index_map = [(SPARK_INDEX_NAME_FORMAT(i), None) for i in range(len(index.levels))] else: - index_map = [(SPARK_INDEX_NAME_FORMAT(i) if name is None else name, - name if name is None or isinstance(name, tuple) else (name,)) - for i, name in enumerate(index.names)] + index_map = [ + (SPARK_INDEX_NAME_FORMAT(i) if name is None else name_like_string(name), + name if name is None or isinstance(name, tuple) else (name,)) + for i, name in enumerate(index.names)] else: name = index.name index_map = [(name_like_string(name) diff --git a/databricks/koalas/tests/test_indexes.py b/databricks/koalas/tests/test_indexes.py index 426a3adb80..4529b47137 100644 --- a/databricks/koalas/tests/test_indexes.py +++ b/databricks/koalas/tests/test_indexes.py @@ -517,6 +517,13 @@ def test_multiindex_from_product(self): self.assert_eq(pidx, kidx) + def test_multiindex_tuple_column_name(self): + column_index = pd.MultiIndex.from_tuples([('a', 'x'), ('a', 'y'), ('b', 'z')]) + pdf = pd.DataFrame([[1, 2, 3], [4, 5, 6], [7, 8, 9]], columns=column_index) + pdf.set_index(('a', 'x'), append=True, inplace=True) + kdf = ks.from_pandas(pdf) + self.assert_eq(pdf, kdf) + def test_len(self): pidx = pd.Index(range(10000)) kidx = ks.Index(range(10000))