Skip to content

Commit 768a757

Browse files
authored
feat: add support for df.loc[list, column(s)] (#1761)
1 parent 68d5e2c commit 768a757

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

bigframes/core/indexers.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,8 +155,8 @@ def __getitem__(self, key):
155155
# row key. We must choose one, so bias towards treating as multi-part row label
156156
if isinstance(key, tuple) and len(key) == 2:
157157
is_row_multi_index = self._dataframe.index.nlevels > 1
158-
is_first_item_tuple = isinstance(key[0], tuple)
159-
if not is_row_multi_index or is_first_item_tuple:
158+
is_first_item_list_or_tuple = isinstance(key[0], (tuple, list))
159+
if not is_row_multi_index or is_first_item_list_or_tuple:
160160
df = typing.cast(
161161
bigframes.dataframe.DataFrame,
162162
_loc_getitem_series_or_dataframe(self._dataframe, key[0]),

tests/system/small/test_dataframe.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3638,9 +3638,7 @@ def test_iat_errors(scalars_df_index, scalars_pandas_df_index, index, error):
36383638
scalars_df_index.iat[index]
36393639

36403640

3641-
def test_iloc_single_integer_out_of_bound_error(
3642-
scalars_df_index, scalars_pandas_df_index
3643-
):
3641+
def test_iloc_single_integer_out_of_bound_error(scalars_df_index):
36443642
with pytest.raises(IndexError, match="single positional indexer is out-of-bounds"):
36453643
scalars_df_index.iloc[99]
36463644

@@ -3655,6 +3653,17 @@ def test_loc_bool_series(scalars_df_index, scalars_pandas_df_index):
36553653
)
36563654

36573655

3656+
def test_loc_list_select_rows_and_columns(scalars_df_index, scalars_pandas_df_index):
3657+
idx_list = [0, 3, 5]
3658+
bf_result = scalars_df_index.loc[idx_list, ["bool_col", "int64_col"]].to_pandas()
3659+
pd_result = scalars_pandas_df_index.loc[idx_list, ["bool_col", "int64_col"]]
3660+
3661+
pd.testing.assert_frame_equal(
3662+
bf_result,
3663+
pd_result,
3664+
)
3665+
3666+
36583667
def test_loc_select_column(scalars_df_index, scalars_pandas_df_index):
36593668
bf_result = scalars_df_index.loc[:, "int64_col"].to_pandas()
36603669
pd_result = scalars_pandas_df_index.loc[:, "int64_col"]

0 commit comments

Comments
 (0)