Before i thought of the recommended pandas way of s.isin, i used the x in s. (do not do this, it’s much slower than s.isin)
I did not realize then that it was searching in the keys of the series and not the values.
Only later i realized if i wanted to map, lambda, in, i have to do s.values to extract series values first.
Why is map, lambda, in (i don’t know which one or combination of them is causing this behaviour), searching within series keys?
I thought the right-hand-side of any in operator just has to be an iterable, and by doing for i in series: print(i) to check what is the iterable contained in any object(series in this case), we indeed see the values (1,2,3) printed, so the items among which membership check is happening should be the values (1,2,3) and not the index (0,1,2)?
How does series.isin work differently from map, lambda, in for it to check membership within series values correctly?
s = pd.Series([1,2,3])
s
to_search = [1,2,3]
s.isin(to_search)
list(map(lambda x:x in s, to_search)) # WHY IS IT SEARCHING KEYS!
list(map(lambda x:x in s.values, to_search))
That’s just how the method is defined. From the documentation:
Return a boolean Series showing whether each element in the Series matches an element in the passed sequence of values exactly.
This is all in. Given two python objects a and b, a in b is just a convenient way of writing b.__contains__(a). This means that b.__contains__ defines the behavior of a in b.
Turning back to your example, 3 in s is short for s.__contains__(3).
>>> s.__contains__(3)
False
Why does this method behave like this? In the source code (or by running help(s.__contains__)) we can see the following:
def __contains__(self, key) -> bool_t:
"""True if the key is in the info axis"""
return key in self._info_axis
Thanks, is there a way to see the class and method inheritance chain?
I was thinking which __iter__ is it. There is a __iter__ in pandas\pandas\core\generic.py which is basically the index.
def __iter__(self):
"""
Iterate over info axis.
Returns
-------
iterator
Info axis as iterator.
"""
return iter(self._info_axis)
Later, i did s.__iter__ to see <bound method IndexOpsMixin.__iter__ then i realize it’s another __iter__ in the mixin.
Is there a easier way to see the inheritance than printing obj.__dunder__ ?
Also, whenever i’m in a method, i must scroll up endlessly to find which class contains this method and risk jumping into another class, any way to jump out of/collapse current cursor position over a method to find it’s class? (i’m using VS Code)
Yeah, things like this are the reason why I presented the alternative of calling help, because it will show you true information and get this out of the way for you, even if it isn’t as insightful.
Given a class, you can find its methods by calling dir: