Skip to content

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

Closed
wants to merge 9 commits into from

Conversation

djgagne
Copy link

@djgagne djgagne commented Aug 20, 2015

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.

@@ -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)):
Copy link
Contributor

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?

Copy link
Author

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.

Copy link
Contributor

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?

@jreback jreback added Bug Indexing Related to indexing on series/frames, not to indexes themselves labels Aug 20, 2015
@jreback jreback added this to the 0.17.0 milestone Aug 20, 2015
@jreback jreback changed the title BUG: GH10610 fix BUG: import of maybe_convert_indices in pandas.core.index.py, #10610 Aug 21, 2015
@djgagne
Copy link
Author

djgagne commented Aug 24, 2015

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))
Copy link
Contributor

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?

Copy link
Author

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.

@jreback
Copy link
Contributor

jreback commented Aug 29, 2015

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])
Copy link
Contributor

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

@jreback
Copy link
Contributor

jreback commented Sep 1, 2015

merged via 12df3ea

thanks!

@jreback jreback closed this Sep 1, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Indexing Related to indexing on series/frames, not to indexes themselves
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Naming inconsistency in import of maybe_convert_indices in pandas.core.index.py
2 participants