Commit d2154c8
feat: Implement item() for Series and Index (#1792)
* feat: Implement item() for Series and Index
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)
* refactor: Move item() docstrings to third_party
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.
* Apply suggestions from code review
* Here's the test I've prepared:
**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".
* 🦉 Updates from OwlBot post-processor
See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md
* fix: Ensure item() matches pandas error messages exactly
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()`.
* 🦉 Updates from OwlBot post-processor
See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md
* fix: Address feedback for Series.item() and Index.item()
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.
* 🦉 Updates from OwlBot post-processor
See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md
* split tests into multiple test cases
---------
Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com>
Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>1 parent 570a40b commit d2154c8
File tree
6 files changed
+127
-0
lines changed- bigframes
- core/indexes
- tests/system/small
- third_party/bigframes_vendored/pandas/core
- indexes
6 files changed
+127
-0
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
618 | 618 | | |
619 | 619 | | |
620 | 620 | | |
| 621 | + | |
| 622 | + | |
| 623 | + | |
| 624 | + | |
621 | 625 | | |
622 | 626 | | |
623 | 627 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
960 | 960 | | |
961 | 961 | | |
962 | 962 | | |
| 963 | + | |
| 964 | + | |
| 965 | + | |
| 966 | + | |
963 | 967 | | |
964 | 968 | | |
965 | 969 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
12 | 12 | | |
13 | 13 | | |
14 | 14 | | |
| 15 | + | |
| 16 | + | |
15 | 17 | | |
16 | 18 | | |
17 | 19 | | |
| |||
458 | 460 | | |
459 | 461 | | |
460 | 462 | | |
| 463 | + | |
| 464 | + | |
| 465 | + | |
| 466 | + | |
| 467 | + | |
| 468 | + | |
| 469 | + | |
| 470 | + | |
| 471 | + | |
| 472 | + | |
| 473 | + | |
| 474 | + | |
| 475 | + | |
| 476 | + | |
| 477 | + | |
| 478 | + | |
| 479 | + | |
| 480 | + | |
| 481 | + | |
| 482 | + | |
| 483 | + | |
| 484 | + | |
| 485 | + | |
| 486 | + | |
| 487 | + | |
| 488 | + | |
| 489 | + | |
| 490 | + | |
| 491 | + | |
| 492 | + | |
| 493 | + | |
| 494 | + | |
| 495 | + | |
| 496 | + | |
| 497 | + | |
| 498 | + | |
| 499 | + | |
| 500 | + | |
| 501 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
4642 | 4642 | | |
4643 | 4643 | | |
4644 | 4644 | | |
| 4645 | + | |
| 4646 | + | |
| 4647 | + | |
| 4648 | + | |
| 4649 | + | |
| 4650 | + | |
| 4651 | + | |
| 4652 | + | |
| 4653 | + | |
| 4654 | + | |
| 4655 | + | |
| 4656 | + | |
| 4657 | + | |
| 4658 | + | |
| 4659 | + | |
| 4660 | + | |
| 4661 | + | |
| 4662 | + | |
| 4663 | + | |
| 4664 | + | |
| 4665 | + | |
| 4666 | + | |
| 4667 | + | |
| 4668 | + | |
| 4669 | + | |
| 4670 | + | |
| 4671 | + | |
| 4672 | + | |
| 4673 | + | |
| 4674 | + | |
| 4675 | + | |
| 4676 | + | |
| 4677 | + | |
| 4678 | + | |
| 4679 | + | |
| 4680 | + | |
| 4681 | + | |
| 4682 | + | |
| 4683 | + | |
Lines changed: 19 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1087 | 1087 | | |
1088 | 1088 | | |
1089 | 1089 | | |
| 1090 | + | |
| 1091 | + | |
| 1092 | + | |
| 1093 | + | |
| 1094 | + | |
| 1095 | + | |
| 1096 | + | |
| 1097 | + | |
| 1098 | + | |
| 1099 | + | |
| 1100 | + | |
| 1101 | + | |
| 1102 | + | |
| 1103 | + | |
| 1104 | + | |
| 1105 | + | |
| 1106 | + | |
| 1107 | + | |
| 1108 | + | |
1090 | 1109 | | |
1091 | 1110 | | |
1092 | 1111 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
4933 | 4933 | | |
4934 | 4934 | | |
4935 | 4935 | | |
| 4936 | + | |
| 4937 | + | |
| 4938 | + | |
| 4939 | + | |
| 4940 | + | |
| 4941 | + | |
| 4942 | + | |
| 4943 | + | |
| 4944 | + | |
| 4945 | + | |
| 4946 | + | |
| 4947 | + | |
| 4948 | + | |
| 4949 | + | |
| 4950 | + | |
| 4951 | + | |
| 4952 | + | |
| 4953 | + | |
| 4954 | + | |
| 4955 | + | |
4936 | 4956 | | |
4937 | 4957 | | |
4938 | 4958 | | |
| |||
0 commit comments