我尝试在多索引中使用对象作为列标题
数据框,但我似乎无法让它工作。 __eq__, __hash__
并且__str__
仅适用于简单的数据框。
这是一个小例子:
class Signal:
def __init__(self, name):
self.name = name
def __eq__(self, other):
try:
return self.name == other or self.name == other.name
except AttributeError as err:
return False
def __str__(self):
return str(self.name)
def __hash__(self):
return hash(self.name)
if __name__ == '__main__':
import pandas as pd
import numpy as np
a = Signal('name')
b = Signal('name2')
c = Signal('something')
data = {
('A', a): np.arange(2),
('A', b): np.ones(2),
('B', c): np.zeros(2)
}
df = pd.DataFrame(data)
print(df)
print('-----------')
print(df['A'])
我也尝试过实施__le__, __ge__
和__ne__
。那没有
做任何事情。我真的不知道我还能做些什么。
有人有想法吗?
答案 0 :(得分:2)
定义=EOMONTH(A2,0)
和__lt__
__gt__
class Signal:
def __init__(self, name):
self.name = name
def __eq__(self, other):
try:
return self.name == other or self.name == other.name
except AttributeError as err:
return False
def __lt__(self, other):
return self.name < other.name
def __gt__(self, other):
return self.name > other.name
def __str__(self):
return str(self.name)
def __hash__(self):
return hash(self.name)