Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.