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

Commit f5b7dba

Browse files
authored
strip, lstrip, rstrip work with NaN (#739)
1 parent 89a4b43 commit f5b7dba

2 files changed

Lines changed: 13 additions & 9 deletions

File tree

sdc/datatypes/hpat_pandas_stringmethods_functions.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1002,15 +1002,19 @@ def strip_usecase(s, to_strip):
10021002
def gen_sdc_pandas_series_str_strip_impl(usecase):
10031003
"""Generate series.str.lstrip/rstrip/strip implementations based on usecase func"""
10041004
def impl(self, to_strip=None):
1005+
mask = get_nan_mask(self._data._data)
10051006
item_count = len(self._data)
1006-
result = [''] * item_count
1007+
res_list = [''] * item_count
10071008

10081009
for it in range(item_count):
10091010
item = self._data._data[it]
10101011
if len(item) > 0:
1011-
result[it] = usecase(item, to_strip)
1012+
res_list[it] = usecase(item, to_strip)
10121013
else:
1013-
result[it] = item
1014+
res_list[it] = item
1015+
1016+
str_arr = create_str_arr_from_list(res_list)
1017+
result = str_arr_set_na_by_mask(str_arr, mask)
10141018

10151019
return pandas.Series(result, self._data._index, name=self._data._name)
10161020

@@ -1192,19 +1196,19 @@ def hpat_pandas_stringmethods_strip(self, to_strip=None):
11921196
'method': hpat_pandas_stringmethods_strip,
11931197
'caption': 'Remove leading and trailing characters.',
11941198
'seealso': seealso_strip_methods,
1195-
'limitations': limitation_nans_unsupported
1199+
'limitations': ''
11961200
},
11971201
'lstrip': {
11981202
'method': hpat_pandas_stringmethods_lstrip,
11991203
'caption': 'Remove leading and trailing characters.',
12001204
'seealso': seealso_strip_methods,
1201-
'limitations': limitation_nans_unsupported
1205+
'limitations': ''
12021206
},
12031207
'rstrip': {
12041208
'method': hpat_pandas_stringmethods_rstrip,
12051209
'caption': 'Remove leading and trailing characters.',
12061210
'seealso': seealso_strip_methods,
1207-
'limitations': limitation_nans_unsupported
1211+
'limitations': ''
12081212
}
12091213
}
12101214

sdc/tests/test_series.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5977,19 +5977,19 @@ def test_series_islower_str(self):
59775977
pd.testing.assert_series_equal(cfunc(S), islower_usecase(S))
59785978

59795979
def test_series_strip_str(self):
5980-
s = pd.Series(['1. Ant. ', '2. Bee!\n', '3. Cat?\t'])
5980+
s = pd.Series(['1. Ant. ', None, '2. Bee!\n', np.nan, '3. Cat?\t'])
59815981
cfunc = self.jit(strip_usecase)
59825982
for to_strip in [None, '123.', '.!? \n\t', '123.!? \n\t']:
59835983
pd.testing.assert_series_equal(cfunc(s, to_strip), strip_usecase(s, to_strip))
59845984

59855985
def test_series_lstrip_str(self):
5986-
s = pd.Series(['1. Ant. ', '2. Bee!\n', '3. Cat?\t'])
5986+
s = pd.Series(['1. Ant. ', None, '2. Bee!\n', np.nan, '3. Cat?\t'])
59875987
cfunc = self.jit(lstrip_usecase)
59885988
for to_strip in [None, '123.', '.!? \n\t', '123.!? \n\t']:
59895989
pd.testing.assert_series_equal(cfunc(s, to_strip), lstrip_usecase(s, to_strip))
59905990

59915991
def test_series_rstrip_str(self):
5992-
s = pd.Series(['1. Ant. ', '2. Bee!\n', '3. Cat?\t'])
5992+
s = pd.Series(['1. Ant. ', None, '2. Bee!\n', np.nan, '3. Cat?\t'])
59935993
cfunc = self.jit(rstrip_usecase)
59945994
for to_strip in [None, '123.', '.!? \n\t', '123.!? \n\t']:
59955995
pd.testing.assert_series_equal(cfunc(s, to_strip), rstrip_usecase(s, to_strip))

0 commit comments

Comments
 (0)