Skip to content
This repository was archived by the owner on Feb 2, 2024. It is now read-only.

Commit 99abf61

Browse files
densmirnAlexanderKalistratov
authored andcommitted
Implement Series.str.endswith() (#384)
1 parent dedb0a9 commit 99abf61

3 files changed

Lines changed: 90 additions & 3 deletions

File tree

sdc/datatypes/hpat_pandas_stringmethods_functions.py

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,8 @@ def hpat_pandas_stringmethods_upper_impl(self):
8282

8383
import numba
8484
from numba.extending import overload_method
85-
from numba.types import (Integer, NoneType, Omitted, StringLiteral, UnicodeType)
85+
from numba.types import (Boolean, Integer, NoneType,
86+
Omitted, StringLiteral, UnicodeType)
8687

8788
from sdc.datatypes.common_functions import TypeChecker
8889
from sdc.datatypes.hpat_pandas_stringmethods_types import StringMethodsType
@@ -188,6 +189,57 @@ def hpat_pandas_stringmethods_{methodname}_impl(self{methodparams}):
188189
"""
189190

190191

192+
@overload_method(StringMethodsType, 'endswith')
193+
def hpat_pandas_stringmethods_endswith(self, pat, na=None):
194+
"""
195+
Pandas Series method :meth:`pandas.core.strings.StringMethods.endswith()` implementation.
196+
197+
Note: Unicode type of list elements are supported only. Numpy.NaN is not supported as elements.
198+
199+
.. only:: developer
200+
201+
Test: python -m sdc.runtests -k sdc.tests.test_series.TestSeries.test_series_endswith
202+
203+
Parameters
204+
----------
205+
self: :class:`pandas.core.strings.StringMethods`
206+
input arg
207+
pat: :obj:`str`
208+
Character sequence
209+
na: :obj:`bool`
210+
Object shown if element tested is not a string
211+
*unsupported*
212+
213+
Returns
214+
-------
215+
:obj:`pandas.Series`
216+
returns :obj:`pandas.Series` object
217+
"""
218+
219+
ty_checker = TypeChecker('Method endswith().')
220+
ty_checker.check(self, StringMethodsType)
221+
222+
if not isinstance(pat, (StringLiteral, UnicodeType)):
223+
ty_checker.raise_exc(pat, 'str', 'pat')
224+
225+
if not isinstance(na, (Boolean, NoneType, Omitted)) and na is not None:
226+
ty_checker.raise_exc(na, 'bool', 'na')
227+
228+
def hpat_pandas_stringmethods_endswith_impl(self, pat, na=None):
229+
if na is not None:
230+
msg = 'Method endswith(). The object na\n expected: None'
231+
raise ValueError(msg)
232+
233+
item_endswith = len(self._data)
234+
result = numpy.empty(item_endswith, numba.types.boolean)
235+
for idx, item in enumerate(self._data._data):
236+
result[idx] = item.endswith(pat)
237+
238+
return pandas.Series(result, self._data._index, name=self._data._name)
239+
240+
return hpat_pandas_stringmethods_endswith_impl
241+
242+
191243
@overload_method(StringMethodsType, 'find')
192244
def hpat_pandas_stringmethods_find(self, sub, start=0, end=None):
193245
"""

sdc/hiframes/pd_series_ext.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -758,8 +758,8 @@ def resolve_head(self, ary, args, kws):
758758
Functions which are still overloaded by HPAT compiler pipeline
759759
"""
760760

761-
str2str_methods_excluded = ['upper', 'find', 'isupper', 'len', 'lower',
762-
'lstrip', 'rstrip', 'strip']
761+
str2str_methods_excluded = ['upper', 'endswith', 'find', 'isupper', 'len',
762+
'lower', 'lstrip', 'rstrip', 'strip']
763763
"""
764764
Functions which are used from Numba directly by calling from StringMethodsType
765765

sdc/tests/test_series.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2424,6 +2424,41 @@ def test_impl(S1, S2):
24242424
hpat_func(S1, S2), test_impl(S1, S2),
24252425
err_msg='S1={}\nS2={}'.format(S1, S2))
24262426

2427+
def test_series_str_endswith(self):
2428+
def test_impl(series, pat):
2429+
return series.str.endswith(pat)
2430+
2431+
hpat_func = self.jit(test_impl)
2432+
2433+
data = test_global_input_data_unicode_kind4
2434+
pats = [''] + [s[-min(len(s) for s in data):] for s in data] + data
2435+
indices = [None, list(range(len(data)))[::-1], data[::-1]]
2436+
names = [None, 'A']
2437+
for index, name in product(indices, names):
2438+
series = pd.Series(data, index, name=name)
2439+
for pat in pats:
2440+
pd.testing.assert_series_equal(hpat_func(series, pat),
2441+
test_impl(series, pat))
2442+
2443+
def test_series_str_endswith_exception_unsupported_na(self):
2444+
def test_impl(series, pat, na):
2445+
return series.str.endswith(pat, na)
2446+
2447+
hpat_func = self.jit(test_impl)
2448+
2449+
series = pd.Series(test_global_input_data_unicode_kind4)
2450+
msg_tmpl = 'Method endswith(). The object na\n {}'
2451+
2452+
with self.assertRaises(TypingError) as raises:
2453+
hpat_func(series, '', 'None')
2454+
msg = msg_tmpl.format('given: unicode_type\n expected: bool')
2455+
self.assertIn(msg, str(raises.exception))
2456+
2457+
with self.assertRaises(ValueError) as raises:
2458+
hpat_func(series, '', False)
2459+
msg = msg_tmpl.format('expected: None')
2460+
self.assertIn(msg, str(raises.exception))
2461+
24272462
def test_series_str_find(self):
24282463
def test_impl(series, sub):
24292464
return series.str.find(sub)

0 commit comments

Comments
 (0)