Skip to content

Using series.str with compiled regular expressions. Problem with is_re #21458

Closed
@florentguymartin

Description

@florentguymartin

I am using pandas 0.23.1 with python 3.6.4

import pandas as pd
import regex as re
s = pd.Series(['foo', 'bar'])
pat = re.compile('a')
s.str.replace(pat, ' ')

Raises the error

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-5-4fc8cf4e83c0> in <module>()
----> 1 s.str.replace(pat, ' ')

~/anaconda3/lib/python3.6/site-packages/pandas/core/strings.py in replace(self, pat, repl, n, case, flags, regex)
   2427     def replace(self, pat, repl, n=-1, case=None, flags=0, regex=True):
   2428         result = str_replace(self._data, pat, repl, n=n, case=case,
-> 2429                              flags=flags, regex=regex)
   2430         return self._wrap_result(result)
   2431 

~/anaconda3/lib/python3.6/site-packages/pandas/core/strings.py in str_replace(arr, pat, repl, n, case, flags, regex)
    652             if case is False:
    653                 flags |= re.IGNORECASE
--> 654         if is_compiled_re or len(pat) > 1 or flags or callable(repl):
    655             n = n if n >= 0 else 0
    656             compiled = re.compile(pat, flags=flags)

TypeError: object of type '_regex.Pattern' has no len()

I think that the problem comes from the call to the function is_re, namely

from pandas.core.dtypes.common import is_re
is_re(pat)

returns False while I think it should return True.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions