-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
BUG: fix a crash when retrieving rows from a multiple-index table #15826
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -31,13 +31,19 @@ | |||||||||
array.view(Column) -> no indices | ||||||||||
""" | ||||||||||
|
||||||||||
from __future__ import annotations | ||||||||||
|
||||||||||
from copy import deepcopy | ||||||||||
from typing import TYPE_CHECKING | ||||||||||
|
||||||||||
import numpy as np | ||||||||||
|
||||||||||
from .bst import MaxValue, MinValue | ||||||||||
from .sorted_array import SortedArray | ||||||||||
|
||||||||||
if TYPE_CHECKING: | ||||||||||
from collections.abc import Iterable | ||||||||||
|
||||||||||
|
||||||||||
class QueryError(ValueError): | ||||||||||
""" | ||||||||||
|
@@ -827,21 +833,27 @@ | |||||||||
self.table = table | ||||||||||
self.indices = table.indices | ||||||||||
|
||||||||||
def _get_rows(self, item): | ||||||||||
def _get_rows( | ||||||||||
self, | ||||||||||
item: int | Iterable[int] | slice | tuple[str, slice], | ||||||||||
) -> list[np.int64]: | ||||||||||
""" | ||||||||||
Retrieve Table rows indexes by value slice. | ||||||||||
""" | ||||||||||
if len(self.indices) == 0: | ||||||||||
raise ValueError("Can only use TableLoc for a table with indices") | ||||||||||
|
||||||||||
if isinstance(item, tuple): | ||||||||||
if ( | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I note that this block is identical to astropy/astropy/table/index.py Lines 950 to 953 in 9cd4f4f
So I suppose it should be updated as well, and probably even abstracted away in a helper function to avoid repeated code. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. On the other hand I don't know if that would fix anything currently broken. In other words, I don't know that |
||||||||||
isinstance(item, tuple) | ||||||||||
and len(item) == 2 | ||||||||||
and isinstance(item[0], str) | ||||||||||
and isinstance(item[1], slice) | ||||||||||
): | ||||||||||
key, item = item | ||||||||||
else: | ||||||||||
key = self.table.primary_key | ||||||||||
|
||||||||||
index = self.indices[key] | ||||||||||
if len(index.columns) > 1: | ||||||||||
raise ValueError("Cannot use .loc on multi-column indices") | ||||||||||
|
||||||||||
if isinstance(item, slice): | ||||||||||
# None signifies no upper/lower bound | ||||||||||
|
@@ -854,7 +866,7 @@ | |||||||||
# item should be a list or ndarray of values | ||||||||||
rows = [] | ||||||||||
for key in item: | ||||||||||
p = index.find((key,)) | ||||||||||
p = index.find(key if isinstance(key, tuple) else (key,)) | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @taldcroft this part of the fix is yours. Let me know if I should explicitly add you as a co-author of the commit. |
||||||||||
if len(p) == 0: | ||||||||||
raise KeyError(f"No matches found for key {key}") | ||||||||||
else: | ||||||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Fix a crash when retrieving rows from a multiple-index table. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
obviously, these annotations are not part of the fix, rather a byproduct of my time spent looking for the bug :)