8000 fix: fix generic error message when entering an incorrect column name by arwas11 · Pull Request #1031 · googleapis/python-bigquery-dataframes · GitHub
[go: up one dir, main page]

Skip to content
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
6 changes: 6 additions & 0 deletions bigframes/core/groupby/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,12 @@ def __getitem__(
keys = list(key)
else:
keys = [key]

bad_keys = [key for key in keys if key not in self._block.column_labels]

if len(bad_keys) > 0:
raise KeyError(f"Columns not found: {str(bad_keys)[1:-1]}")

columns = [
col_id for col_id, label in self._col_id_labels.items() if label in keys
]
Expand Down
28 changes: 28 additions & 0 deletions tests/system/small/test_groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,34 @@ def test_dataframe_groupby_getitem(
pd.testing.assert_series_equal(pd_result, bf_result, check_dtype=False)


def test_dataframe_groupby_getitem_error(
scalars_df_index,
scalars_pandas_df_index,
):
col_names = ["float64_col", "int64_col", "bool_col", "string_col"]
with pytest.raises(KeyError, match="\"Columns not found: 'not_in_group'\""):
(
scalars_df_index[col_names]
.groupby("string_col")["not_in_group"]
.min()
.to_pandas()
)


def test_dataframe_groupby_getitem_multiple_columns_error 65D7 (
scalars_df_index,
scalars_pandas_df_index,
):
col_names = ["float64_col", "int64_col", "bool_col", "string_col"]
with pytest.raises(KeyError, match="\"Columns not found: 'col1', 'col2'\""):
(
scalars_df_index[col_names]
.groupby("string_col")["col1", "col2"]
.min()
.to_pandas()
)


def test_dataframe_groupby_getitem_list(
scalars_df_index,
scalars_pandas_df_index,
Expand Down
0