Skip to content

Commit 99ca0df

Browse files
authored
fix: make explode respect the index labels (#1064)
1 parent 45b672a commit 99ca0df

File tree

3 files changed

+42
-2
lines changed

3 files changed

+42
-2
lines changed

bigframes/core/blocks.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1390,7 +1390,7 @@ def explode(
13901390
expr,
13911391
column_labels=self.column_labels,
13921392
index_columns=self.index_columns,
1393-
index_labels=self.column_labels.names,
1393+
index_labels=self._index_labels,
13941394
)
13951395

13961396
def _standard_stats(self, column_id) -> typing.Sequence[agg_ops.UnaryAggregateOp]:

tests/system/small/test_multiindex.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1178,7 +1178,7 @@ def test_column_multi_index_dot_not_supported():
11781178
bf1 @ bf2
11791179

11801180

1181-
def test_explode_w_multi_index():
1181+
def test_explode_w_column_multi_index():
11821182
data = [[[1, 1], np.nan, [3, 3]], [[2], [5], []]]
11831183
multi_level_columns = pandas.MultiIndex.from_arrays(
11841184
[["col0", "col0", "col1"], ["col00", "col01", "col11"]]
@@ -1197,6 +1197,24 @@ def test_explode_w_multi_index():
11971197
)
11981198

11991199

1200+
def test_explode_w_multi_index():
1201+
data = [[[1, 1], np.nan, [3, 3]], [[2], [5], []]]
1202+
columns = ["col00", "col01", "col11"]
1203+
multi_index = pandas.MultiIndex.from_frame(
1204+
pandas.DataFrame({"idx0": [5, 1], "idx1": ["z", "x"]})
1205+
)
1206+
1207+
df = bpd.DataFrame(data, index=multi_index, columns=columns)
1208+
pd_df = df.to_pandas()
1209+
1210+
pandas.testing.assert_frame_equal(
1211+
df.explode("col00").to_pandas(),
1212+
pd_df.explode("col00"),
1213+
check_dtype=False,
1214+
check_index_type=False,
1215+
)
1216+
1217+
12001218
def test_column_multi_index_w_na_stack(scalars_df_index, scalars_pandas_df_index):
12011219
columns = ["int64_too", "int64_col", "rowindex_2"]
12021220
level1 = pandas.Index(["b", "c", "d"])

tests/system/small/test_series.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3852,6 +3852,28 @@ def test_series_explode(data):
38523852
pytest.param([5, 1, 3, 2], False, id="ignore_unordered_index"),
38533853
pytest.param(["z", "x", "a", "b"], True, id="str_index"),
38543854
pytest.param(["z", "x", "a", "b"], False, id="ignore_str_index"),
3855+
pytest.param(
3856+
pd.Index(["z", "x", "a", "b"], name="idx"), True, id="str_named_index"
3857+
),
3858+
pytest.param(
3859+
pd.Index(["z", "x", "a", "b"], name="idx"),
3860+
False,
3861+
id="ignore_str_named_index",
3862+
),
3863+
pytest.param(
3864+
pd.MultiIndex.from_frame(
3865+
pd.DataFrame({"idx0": [5, 1, 3, 2], "idx1": ["z", "x", "a", "b"]})
3866+
),
3867+
True,
3868+
id="multi_index",
3869+
),
3870+
pytest.param(
3871+
pd.MultiIndex.from_frame(
3872+
pd.DataFrame({"idx0": [5, 1, 3, 2], "idx1": ["z", "x", "a", "b"]})
3873+
),
3874+
False,
3875+
id="ignore_multi_index",
3876+
),
38553877
],
38563878
)
38573879
def test_series_explode_w_index(index, ignore_index):

0 commit comments

Comments
 (0)