Skip to content

Instantly share code, notes, and snippets.

@shoyer
Created March 28, 2014 22:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shoyer/9844410 to your computer and use it in GitHub Desktop.
Save shoyer/9844410 to your computer and use it in GitHub Desktop.
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