0

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

1
  • I imagine you could achieve this using a __getattr__ as well. Of course, I have no idea if that's what Pandas actually does Commented May 20, 2022 at 6:10

2 Answers 2

1

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
Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.