0
我希望能夠在DataFrame中有一些通用對象實例的列標籤。因此,而不是一個個STR對象,一個簡單的類,它包裝STR並提供一些額外的功能:熊貓指數對象
class WrapStr(object):
def __init__(self,str):
self.str = str
def __eq__(self,other):
return self.str == other.str
def __repr__(self):
return self.str
的問題是,pd.Index不呼籲WrapStr實例EQ方法而只是檢查是否這兩個實例是相同的。
first_ins = WrapStr('col1')
my_ix = pd.Index([first_ins])
sec_ins = WrapStr('col1')
print first_ins in my_ix # True
print sec_ins in my_ix # False
它看起來像包含檢查在https://github.com/pydata/pandas/blob/master/pandas/index.pyx線92和448
如何支持這樣的擴展列標籤任何想法界定?
這是一般不可能的。這些字符串需要c-hashable,IOW,做索引計算的c-libs需要字符串。你可以嘗試定義''__hash__'',可能工作。 – Jeff
謝謝傑夫的快速反應! – bjonen