From a9fb4c63e348bc25450dffa7ce5c63aba220f30a Mon Sep 17 00:00:00 2001 From: Shuowei Li Date: Thu, 26 Jun 2025 04:27:14 +0000 Subject: [PATCH 1/2] Add item assignment for bigframes dataframe --- bigframes/series.py | 4 ++ tests/system/small/test_series.py | 63 +++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+) diff --git a/bigframes/series.py b/bigframes/series.py index ae6cd7b2ad..ebc2913f78 100644 --- a/bigframes/series.py +++ b/bigframes/series.py @@ -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: diff --git a/tests/system/small/test_series.py b/tests/system/small/test_series.py index 6760d63a20..80802bab1c 100644 --- a/tests/system/small/test_series.py +++ b/tests/system/small/test_series.py @@ -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"), + ( + (-2, 999), + (-1, 888), + (0, None), + (1, 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",), ( From fa7e4d637f6bf2011d1000243280e53bb9be93f8 Mon Sep 17 00:00:00 2001 From: Shuowei Li Date: Thu, 26 Jun 2025 04:31:05 +0000 Subject: [PATCH 2/2] testcase update --- tests/system/small/test_series.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/system/small/test_series.py b/tests/system/small/test_series.py index 80802bab1c..d513b0e780 100644 --- a/tests/system/small/test_series.py +++ b/tests/system/small/test_series.py @@ -536,10 +536,10 @@ def test_series___setitem__(scalars_dfs, index_col, key, value): @pytest.mark.parametrize( ("key", "value"), ( - (-2, 999), - (-1, 888), + (0, 999), + (1, 888), (0, None), - (1, 777), + (-2345, 777), ), ) def test_series___setitem___with_int_key_numeric(scalars_dfs, key, value):