Skip to content

Commit

Permalink
Set column names in _from_columns_like_self factory (#10400)
Browse files Browse the repository at this point in the history
A quick fix to set column names in the `_from_columns_like_self` factory. Since dataframe column names are slightly more complex than simple Frame, this fix makes sure column name metadata (such as multi level names) are properly copied to the result dataframe.

Addresses this comment: #10315 (comment)

Authors:
  - Michael Wang (https://github.com/isVoid)

Approvers:
  - GALI PREM SAGAR (https://github.com/galipremsagar)

URL: #10400
  • Loading branch information
isVoid authored Mar 10, 2022
1 parent 46ac622 commit 289a6a1
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
14 changes: 13 additions & 1 deletion python/cudf/cudf/core/dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from typing import (
Any,
Dict,
List,
MutableMapping,
Optional,
Set,
Expand Down Expand Up @@ -1319,7 +1320,6 @@ def _slice(self: T, arg: slice) -> T:

if is_range_index:
result.index = self.index[start:stop]
result._set_column_names_like(self)
return result

@_cudf_nvtx_annotate
Expand Down Expand Up @@ -6048,6 +6048,18 @@ def _sample_axis_1(

return result

def _from_columns_like_self(
self,
columns: List[ColumnBase],
column_names: Iterable[str],
index_names: Optional[List[str]] = None,
) -> DataFrame:
result = super()._from_columns_like_self(
columns, column_names, index_names
)
result._set_column_names_like(self)
return result


def from_dataframe(df, allow_copy=False):
return df_protocol.from_dataframe(df, allow_copy=allow_copy)
Expand Down
4 changes: 2 additions & 2 deletions python/cudf/cudf/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ def _from_data(
def _from_columns(
cls,
columns: List[ColumnBase],
column_names: List[str],
column_names: abc.Iterable[str],
index_names: Optional[List[str]] = None,
):
"""Construct a `Frame` object from a list of columns.
Expand Down Expand Up @@ -231,7 +231,7 @@ def _from_columns(
def _from_columns_like_self(
self,
columns: List[ColumnBase],
column_names: List[str],
column_names: abc.Iterable[str],
index_names: Optional[List[str]] = None,
):
"""Construct a `Frame` from a list of columns with metadata from self.
Expand Down

0 comments on commit 289a6a1

Please sign in to comment.