Reputation: 140
Here is the following class
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
I am trying to add arrays of MyClass instances indexed by dates:
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 31
dtype: int64
start_date = pd.Timestamp(datetime.date(2017, 1, 10))
end_date = pd.Timestamp(datetime.date(2017, 1, 20))
test = pd.Series(MyClass('STR'), index=pd.date_range(start_date, end_date))
test.value_counts()
STR 11
dtype: int64
ref = ref.add(test, fill_value=MyClass('None'))
ref.value_counts()
I do not understand the results from value_counts above
None 19
STR 11
None 1
dtype: int64
Why None appears twice?
Thank you
Upvotes: 0
Views: 100
Reputation: 201
i think hash value of your object is required for pandas to check uniqueness.
can you add below to your class -
def __hash__(self):
return self.cat.__hash__()
after adding this, you should get the below value -
None 20
STR 11
dtype: int64
Upvotes: 1