-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
BUG: import of maybe_convert_indices in pandas.core.index.py, #10610 #10872
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
Conversation
…checking in the case of a mixed-integer index. Added a test to verify in test_index
…checking in the case of a mixed-integer index. Added a test to verify in test_index
…e_index # Conflicts: # pandas/tests/test_index.py
…into mixed_type_index
…into mixed_type_index n test_index
@@ -971,11 +971,13 @@ def _convert_list_indexer(self, keyarr, kind=None): | |||
|
|||
if self.inferred_type == 'mixed-integer': | |||
indexer = self.get_indexer(keyarr) | |||
if not np.all(np.in1d(indexer, self.values)): |
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.
why is this line necessary?
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.
Without the line, when passed a list containing index values, the function would output the data associated with the nearest integer index value. For other cases where an invalid index is passed to a DataFrame, an IndexError would be raised, so I made sure to explicitly check for that condition.
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.
your test doesn't hit that line though it goes thru
can u make a test that is explicitly caught there?
I removed the index checking if-statement, and added a line that would change the -1 indices to equal the length of the Index being evaluated, so that the IndexError would be raised in maybe_convert_indices. |
# values outside the range of indices so as to trigger an IndexError in maybe_convert_indices | ||
indexer[indexer < 0] = len(self) | ||
from pandas.core.indexing import maybe_convert_indices | ||
return maybe_convert_indices(indexer, len(self)) |
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.
this is already done inside maybe_convert_indices
why do you think its needed here?
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.
It is necessary because negative indices passed into maybe_convert_indices are summed by the length of the array before the bounds checking is done. Because get_indexer assigns labels that are not found in the index with a value of -1, the last row or column is assigned to those positions instead of either NaNs or an exception being raised. With the line I added, the bounds checking in maybe_convert_indices will now throw an IndexError for out of bounds indices.
ok, this is fine. pls squash to a single commit. ping when green. |
@@ -1729,6 +1729,11 @@ def test_equals_op_multiindex(self): | |||
df.index == index_a | |||
tm.assert_numpy_array_equal(index_a == mi3, np.array([False, False, False])) | |||
|
|||
def test_multitype_list_index_access(self): | |||
df = pd.DataFrame(np.random.random((10, 5)), columns=["a"] + [20, 21, 22, 23]) |
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.
add the issue number as a comment here
merged via 12df3ea thanks! |
closes #10610
I fixed the import statement and added a test to check for proper behavior when accessing a mixed-integer index with a list of values.