Skip to content

feat: support item assignment in series #1859

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 2 commits into from
Jun 26, 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
4 changes: 4 additions & 0 deletions bigframes/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -1598,6 +1598,10 @@ def __getattr__(self, key: str):
else:
raise AttributeError(key)

def __setitem__(self, key, value) -> None:
"""Set item using direct assignment, delegating to .loc indexer."""
self.loc[key] = value

def _apply_aggregation(
self, op: agg_ops.UnaryAggregateOp | agg_ops.NullaryAggregateOp
) -> Any:
Expand Down
63 changes: 63 additions & 0 deletions tests/system/small/test_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -510,6 +510,69 @@ def test_series___getitem___with_default_index(scalars_dfs):
assert bf_result == pd_result


@pytest.mark.parametrize(
("index_col", "key", "value"),
(
("int64_too", 2, "new_string_value"),
("string_col", "Hello, World!", "updated_value"),
("int64_too", 0, None),
),
)
def test_series___setitem__(scalars_dfs, index_col, key, value):
col_name = "string_col"
scalars_df, scalars_pandas_df = scalars_dfs
scalars_df = scalars_df.set_index(index_col, drop=False)
scalars_pandas_df = scalars_pandas_df.set_index(index_col, drop=False)

bf_series = scalars_df[col_name]
pd_series = scalars_pandas_df[col_name].copy()

bf_series[key] = value
pd_series[key] = value

pd.testing.assert_series_equal(bf_series.to_pandas(), pd_series)


@pytest.mark.parametrize(
("key", "value"),
(
(0, 999),
(1, 888),
(0, None),
(-2345, 777),
),
)
def test_series___setitem___with_int_key_numeric(scalars_dfs, key, value):
col_name = "int64_col"
index_col = "int64_too"
scalars_df, scalars_pandas_df = scalars_dfs
scalars_df = scalars_df.set_index(index_col, drop=False)
scalars_pandas_df = scalars_pandas_df.set_index(index_col, drop=False)

bf_series = scalars_df[col_name]
pd_series = scalars_pandas_df[col_name].copy()

bf_series[key] = value
pd_series[key] = value

pd.testing.assert_series_equal(bf_series.to_pandas(), pd_series)


def test_series___setitem___with_default_index(scalars_dfs):
col_name = "float64_col"
key = 2
value = 123.456
scalars_df, scalars_pandas_df = scalars_dfs

bf_series = scalars_df[col_name]
pd_series = scalars_pandas_df[col_name].copy()

bf_series[key] = value
pd_series[key] = value

assert bf_series.to_pandas().iloc[key] == pd_series.iloc[key]


@pytest.mark.parametrize(
("col_name",),
(
Expand Down