From ce3894c69dce8bebf4bff4032a3f3db1c5c7b5b9 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Mon, 22 Sep 2025 17:52:15 +0000 Subject: [PATCH 1/3] feat: implement Index.to_list() This commit implements the `Index.to_list()` method, which is an alias for `tolist()`. This new method provides a way to convert a BigQuery DataFrames Index to a Python list, similar to the existing `Series.to_list()` method. The implementation follows the pattern of other methods in the library by first converting the Index to a pandas Index using `to_pandas()` and then calling the corresponding `.to_list()` method. A unit test has been added to verify the functionality of the new method. --- bigframes/core/indexes/base.py | 5 +++++ tests/system/small/test_index.py | 6 ++++++ tests/unit/test_index.py | 11 +++++++++++ 3 files changed, 22 insertions(+) diff --git a/bigframes/core/indexes/base.py b/bigframes/core/indexes/base.py index 2a35ab6546..2ed4cb30ae 100644 --- a/bigframes/core/indexes/base.py +++ b/bigframes/core/indexes/base.py @@ -740,6 +740,11 @@ def to_numpy(self, dtype=None, *, allow_large_results=None, **kwargs) -> np.ndar __array__ = to_numpy + def tolist(self, *, allow_large_results: Optional[bool] = None) -> list: + return self.to_pandas(allow_large_results=allow_large_results).to_list() + + to_list = tolist + def __len__(self): return self.shape[0] diff --git a/tests/system/small/test_index.py b/tests/system/small/test_index.py index a82bdf7635..90986c989a 100644 --- a/tests/system/small/test_index.py +++ b/tests/system/small/test_index.py @@ -638,6 +638,12 @@ def test_index_item_with_empty(session): bf_idx_empty.item() +def test_index_to_list(scalars_df_index, scalars_pandas_df_index): + bf_result = scalars_df_index.index.to_list() + pd_result = scalars_pandas_df_index.index.to_list() + assert bf_result == pd_result + + @pytest.mark.parametrize( ("key", "value"), [ diff --git a/tests/unit/test_index.py b/tests/unit/test_index.py index 97f1e4419e..0c481832c0 100644 --- a/tests/unit/test_index.py +++ b/tests/unit/test_index.py @@ -12,6 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. +import pandas as pd import pytest from bigframes.testing import mocks @@ -38,3 +39,13 @@ def test_index_rename_inplace_returns_none(monkeypatch: pytest.MonkeyPatch): # Make sure the linked DataFrame is updated, too. assert dataframe.index.name == "my_index_name" assert index.name == "my_index_name" + + +def test_index_to_list(monkeypatch: pytest.MonkeyPatch): + pd_index = pd.Index([1, 2, 3], name="my_index") + df = mocks.create_dataframe( + monkeypatch, + data=pd.DataFrame({"my_index": [1, 2, 3]}).set_index("my_index"), + ) + bf_index = df.index + assert bf_index.to_list() == pd_index.to_list() From ac45c911d2f8ade57998e766d2c441bfc172e6dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim=20Swe=C3=B1a=20=28Swast=29?= Date: Mon, 22 Sep 2025 12:54:07 -0500 Subject: [PATCH 2/3] Update bigframes/core/indexes/base.py --- bigframes/core/indexes/base.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/bigframes/core/indexes/base.py b/bigframes/core/indexes/base.py index 2ed4cb30ae..c5e2657629 100644 --- a/bigframes/core/indexes/base.py +++ b/bigframes/core/indexes/base.py @@ -740,11 +740,9 @@ def to_numpy(self, dtype=None, *, allow_large_results=None, **kwargs) -> np.ndar __array__ = to_numpy - def tolist(self, *, allow_large_results: Optional[bool] = None) -> list: + def to_list(self, *, allow_large_results: Optional[bool] = None) -> list: return self.to_pandas(allow_large_results=allow_large_results).to_list() - to_list = tolist - def __len__(self): return self.shape[0] From fb55752007c82154ea28de65ca56713b8404fa0d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim=20Swe=C3=B1a=20=28Swast=29?= Date: Mon, 22 Sep 2025 12:58:37 -0500 Subject: [PATCH 3/3] Update tests/unit/test_index.py --- tests/unit/test_index.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/unit/test_index.py b/tests/unit/test_index.py index 0c481832c0..b875d56e7a 100644 --- a/tests/unit/test_index.py +++ b/tests/unit/test_index.py @@ -45,7 +45,7 @@ def test_index_to_list(monkeypatch: pytest.MonkeyPatch): pd_index = pd.Index([1, 2, 3], name="my_index") df = mocks.create_dataframe( monkeypatch, - data=pd.DataFrame({"my_index": [1, 2, 3]}).set_index("my_index"), - ) + data={"my_index": [1, 2, 3]}, + ).set_index("my_index") bf_index = df.index assert bf_index.to_list() == pd_index.to_list()