So in Pandas we can do str
operations on a string column like
str_lower = df["str_col"].str.lower()
I wonder, how is the str.lower()
implemented in a class (note it is not about the specific implementation of str.lower()
but more how such a thing would be implemented in python generally)?
The only thing I can think of, Is a method of a sub-class defined in the class e.g
class DataFrame():
class str():
def lower(self):
return [p.lower() for p in self.column]
but I doubt it's correct
CodePudding user response:
Since Pandas is open source, you can find the code on Github! Here is the .str implementation page. The _map_and_wrap
function provides a nice way of understanding what's happening, start with it and go deeper!
def _map_and_wrap(name, docstring):
@forbid_nonstring_types(["bytes"], name=name)
def wrapper(self):
result = getattr(self._data.array, f"_str_{name}")()
return self._wrap_result(result)
wrapper.__doc__ = docstring
return wrapper
CodePudding user response:
Maybe it uses property
, it allows you to get the return value of a function like a attribute:
>>> class Foo:
... def __init__(self, *args):
... self.lst = list(args)
... @property
... def string(self):
... return ' '.join(map(str, self.lst))
...
>>> f = Foo(1, 2, 3, 4, 5)
>>> f.string
'1 2 3 4 5'
>>> f.string.split()
['1', '2', '3', '4', '5']
The essence of property
is to wrap a function into a descriptor. You can consider learning about it.