Skip to content

fix: fix df/series.iloc by list with multiindex #79

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 2 commits into from
Oct 3, 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
15 changes: 8 additions & 7 deletions bigframes/core/indexers.py
Original file line number Diff line number Diff line change
Expand Up @@ -332,8 +332,6 @@ def _iloc_getitem_series_or_dataframe(
elif isinstance(key, slice):
return series_or_dataframe._slice(key.start, key.stop, key.step)
elif pd.api.types.is_list_like(key):
# TODO(henryjsolberg): support MultiIndex

if len(key) == 0:
return typing.cast(
typing.Union[bigframes.dataframe.DataFrame, bigframes.series.Series],
Expand All @@ -346,15 +344,18 @@ def _iloc_getitem_series_or_dataframe(
original_series_name if original_series_name is not None else "0"
)
df = series_or_dataframe.to_frame()
original_index_name = df.index.name
temporary_index_name = guid.generate_guid(prefix="temp_iloc_index_")
df = df.rename_axis(temporary_index_name)
original_index_names = df.index.names
temporary_index_names = [
guid.generate_guid(prefix="temp_iloc_index_")
for _ in range(len(df.index.names))
]
df = df.rename_axis(temporary_index_names)

# set to offset index and use regular loc, then restore index
df = df.reset_index(drop=False)
result = df.loc[key]
result = result.set_index(temporary_index_name)
result = result.rename_axis(original_index_name)
result = result.set_index(temporary_index_names)
result = result.rename_axis(original_index_names)

if isinstance(series_or_dataframe, bigframes.series.Series):
result = result[series_name]
Expand Down
18 changes: 18 additions & 0 deletions tests/system/small/test_dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -2525,6 +2525,24 @@ def test_iloc_list(scalars_df_index, scalars_pandas_df_index):
)


def test_iloc_list_multiindex(scalars_dfs):
scalars_df, scalars_pandas_df = scalars_dfs
scalars_df = scalars_df.copy()
scalars_pandas_df = scalars_pandas_df.copy()
scalars_df = scalars_df.set_index(["bytes_col", "numeric_col"])
scalars_pandas_df = scalars_pandas_df.set_index(["bytes_col", "numeric_col"])

index_list = [0, 0, 0, 5, 4, 7]

bf_result = scalars_df.iloc[index_list]
pd_result = scalars_pandas_df.iloc[index_list]

pd.testing.assert_frame_equal(
bf_result.to_pandas(),
pd_result,
)


def test_iloc_empty_list(scalars_df_index, scalars_pandas_df_index):
index_list = []

Expand Down