Skip to content

Commit 3febea9

Browse files
Genesis929tswast
andauthored
feat: support dataframe.loc with conditional columns selection (#233)
Co-authored-by: Tim Swast <[email protected]>
1 parent 99598c7 commit 3febea9

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

bigframes/core/indexers.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,12 @@ def __getitem__(self, key):
156156
bigframes.dataframe.DataFrame,
157157
_loc_getitem_series_or_dataframe(self._dataframe, key[0]),
158158
)
159-
return df[key[1]]
159+
160+
columns = key[1]
161+
if isinstance(columns, pd.Series) and columns.dtype == "bool":
162+
columns = df.columns[columns]
163+
164+
return df[columns]
160165

161166
return typing.cast(
162167
bigframes.dataframe.DataFrame,

tests/system/small/test_dataframe.py

+11
Original file line numberDiff line numberDiff line change
@@ -2474,6 +2474,17 @@ def test_loc_select_column(scalars_df_index, scalars_pandas_df_index):
24742474
)
24752475

24762476

2477+
def test_loc_select_with_column_condition(scalars_df_index, scalars_pandas_df_index):
2478+
bf_result = scalars_df_index.loc[:, scalars_df_index.dtypes == "Int64"].to_pandas()
2479+
pd_result = scalars_pandas_df_index.loc[
2480+
:, scalars_pandas_df_index.dtypes == "Int64"
2481+
]
2482+
pd.testing.assert_frame_equal(
2483+
bf_result,
2484+
pd_result,
2485+
)
2486+
2487+
24772488
def test_loc_single_index_with_duplicate(scalars_df_index, scalars_pandas_df_index):
24782489
scalars_df_index = scalars_df_index.set_index("string_col", drop=False)
24792490
scalars_pandas_df_index = scalars_pandas_df_index.set_index(

0 commit comments

Comments
 (0)