Search code examples
pythonregexpandaspython-rere2

Can I use re2 library with pandas?


I'm using re2 library with Python 3 using this library: https://github.com/andreasvc/pyre2

I want to use this library inside pandas in this example:

pandas_series.str.contains(regex, case=False)

It's possible to use pandas and re2 library together in this example?


Solution

  • Since Pandas regex methods use re, you can only use apply and pass a custom method using RE2 regex.

    You may use

    import pandas as pd
    import re2
    df = pd.DataFrame({'test': [ 'abc' , 'def' , '123' ]})
    def re2_contains(s, rx):
        return bool(rx.search(s))
    
    rex = re2.compile(r'^[a-z]+$')
    >>> df['test'].apply(lambda x: re2_contains(x, rex))
    0     True
    1     True
    2    False
    Name: test, dtype: bool