Skip to content

Commit 5ac217d

Browse files
arwas11chelsea-lin
andauthored
fix: fix generic error message when entering an incorrect column name (#1031)
* fix: fix generic error message when entering incorrect column name * fix varibale initialization * Add a test case * Update code and fix failling tests * Add test case for multiple columns --------- Co-authored-by: Chelsea Lin <[email protected]>
1 parent 3988eda commit 5ac217d

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

bigframes/core/groupby/__init__.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,12 @@ def __getitem__(
8888
keys = list(key)
8989
else:
9090
keys = [key]
91+
92+
bad_keys = [key for key in keys if key not in self._block.column_labels]
93+
94+
if len(bad_keys) > 0:
95+
raise KeyError(f"Columns not found: {str(bad_keys)[1:-1]}")
96+
9197
columns = [
9298
col_id for col_id, label in self._col_id_labels.items() if label in keys
9399
]

tests/system/small/test_groupby.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,34 @@ def test_dataframe_groupby_getitem(
421421
pd.testing.assert_series_equal(pd_result, bf_result, check_dtype=False)
422422

423423

424+
def test_dataframe_groupby_getitem_error(
425+
scalars_df_index,
426+
scalars_pandas_df_index,
427+
):
428+
col_names = ["float64_col", "int64_col", "bool_col", "string_col"]
429+
with pytest.raises(KeyError, match="\"Columns not found: 'not_in_group'\""):
430+
(
431+
scalars_df_index[col_names]
432+
.groupby("string_col")["not_in_group"]
433+
.min()
434+
.to_pandas()
435+
)
436+
437+
438+
def test_dataframe_groupby_getitem_multiple_columns_error(
439+
scalars_df_index,
440+
scalars_pandas_df_index,
441+
):
442+
col_names = ["float64_col", "int64_col", "bool_col", "string_col"]
443+
with pytest.raises(KeyError, match="\"Columns not found: 'col1', 'col2'\""):
444+
(
445+
scalars_df_index[col_names]
446+
.groupby("string_col")["col1", "col2"]
447+
.min()
448+
.to_pandas()
449+
)
450+
451+
424452
def test_dataframe_groupby_getitem_list(
425453
scalars_df_index,
426454
scalars_pandas_df_index,

0 commit comments

Comments
 (0)