Skip to content

Commit c5d251a

Browse files
authored
feat: support index item assign in Series (#1868)
* implement index item assignment * add testcase * final touch up
1 parent 942e66c commit c5d251a

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

‎bigframes/core/indexes/base.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,11 @@ def dtypes(self) -> pandas.Series:
174174
index=typing.cast(typing.Tuple, self._block.index.names),
175175
)
176176

177+
def __setitem__(self, key, value) -> None:
178+
"""Index objects are immutable. Use Index constructor to create
179+
modified Index."""
180+
raise TypeError("Index does not support mutable operations")
181+
177182
@property
178183
def size(self) -> int:
179184
return self.shape[0]

‎tests/system/small/test_index.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -499,3 +499,29 @@ def test_index_item_with_empty(session):
499499

500500
with pytest.raises(ValueError, match=re.escape(expected_message)):
501501
bf_idx_empty.item()
502+
503+
504+
@pytest.mark.parametrize(
505+
("key", "value"),
506+
[
507+
(0, "string_value"),
508+
(1, 42),
509+
("label", None),
510+
(-1, 3.14),
511+
],
512+
)
513+
def test_index_setitem_different_types(scalars_dfs, key, value):
514+
"""Tests that custom Index setitem raises TypeError."""
515+
scalars_df, _ = scalars_dfs
516+
index = scalars_df.index
517+
518+
with pytest.raises(TypeError, match="Index does not support mutable operations"):
519+
index[key] = value
520+
521+
522+
def test_custom_index_setitem_error():
523+
"""Tests that custom Index setitem raises TypeError."""
524+
custom_index = bpd.Index([1, 2, 3, 4, 5], name="custom")
525+
526+
with pytest.raises(TypeError, match="Index does not support mutable operations"):
527+
custom_index[2] = 999

0 commit comments

Comments
 (0)