You can use the equal
operation to create a bool
series then use any()
to get the desired result. Or as a more comprehensive approach you can use apply()
method of the series objects in order to apply a specific function on all items. Then you can use any()
to get the expected result:
In [28]: (s == ('a', 'b', 'c')).any()
Out[28]: True
In [30]: s.apply(('a', 'b', 'c').__eq__).any()
Out[30]: True
Also note that since Series
is a One-dimensional ndarray with axis labels the membership checking with in
operator will be performed on indices rather than the items.
In [32]: 3 in s
Out[32]: False
In [33]: 2 in s
Out[33]: True
In case you want to change this behavior you might want to override the __contains__
method of the object by creating your own Series
type.
In [39]: class MySeries(pd.Series):
def __init__(self, *args, **kwargs):
super(MySeries, self).__init__(*args, **kwargs)
def __contains__(self, arg):
return (self == arg).any()
....:
In [40]: ms = MySeries([('a','b','c'), ('a','c','b'), ('c','a','b')])
In [41]: ('a', 'b', 'c') in ms
Out[41]: True
In [42]:
In [42]: ('a', 'b', 't') in ms
Out[42]: False
You can also make it work for both indices and items:
In [51]: class MySeries(pd.Series):
def __init__(self, *args, **kwargs):
super(MySeries, self).__init__(*args, **kwargs)
def __contains__(self, arg):
if isinstance(arg, int):
return super(MySeries, self).__contains__(arg)
return (self == arg).any()
....:
In [52]:
In [52]: ms = MySeries([('a','b','c'), ('a','c','b'), ('c','a','b')])
In [53]:
In [53]: 3 in ms
Out[53]: False
In [54]: 2 in ms
Out[54]: True
In [55]:
In [55]: ('a', 'b', 'c') in ms
Out[55]: True
In [56]: ('a', 'b', 'd') in ms
Out[56]: False