Skip to content

feat: Implement item() for Series and Index #1792

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
Open

Conversation

tswast
Copy link
Collaborator

@tswast tswast commented Jun 4, 2025

This commit introduces the item() method to both Series and Index classes.

The item() method allows you to extract the single value from a Series or Index. It calls peek(2) internally and raises a ValueError if the Series or Index does not contain exactly one element. This behavior is consistent with pandas.

Unit tests have been added to verify the functionality for:

  • Single-item Series/Index
  • Multi-item Series/Index (ValueError expected)
  • Empty Series/Index (ValueError expected)

Thank you for opening a Pull Request! Before submitting your PR, there are a few things you can do to make sure it goes smoothly:

  • Make sure to open an issue as a bug/issue before writing your code! That way we can discuss the change, evaluate designs, and agree on the general idea
  • Ensure the tests and linter pass
  • Code coverage does not decrease (if any source code was changed)
  • Appropriate docs were updated (if necessary)

Fixes internal issue b/422275127 🦕

This commit introduces the `item()` method to both `Series` and `Index` classes.

The `item()` method allows you to extract the single value from a Series or Index.
It calls `peek(2)` internally and raises a `ValueError` if the Series or Index
does not contain exactly one element. This behavior is consistent with pandas.

Unit tests have been added to verify the functionality for:
- Single-item Series/Index
- Multi-item Series/Index (ValueError expected)
- Empty Series/Index (ValueError expected)
@tswast tswast requested review from a team as code owners June 4, 2025 20:03
@tswast tswast requested a review from jialuoo June 4, 2025 20:03
@product-auto-label product-auto-label bot added the size: m Pull request size is medium. label Jun 4, 2025
@product-auto-label product-auto-label bot added the api: bigquery Issues related to the googleapis/python-bigquery-dataframes API. label Jun 4, 2025
@tswast tswast requested review from Genesis929 and removed request for jialuoo June 4, 2025 20:04
This commit moves the docstrings for the `item()` method in `Series` and `Index`
to their respective files in the `third_party/bigframes_vendored/pandas/core/`
directory.

The docstrings have been updated to match the pandas docstrings as closely as
possible, while adhering to the existing style in the BigQuery DataFrames repository.
This ensures that the BigQuery DataFrames API documentation remains consistent
with pandas where applicable.
google-labs-jules bot and others added 4 commits June 4, 2025 20:19
**Test: Update item() tests to match pandas behavior**

This commit updates the tests for `Series.item()` and `Index.item()`
to align more closely with pandas.

The changes include:
- Comparing the return value of `bigframes_series.item()` and
  `bigframes_index.item()` with their pandas counterparts.
- Asserting that the ValueError messages for multi-item and empty
  Series/Index cases are identical to those raised by pandas.
  The expected message is "can only convert an array of size 1 to a Python scalar".
This commit modifies the implementation of `Series.item()` and `Index.item()`
to delegate the single-item check and ValueError raising to pandas.

Previously, `item()` used `peek(2)` and manually checked the length.
The new implementation changes:
- `Series.item()` to `self.peek(1).item()`
- `Index.item()` to `self.to_series().peek(1).item()`

This ensures that the ValueError message ("can only convert an array of size 1 to a Python scalar")
is identical to the one produced by pandas when the Series/Index does not
contain exactly one element.

Existing tests were verified to still pass and accurately cover these
conditions by comparing against `pandas.Series.item()` and `pandas.Index.item()`.
google-labs-jules bot and others added 3 commits June 6, 2025 16:20
This commit incorporates several fixes and improvements based on feedback:

1.  **Docstring Style**:
    *   "Examples:" headings in `Series.item()` and `Index.item()`
        docstrings (in `third_party/`) are now bold (`**Examples:**`).

2.  **Implementation of `item()`**:
    *   `Series.item()` now uses `self.peek(2)` and then calls `.item()` on
        the peeked pandas Series if length is 1, otherwise raises
        `ValueError("can only convert an array of size 1 to a Python scalar")`.
    *   `Index.item()` now uses `self.to_series().peek(2)` and then calls
        `.item()` on the peeked pandas Series if length is 1, otherwise
        raises the same ValueError.
        This change was made to allow tests to fail correctly when there is
        more than 1 item, rather than relying on pandas' `peek(1).item()`
        which would fetch only one item and not detect the multi-item error.

3.  **Test Updates**:
    *   Tests for `Series.item()` and `Index.item()` now capture the
        precise error message from the corresponding pandas method when
        testing error conditions (multiple items, empty).
    *   The tests now assert that the BigQuery DataFrames methods raise
        a `ValueError` with a message identical to the one from pandas.

4.  **Doctest Fix**:
    *   The doctest for `Series.item()` in
        `third_party/bigframes_vendored/pandas/core/series.py` has been
        updated to expect `np.int64(1)` to match pandas behavior.
        `import numpy as np` was added to the doctest.

5.  **Mypy Fix**:
    *   A type annotation (`pd_idx_empty: pd.Index = ...`) was added in
        `tests/system/small/test_index.py` to resolve a `var-annotated`
        mypy error.
@tswast
Copy link
Collaborator Author

tswast commented Jun 6, 2025

Test failure for test_read_gbq_with_configuration appears to be a flake.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
api: bigquery Issues related to the googleapis/python-bigquery-dataframes API. size: m Pull request size is medium.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants