Skip to content

fix: DF.drop tuple input as multi-index #301

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
Jan 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 25 additions & 6 deletions bigframes/dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -1062,12 +1062,31 @@ def drop(
level_id = self._resolve_levels(level or 0)[0]

if utils.is_list_like(index):
block, inverse_condition_id = block.apply_unary_op(
level_id, ops.IsInOp(index, match_nulls=True)
)
block, condition_id = block.apply_unary_op(
inverse_condition_id, ops.invert_op
)
# Only tuple is treated as multi-index value combinations
if isinstance(index, tuple):
if level is not None:
raise ValueError("Multi-index tuple can't specify level.")
condition_id = None
for i, idx in enumerate(index):
level_id = self._resolve_levels(i)[0]
block, condition_id_cur = block.apply_unary_op(
level_id, ops.partial_right(ops.ne_op, idx)
)
if condition_id:
block, condition_id = block.apply_binary_op(
condition_id, condition_id_cur, ops.or_op
)
else:
condition_id = condition_id_cur

condition_id = typing.cast(str, condition_id)
else:
block, inverse_condition_id = block.apply_unary_op(
level_id, ops.IsInOp(index, match_nulls=True)
)
block, condition_id = block.apply_unary_op(
inverse_condition_id, ops.invert_op
)
elif isinstance(index, indexes.Index):
return self._drop_by_index(index)
else:
Expand Down
1 change: 1 addition & 0 deletions tests/system/small/test_multiindex.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,7 @@ def test_series_multi_index_droplevel(scalars_df_index, scalars_pandas_df_index,
(1, 0),
([0, 1], 0),
([True, None], 1),
((0, True), None),
],
)
def test_multi_index_drop(scalars_df_index, scalars_pandas_df_index, labels, level):
Expand Down
2 changes: 1 addition & 1 deletion third_party/bigframes_vendored/pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -1107,7 +1107,7 @@ def drop(

Args:
labels:
Index or column labels to drop.
Index or column labels to drop. A tuple will be used as a single label and not treated as a list-like.
axis:
Whether to drop labels from the index (0 or 'index') or
columns (1 or 'columns').
Expand Down