Skip to content
This repository was archived by the owner on May 7, 2026. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
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.
  • Loading branch information
google-labs-jules[bot] committed Sep 22, 2025
commit ce3894c69dce8bebf4bff4032a3f3db1c5c7b5b9
5 changes: 5 additions & 0 deletions bigframes/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Comment thread
tswast marked this conversation as resolved.
Outdated

def __len__(self):
return self.shape[0]

Expand Down
6 changes: 6 additions & 0 deletions tests/system/small/test_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
[
Expand Down
11 changes: 11 additions & 0 deletions tests/unit/test_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"),
)
Comment thread
tswast marked this conversation as resolved.
Outdated
bf_index = df.index
assert bf_index.to_list() == pd_index.to_list()
Loading