Skip to content

Commit f95000d

Browse files
authored
fix: for reset_index on unnamed multiindex, always use level_[n] label (#182)
Thank you for opening a Pull Request! Before submitting your PR, there are a few things you can do to make sure it goes smoothly: - [ ] Make sure to open an issue as a [bug/issue](https://togithub.com/googleapis/python-bigquery-dataframes/issues/new/choose) before writing your code! That way we can discuss the change, evaluate designs, and agree on the general idea - [ ] Ensure the tests and linter pass - [ ] Code coverage does not decrease (if any source code was changed) - [ ] Appropriate docs were updated (if necessary) Fixes #<issue_number_goes_here> 🦕
1 parent 800d44e commit f95000d

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

bigframes/core/blocks.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ def reset_index(self, drop: bool = True) -> Block:
282282
column_labels_modified = self.column_labels
283283
for level, label in enumerate(index_labels):
284284
if label is None:
285-
if "index" not in self.column_labels:
285+
if "index" not in self.column_labels and len(index_labels) <= 1:
286286
label = "index"
287287
else:
288288
label = f"level_{level}"

tests/system/small/test_dataframe.py

+22
Original file line numberDiff line numberDiff line change
@@ -1235,6 +1235,28 @@ def test_reset_index_with_unnamed_index(
12351235
pandas.testing.assert_frame_equal(bf_result, pd_result)
12361236

12371237

1238+
def test_reset_index_with_unnamed_multiindex(
1239+
scalars_df_index,
1240+
scalars_pandas_df_index,
1241+
):
1242+
bf_df = dataframe.DataFrame(
1243+
([1, 2, 3], [2, 5, 7]),
1244+
index=pd.MultiIndex.from_tuples([("a", "aa"), ("a", "aa")]),
1245+
)
1246+
pd_df = pd.DataFrame(
1247+
([1, 2, 3], [2, 5, 7]),
1248+
index=pd.MultiIndex.from_tuples([("a", "aa"), ("a", "aa")]),
1249+
)
1250+
1251+
bf_df = bf_df.reset_index()
1252+
pd_df = pd_df.reset_index()
1253+
1254+
assert pd_df.columns[0] == "level_0"
1255+
assert bf_df.columns[0] == "level_0"
1256+
assert pd_df.columns[1] == "level_1"
1257+
assert bf_df.columns[1] == "level_1"
1258+
1259+
12381260
def test_reset_index_with_unnamed_index_and_index_column(
12391261
scalars_df_index,
12401262
scalars_pandas_df_index,

0 commit comments

Comments
 (0)