Skip to content

feat: support index item assign in Series #1868

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 3 commits into from
Jun 27, 2025
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
5 changes: 5 additions & 0 deletions bigframes/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
26 changes: 26 additions & 0 deletions tests/system/small/test_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -499,3 +499,29 @@ 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):
"""Tests that custom Index setitem raises TypeError."""
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():
"""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"):
custom_index[2] = 999