Skip to content

Commit

Permalink
Clean up state variables in MultiIndex (#16203)
Browse files Browse the repository at this point in the history
MultiIndex sets it's own state variables outside of `__init__` and allows some uninitialized private variables that may be called in other methods. This PR now ensures these state variables are always initialized in `__init__`, `_from_data` and `_simple_new`

Authors:
  - Matthew Roeschke (https://github.com/mroeschke)
  - GALI PREM SAGAR (https://github.com/galipremsagar)

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

URL: #16203
  • Loading branch information
mroeschke authored Jul 12, 2024
1 parent 99ad73d commit 390e6fe
Show file tree
Hide file tree
Showing 4 changed files with 152 additions and 173 deletions.
22 changes: 9 additions & 13 deletions python/cudf/cudf/core/dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -3593,15 +3593,15 @@ def rename(

if level is not None and isinstance(self.index, MultiIndex):
level = self.index._get_level_label(level)
out_index = self.index.copy(deep=copy)
level_values = out_index.get_level_values(level)
level_values.to_frame().replace(
level_values = self.index.get_level_values(level)
ca = self.index._data.copy(deep=copy)
ca[level] = level_values._column.find_and_replace(
to_replace=list(index.keys()),
value=list(index.values()),
inplace=True,
replacement=list(index.values()),
)
out_index = type(self.index)._from_data(
ca, name=self.index.name
)
out_index._data[level] = column.as_column(level_values)
out_index._compute_levels_and_codes()
else:
to_replace = list(index.keys())
vals = list(index.values())
Expand Down Expand Up @@ -7058,12 +7058,8 @@ def stack(self, level=-1, dropna=no_default, future_stack=False):
# Assemble the final index
new_index_columns = [*repeated_index._columns, *tiled_index]
index_names = [*self.index.names, *unique_named_levels.names]
new_index = MultiIndex.from_frame(
DataFrame._from_data(
dict(zip(range(0, len(new_index_columns)), new_index_columns))
),
names=index_names,
)
new_index = MultiIndex._from_data(dict(enumerate(new_index_columns)))
new_index.names = index_names

# Compute the column indices that serves as the input for
# `interleave_columns`
Expand Down
Loading

0 comments on commit 390e6fe

Please sign in to comment.