"I found," that I can create pandas.Index, using Python objects, and everything looks fine, as long as the objects are implemented: __hash__
, __eq__
, __ne__
, __str__
. Is there any success for this? For example. will sort and select work as fast as if i were using strings or integer indices? How well is this indicator supported? Is there any documentation on how to do this correctly?
Here is an example:
class MyObject(object):
def __init__(self, name):
self.name = name
self.complicated_object = lambda x: 2 * x
def __hash__(self):
return hash(self.name)
def __str__(self):
return self.name
def __eq__(self, other):
if isinstance(other, basestring):
return self.name == other
else:
return self.name == other.name
my_series = pd.Series([1, 2], index=[MyObject('cat'), MyObject('dog')])
print my_series
my_series.index[0]
Prints
cat 1
dog 2
dtype: int64
<__main__.MyObject at 0x81a67d0>
source
share