Created
March 28, 2014 22:33
-
-
Save shoyer/9844410 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
def convert_label_to_indexer(index, label): | |
"""Given a pandas.Index and labels (e.g., from __getitem__) for one dimension, | |
return an indexer suitable for indexing an ndarray along that dimension | |
""" | |
if isinstance(label, slice): | |
indexer = index.slice_indexer(label.start, label.stop, label.step) | |
elif np.isscalar(label): | |
indexer = indexer.get_loc(label) | |
else: | |
indexer = index.get_indexer(np.asarray(label)) | |
if np.any(indexer < 0): | |
raise ValueError('not all values found in index %r' % dim) | |
return indexer |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment