Skip to content

feat: support dataframe.loc with conditional columns selection #233

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
Dec 11, 2023
7 changes: 6 additions & 1 deletion bigframes/core/indexers.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,12 @@ def __getitem__(self, key):
bigframes.dataframe.DataFrame,
_loc_getitem_series_or_dataframe(self._dataframe, key[0]),
)
return df[key[1]]

columns = key[1]
if isinstance(columns, pd.Series) and columns.dtype == "bool":
columns = df.columns[columns]

return df[columns]

return typing.cast(
bigframes.dataframe.DataFrame,
Expand Down
11 changes: 11 additions & 0 deletions tests/system/small/test_dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -2474,6 +2474,17 @@ def test_loc_select_column(scalars_df_index, scalars_pandas_df_index):
)


def test_loc_select_with_column_condition(scalars_df_index, scalars_pandas_df_index):
bf_result = scalars_df_index.loc[:, scalars_df_index.dtypes == "Int64"].to_pandas()
pd_result = scalars_pandas_df_index.loc[
:, scalars_pandas_df_index.dtypes == "Int64"
]
pd.testing.assert_frame_equal(
bf_result,
pd_result,
)


def test_loc_single_index_with_duplicate(scalars_df_index, scalars_pandas_df_index):
scalars_df_index = scalars_df_index.set_index("string_col", drop=False)
scalars_pandas_df_index = scalars_pandas_df_index.set_index(
Expand Down