Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
florentguymartin opened this issue Jun 13, 2018 · 3 comments
Closed

Comments

@florentguymartin
Copy link

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.

@jreback
Copy link
Contributor

jreback commented Jun 13, 2018

this was fixed in #21098

@TomAugspurger this should have been backported?

@florentguymartin
Copy link
Author

I just realized that the reason is the wrong import statement

which should have just been
import re
Sorry for that.

@TomAugspurger
Copy link
Contributor

It'd be nice if we could support the 3rd party regex module, but I have no idea how much work that would be.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants