Python 值\相同值的返回计数?

Python 值\相同值的返回计数?,python,pandas,Python,Pandas,下面是一节课 class MyClass: def __init__(self, cat): if cat not in ['STR', 'MTR', 'CA', 'Other', 'None']: raise ValueError self.cat = cat def __add__(self, other): if self.cat == 'STR' or other.cat =

下面是一节课

class MyClass: 

    def __init__(self, cat): 
        if cat not in ['STR', 'MTR', 'CA', 'Other', 'None']:
            raise ValueError
        self.cat = cat
        
    def __add__(self, other): 
        if self.cat == 'STR' or other.cat == 'STR':
            return MyClass('STR')
        if self.cat == 'MTR' or self.cat == 'CA' or other.cat == 'MTR' or other.cat == 'CA':
            return MyClass('MTR')
        return MyClass('None')
    
    def __eq__(self, other): 
        if isinstance(other, str):
            return self.cat == other
        elif isinstance(other, MyClass): 
            return self.cat == other.cat
        else:
            raise ValueError
            
    def __ne__(self, other): 
        if isinstance(other, str):
            return self.cat != other
        elif isinstance(other, MyClass): 
            return self.cat != other.cat
        else:
            raise ValueError    
            
    def __str__(self): 
        return self.cat
我正在尝试添加按日期索引的MyClass实例数组:

start_date =  pd.Timestamp(datetime.date(2017, 1, 1))
end_date =  pd.Timestamp(datetime.date(2017, 1, 31))
ref = pd.Series(MyClass('None'), index=pd.date_range(start_date, end_date))
ref.value_counts()
我不理解上述数值计算的结果

None    19
STR     11
None     1
dtype: int64
为什么没有人出现两次


谢谢

我认为熊猫需要对象的散列值来检查唯一性

你能把下面的内容加到你的课堂上吗-

def __hash__(self):
    return self.cat.__hash__()
添加此值后,应获得以下值-

None    20
STR     11
dtype: int64
ref = ref.add(test, fill_value=MyClass('None'))
ref.value_counts()
None    19
STR     11
None     1
dtype: int64
def __hash__(self):
    return self.cat.__hash__()
None    20
STR     11
dtype: int64