Skip to content

fix: for reset_index on unnamed multiindex, always use level_[n] label #182

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Nov 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion bigframes/core/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ def reset_index(self, drop: bool = True) -> Block:
column_labels_modified = self.column_labels
for level, label in enumerate(index_labels):
if label is None:
if "index" not in self.column_labels:
if "index" not in self.column_labels and len(index_labels) <= 1:
label = "index"
else:
label = f"level_{level}"
Expand Down
22 changes: 22 additions & 0 deletions tests/system/small/test_dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -1235,6 +1235,28 @@ def test_reset_index_with_unnamed_index(
pandas.testing.assert_frame_equal(bf_result, pd_result)


def test_reset_index_with_unnamed_multiindex(
scalars_df_index,
scalars_pandas_df_index,
):
bf_df = dataframe.DataFrame(
([1, 2, 3], [2, 5, 7]),
index=pd.MultiIndex.from_tuples([("a", "aa"), ("a", "aa")]),
)
pd_df = pd.DataFrame(
([1, 2, 3], [2, 5, 7]),
index=pd.MultiIndex.from_tuples([("a", "aa"), ("a", "aa")]),
)

bf_df = bf_df.reset_index()
pd_df = pd_df.reset_index()

assert pd_df.columns[0] == "level_0"
assert bf_df.columns[0] == "level_0"
assert pd_df.columns[1] == "level_1"
assert bf_df.columns[1] == "level_1"


def test_reset_index_with_unnamed_index_and_index_column(
scalars_df_index,
scalars_pandas_df_index,
Expand Down