diff --git a/bigframes/core/indexes/base.py b/bigframes/core/indexes/base.py index 2a35ab6546..c5e2657629 100644 --- a/bigframes/core/indexes/base.py +++ b/bigframes/core/indexes/base.py @@ -740,6 +740,9 @@ def to_numpy(self, dtype=None, *, allow_large_results=None, **kwargs) -> np.ndar __array__ = to_numpy + def to_list(self, *, allow_large_results: Optional[bool] = None) -> list: + return self.to_pandas(allow_large_results=allow_large_results).to_list() + 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..b875d56e7a 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={"my_index": [1, 2, 3]}, + ).set_index("my_index") + bf_index = df.index + assert bf_index.to_list() == pd_index.to_list()