Skip to content

Commit

Permalink
fix for Python 3.7: re._pattern_type no longer exist (#849)
Browse files Browse the repository at this point in the history
* fix for Python 3.7: re._pattern_type no longer exist

* re._pattern_type was removed in Python 3.7. re.Pattern can be used
instead.
* Try re.Pattern first, and fall back to re._pattern_type otherwise.

* Update try..except block to if..else for performance

* Also add check in test_series.py to check for supported behavior in
  regex.
  • Loading branch information
smola authored and devin-petersohn committed Nov 3, 2019
1 parent 6a51d9a commit a824069
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 9 deletions.
19 changes: 10 additions & 9 deletions modin/pandas/series.py
Expand Up @@ -15,6 +15,13 @@
from .utils import _inherit_docstrings
from .utils import from_pandas, to_pandas

if sys.version_info[0] == 3 and sys.version_info[1] >= 7:
# Python >= 3.7
from re import Pattern as _pattern_type
else:
# Python <= 3.6
from re import _pattern_type


@_inherit_docstrings(pandas.Series, excluded=[pandas.Series, pandas.Series.__init__])
class Series(BasePandasDataset):
Expand Down Expand Up @@ -1434,9 +1441,7 @@ def slice_replace(self, start=None, stop=None, repl=None):
)

def count(self, pat, flags=0, **kwargs):
import re

if not isinstance(pat, (str, re._pattern_type)):
if not isinstance(pat, (str, _pattern_type)):
raise TypeError("first argument must be string or compiled pattern")
return Series(
query_compiler=self._query_compiler.str_count(pat, flags=flags, **kwargs)
Expand All @@ -1449,18 +1454,14 @@ def endswith(self, pat, na=np.NaN):
return Series(query_compiler=self._query_compiler.str_endswith(pat, na=na))

def findall(self, pat, flags=0, **kwargs):
import re

if not isinstance(pat, (str, re._pattern_type)):
if not isinstance(pat, (str, _pattern_type)):
raise TypeError("first argument must be string or compiled pattern")
return Series(
query_compiler=self._query_compiler.str_findall(pat, flags=flags, **kwargs)
)

def match(self, pat, case=True, flags=0, na=np.NaN):
import re

if not isinstance(pat, (str, re._pattern_type)):
if not isinstance(pat, (str, _pattern_type)):
raise TypeError("first argument must be string or compiled pattern")
return Series(
query_compiler=self._query_compiler.str_match(pat, flags=flags, na=na)
Expand Down
5 changes: 5 additions & 0 deletions modin/pandas/test/test_series.py
Expand Up @@ -5,6 +5,7 @@
import matplotlib
import modin.pandas as pd
from numpy.testing import assert_array_equal
import sys

from modin.pandas.utils import to_pandas
from .utils import (
Expand Down Expand Up @@ -2717,6 +2718,10 @@ def test_str_cat():
@pytest.mark.parametrize("n", int_arg_values, ids=int_arg_keys)
@pytest.mark.parametrize("expand", bool_arg_values, ids=bool_arg_keys)
def test_str_split(data, pat, n, expand):
# Empty pattern not supported on Python 3.7+
if sys.version_info[0] == 3 and sys.version_info[1] >= 7 and pat == "":
return

modin_series, pandas_series = create_test_series(data)

if n >= -1:
Expand Down

0 comments on commit a824069

Please sign in to comment.