Skip to content

Commit

Permalink
add _index_from_columns helper
Browse files Browse the repository at this point in the history
  • Loading branch information
isVoid committed Dec 7, 2021
1 parent 0413858 commit 5b81aa3
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 8 deletions.
4 changes: 1 addition & 3 deletions python/cudf/cudf/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
7 changes: 7 additions & 0 deletions python/cudf/cudf/core/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
11 changes: 6 additions & 5 deletions python/cudf/cudf/core/indexed_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit 5b81aa3

Please sign in to comment.