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

Commit 69c5214

Browse files
authored
Implement Series.str.center() (#385)
1 parent efa5a89 commit 69c5214

3 files changed

Lines changed: 112 additions & 2 deletions

File tree

sdc/datatypes/hpat_pandas_stringmethods_functions.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,53 @@ def hpat_pandas_stringmethods_{methodname}_impl(self{methodparams}):
189189
"""
190190

191191

192+
@overload_method(StringMethodsType, 'center')
193+
def hpat_pandas_stringmethods_center(self, width, fillchar=' '):
194+
"""
195+
Pandas Series method :meth:`pandas.core.strings.StringMethods.center()` 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_center
202+
203+
Parameters
204+
----------
205+
self: :class:`pandas.core.strings.StringMethods`
206+
input arg
207+
width: :obj:`int`
208+
Minimum width of resulting string
209+
fillchar: :obj:`str`
210+
Additional character for filling, default is whitespace
211+
212+
Returns
213+
-------
214+
:obj:`pandas.Series`
215+
returns :obj:`pandas.Series` object
216+
"""
217+
218+
ty_checker = TypeChecker('Method center().')
219+
ty_checker.check(self, StringMethodsType)
220+
221+
if not isinstance(width, Integer):
222+
ty_checker.raise_exc(width, 'int', 'width')
223+
224+
accepted_types = (Omitted, StringLiteral, UnicodeType)
225+
if not isinstance(fillchar, accepted_types) and fillchar != ' ':
226+
ty_checker.raise_exc(fillchar, 'str', 'fillchar')
227+
228+
def hpat_pandas_stringmethods_center_impl(self, width, fillchar=' '):
229+
item_count = len(self._data)
230+
result = [''] * item_count
231+
for idx, item in enumerate(self._data._data):
232+
result[idx] = item.center(width, fillchar)
233+
234+
return pandas.Series(result, self._data._index, name=self._data._name)
235+
236+
return hpat_pandas_stringmethods_center_impl
237+
238+
192239
@overload_method(StringMethodsType, 'endswith')
193240
def hpat_pandas_stringmethods_endswith(self, pat, na=None):
194241
"""

sdc/hiframes/pd_series_ext.py

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

761-
str2str_methods_excluded = ['upper', 'endswith', 'find', 'isupper', 'len',
762-
'lower', 'lstrip', 'rstrip', 'startswith', 'strip']
761+
str2str_methods_excluded = [
762+
'upper', 'center', 'endswith', 'find', 'isupper', 'len',
763+
'lower', 'lstrip', 'rstrip', 'startswith', 'strip'
764+
]
763765
"""
764766
Functions which are used from Numba directly by calling from StringMethodsType
765767

sdc/tests/test_series.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2424,6 +2424,67 @@ 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_center_default_fillchar(self):
2428+
def test_impl(series, width):
2429+
return series.str.center(width)
2430+
2431+
hpat_func = self.jit(test_impl)
2432+
2433+
data = test_global_input_data_unicode_kind1
2434+
series = pd.Series(data)
2435+
width = max(len(s) for s in data) + 10
2436+
2437+
pd.testing.assert_series_equal(hpat_func(series, width),
2438+
test_impl(series, width))
2439+
2440+
def test_series_str_center(self):
2441+
def test_impl(series, width, fillchar):
2442+
return series.str.center(width, fillchar)
2443+
2444+
hpat_func = self.jit(test_impl)
2445+
2446+
data = test_global_input_data_unicode_kind1
2447+
data_lengths = [len(s) for s in data]
2448+
widths = [max(data_lengths) + 10, min(data_lengths)]
2449+
2450+
for index in [None, list(range(len(data)))[::-1], data[::-1]]:
2451+
series = pd.Series(data, index, name='A')
2452+
for width, fillchar in product(widths, ['\t']):
2453+
jit_result = hpat_func(series, width, fillchar)
2454+
ref_result = test_impl(series, width, fillchar)
2455+
pd.testing.assert_series_equal(jit_result, ref_result)
2456+
2457+
def test_series_str_center_exception_unsupported_fillchar(self):
2458+
def test_impl(series, width, fillchar):
2459+
return series.str.center(width, fillchar)
2460+
2461+
hpat_func = self.jit(test_impl)
2462+
2463+
data = test_global_input_data_unicode_kind1
2464+
series = pd.Series(data)
2465+
width = max(len(s) for s in data) + 10
2466+
2467+
with self.assertRaises(TypingError) as raises:
2468+
hpat_func(series, width, 10)
2469+
msg_tmpl = 'Method center(). The object fillchar\n {}'
2470+
msg = msg_tmpl.format('given: int64\n expected: str')
2471+
self.assertIn(msg, str(raises.exception))
2472+
2473+
def test_series_str_center_exception_unsupported_kind4(self):
2474+
def test_impl(series, width):
2475+
return series.str.center(width)
2476+
2477+
hpat_func = self.jit(test_impl)
2478+
2479+
data = test_global_input_data_unicode_kind4
2480+
series = pd.Series(data)
2481+
width = max(len(s) for s in data) + 10
2482+
2483+
with self.assertRaises(SystemError) as raises:
2484+
hpat_func(series, width)
2485+
msg = 'NULL object passed to Py_BuildValue'
2486+
self.assertIn(msg, str(raises.exception))
2487+
24272488
def test_series_str_endswith(self):
24282489
def test_impl(series, pat):
24292490
return series.str.endswith(pat)

0 commit comments

Comments
 (0)