From 5b81aa3b8d55f88c4d5c8252a07a5dd47f08ba8f Mon Sep 17 00:00:00 2001 From: Michael Wang Date: Tue, 7 Dec 2021 15:53:02 -0800 Subject: [PATCH] add _index_from_columns helper --- python/cudf/cudf/core/frame.py | 4 +--- python/cudf/cudf/core/index.py | 7 +++++++ python/cudf/cudf/core/indexed_frame.py | 11 ++++++----- 3 files changed, 14 insertions(+), 8 deletions(-) diff --git a/python/cudf/cudf/core/frame.py b/python/cudf/cudf/core/frame.py index d7a75cb9f40..252fe639a0b 100644 --- a/python/cudf/cudf/core/frame.py +++ b/python/cudf/cudf/core/frame.py @@ -157,9 +157,7 @@ def _from_columns( n_index_columns = 0 if index_names is not None: n_index_columns = len(index_names) - index = cudf.core.index._index_from_data( - dict(zip(range(n_index_columns), columns)) - ) + index = cudf.core.index._index_from_columns(columns) if isinstance(index, cudf.MultiIndex): index.names = index_names else: diff --git a/python/cudf/cudf/core/index.py b/python/cudf/cudf/core/index.py index 8f905ee6d49..9dedde4502a 100644 --- a/python/cudf/cudf/core/index.py +++ b/python/cudf/cudf/core/index.py @@ -114,6 +114,13 @@ def _index_from_data(data: MutableMapping, name: Any = None): return index_class_type._from_data(data, None, name) +def _index_from_columns( + columns: List[cudf.core.column.ColumnBase], name: Any = None +): + """Construct an index from ``columns``, with levels named 0, 1, 2...""" + return _index_from_data(dict(zip(range(len(columns)), columns)), name=name) + + class RangeIndex(BaseIndex): """ Immutable Index implementing a monotonic integer range. diff --git a/python/cudf/cudf/core/indexed_frame.py b/python/cudf/cudf/core/indexed_frame.py index a76b8bebb44..6ce5e9adaea 100644 --- a/python/cudf/cudf/core/indexed_frame.py +++ b/python/cudf/cudf/core/indexed_frame.py @@ -19,7 +19,11 @@ from cudf.api.types import is_categorical_dtype, is_integer_dtype, is_list_like from cudf.core.column import arange from cudf.core.frame import Frame -from cudf.core.index import Index, RangeIndex, _index_from_data +from cudf.core.index import ( + Index, + RangeIndex, + _index_from_columns, +) from cudf.core.multiindex import MultiIndex from cudf.utils.utils import _gather_map_is_valid, cached_property @@ -893,10 +897,7 @@ def _reset_index(self, level, drop, col_level=0, col_fill=""): index_names, ) = self._index._split_columns_by_levels(level) if index_columns: - index = _index_from_data( - dict(zip(range(len(index_columns)), index_columns)), - name=self._index.name, - ) + index = _index_from_columns(index_columns, name=self._index.name,) if isinstance(index, MultiIndex): index.names = index_names else: