From 253201cdba9ab1b6be836b8a507509158ac60aa9 Mon Sep 17 00:00:00 2001 From: Shuowei Li Date: Thu, 26 Jun 2025 20:05:52 +0000 Subject: [PATCH 1/3] implement index item assignment --- bigframes/core/indexes/base.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/bigframes/core/indexes/base.py b/bigframes/core/indexes/base.py index bc8b47d216..f653b8700b 100644 --- a/bigframes/core/indexes/base.py +++ b/bigframes/core/indexes/base.py @@ -174,6 +174,11 @@ def dtypes(self) -> pandas.Series: index=typing.cast(typing.Tuple, self._block.index.names), ) + def __setitem__(self, key, value) -> None: + """Index objects are immutable. Use Index constructor to create + modified Index.""" + raise TypeError("Index does not support mutable operations") + @property def size(self) -> int: return self.shape[0] From 326b27f4ddebea1a356fd5a6381d617b19839695 Mon Sep 17 00:00:00 2001 From: Shuowei Li Date: Fri, 27 Jun 2025 17:23:12 +0000 Subject: [PATCH 2/3] add testcase --- tests/system/small/test_index.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/tests/system/small/test_index.py b/tests/system/small/test_index.py index 3b9854be26..146865144e 100644 --- a/tests/system/small/test_index.py +++ b/tests/system/small/test_index.py @@ -499,3 +499,28 @@ def test_index_item_with_empty(session): with pytest.raises(ValueError, match=re.escape(expected_message)): bf_idx_empty.item() + + +@pytest.mark.parametrize( + ("key", "value"), + [ + (0, "string_value"), + (1, 42), + ("label", None), + (-1, 3.14), + ], +) +def test_index_setitem_different_types(scalars_dfs, key, value): + scalars_df, _ = scalars_dfs + index = scalars_df.index + + with pytest.raises(TypeError, match="Index does not support mutable operations"): + index[key] = value + + +def test_custom_index_setitem_error(session): + # Create a custom index + custom_index = bpd.Index([1, 2, 3, 4, 5], name="custom") + + with pytest.raises(TypeError, match="Index does not support mutable operations"): + custom_index[2] = 999 From f4a80f6f96c0f77a78afa9a885843fdc59b2ae87 Mon Sep 17 00:00:00 2001 From: Shuowei Li Date: Fri, 27 Jun 2025 17:34:27 +0000 Subject: [PATCH 3/3] final touch up --- tests/system/small/test_index.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tests/system/small/test_index.py b/tests/system/small/test_index.py index 146865144e..c7e316a9d2 100644 --- a/tests/system/small/test_index.py +++ b/tests/system/small/test_index.py @@ -511,6 +511,7 @@ def test_index_item_with_empty(session): ], ) def test_index_setitem_different_types(scalars_dfs, key, value): + """Tests that custom Index setitem raises TypeError.""" scalars_df, _ = scalars_dfs index = scalars_df.index @@ -518,8 +519,8 @@ def test_index_setitem_different_types(scalars_dfs, key, value): index[key] = value -def test_custom_index_setitem_error(session): - # Create a custom index +def test_custom_index_setitem_error(): + """Tests that custom Index setitem raises TypeError.""" custom_index = bpd.Index([1, 2, 3, 4, 5], name="custom") with pytest.raises(TypeError, match="Index does not support mutable operations"):