Thanks to visit codestin.com
Credit goes to github.com

Skip to content

feat: Allow iloc to support lists of negative indices #1497

New issue

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

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

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Mar 19, 2025
Merged

Conversation

Genesis929
Copy link
Collaborator

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 #<issue_number_goes_here> 🦕

@product-auto-label product-auto-label bot added size: s Pull request size is small. api: bigquery Issues related to the googleapis/python-bigquery-dataframes API. labels Mar 17, 2025
@Genesis929 Genesis929 marked this pull request as ready for review March 17, 2025 22:51
@Genesis929 Genesis929 requested review from a team as code owners March 17, 2025 22:51
@Genesis929 Genesis929 requested a review from shuoweil March 17, 2025 22:51
@Genesis929 Genesis929 requested a review from GarrettWu March 17, 2025 22:51
@Genesis929 Genesis929 changed the title Iloc neg huanc feat: support iloc with negative indices Mar 17, 2025
@Genesis929 Genesis929 changed the title feat: support iloc with negative indices feat: iloc now supports negative indices Mar 17, 2025
@@ -4409,7 +4409,7 @@ def test_loc_list_multiindex(scalars_dfs_maybe_ordered):


def test_iloc_list(scalars_df_index, scalars_pandas_df_index):
index_list = [0, 0, 0, 5, 4, 7]
index_list = [0, 0, 0, 5, 4, 7, -2, -5, 3]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we support base case of iloc[neg_number]?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems works.

@Genesis929 Genesis929 changed the title feat: iloc now supports negative indices feat: iloc now supports list of negative indices Mar 17, 2025
@Genesis929 Genesis929 changed the title feat: iloc now supports list of negative indices feat: Allow iloc to support lists of negative indices Mar 17, 2025
@product-auto-label product-auto-label bot added size: m Pull request size is medium. and removed size: s Pull request size is small. labels Mar 18, 2025
if not is_key_unisigned or key[0] < 0:
neg_block, _ = block.apply_window_op(
offsets_id,
ops.aggregations.ReverseRowNumberOp(),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need a new op? Or could we have just used existing ops?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated, using SizeUnaryOp and SubOp instead.

Comment on lines 486 to 490
elif "shape" in series_or_dataframe._block.__dict__:
# If there is a cache, we convert all indices to positive.
row_count = series_or_dataframe._block.shape[0]
key = [k if k >= 0 else row_count + k for k in key]
is_key_unisigned = True
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems a bit fragile. We can use block.expr.node.row_count, but going though shape depends on some implementation details that might change. I don't know if we necessarily need this optimization at all?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed

@@ -477,6 +478,19 @@ def _iloc_getitem_series_or_dataframe(
Union[bigframes.dataframe.DataFrame, bigframes.series.Series],
series_or_dataframe.iloc[0:0],
)

# Check if both positive index and negative index are necessary
if isinstance(key, bigframes.series.Series):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

might need to check for bigframes.Index as well? Or maybe we should have a helper that helps identify an "remote" or "large" object we don't want to iterate over

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated to also check index type.

For large object it's may not be necessary, in cloudtop, I tried 1 million keys(which have the same sign), and this process took 0.03s.

@Genesis929 Genesis929 enabled auto-merge (squash) March 19, 2025 20:48
@Genesis929 Genesis929 merged commit a9cf215 into main Mar 19, 2025
18 of 24 checks passed
@Genesis929 Genesis929 deleted the iloc_neg_huanc branch March 19, 2025 21:20
shobsi pushed a commit that referenced this pull request Mar 28, 2025
* feat: support iloc with negative indices

* update partial ordering test

* update naming

* update logic

* update comment

* update logic and tests

* update filter
Sign up for free to join this conversation on GitHub. Already have an account? Sign in 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.

5 participants