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

Commit 3782e01

Browse files
Add isspace (#452)
* start impl isspace * add example * correction doc * skip in SDC_CONFIG_PIPELINE_SDC=1, not supported * correction doc Co-authored-by: Alexander Kalistratov <alexander.kalistratov@intel.com>
1 parent 923b23a commit 3782e01

File tree

3 files changed

+133
-0
lines changed

3 files changed

+133
-0
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# *****************************************************************************
2+
# Copyright (c) 2019, Intel Corporation All rights reserved.
3+
#
4+
# Redistribution and use in source and binary forms, with or without
5+
# modification, are permitted provided that the following conditions are met:
6+
#
7+
# Redistributions of source code must retain the above copyright notice,
8+
# this list of conditions and the following disclaimer.
9+
#
10+
# Redistributions in binary form must reproduce the above copyright notice,
11+
# this list of conditions and the following disclaimer in the documentation
12+
# and/or other materials provided with the distribution.
13+
#
14+
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
15+
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
16+
# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17+
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
18+
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19+
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20+
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
21+
# OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
22+
# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23+
# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
24+
# EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25+
# *****************************************************************************
26+
27+
import pandas as pd
28+
from numba import njit
29+
30+
31+
@njit
32+
def series_str_isspace():
33+
series = pd.Series([' ', ' c ', ' b ', ' a '])
34+
out_series = series.str.isspace()
35+
36+
return out_series # Expect series of True, False, False, False
37+
38+
39+
print(series_str_isspace())

sdc/datatypes/hpat_pandas_stringmethods_functions.py

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -991,6 +991,84 @@ def hpat_pandas_stringmethods_istitle_impl(self):
991991
return hpat_pandas_stringmethods_istitle_impl
992992

993993

994+
@overload_method(StringMethodsType, 'isspace')
995+
def hpat_pandas_stringmethods_isspace(self):
996+
"""
997+
Intel Scalable Dataframe Compiler User Guide
998+
********************************************
999+
Pandas API: pandas.Series.str.isspace
1000+
1001+
Limitations
1002+
-----------
1003+
Series elements are expected to be Unicode strings. Elements cannot be NaN.
1004+
1005+
Examples
1006+
--------
1007+
.. literalinclude:: ../../../examples/series/str/series_str_isspace.py
1008+
:language: python
1009+
:lines: 27-
1010+
:caption: Check if all the characters in the text are whitespaces
1011+
:name: ex_series_str_isspace
1012+
1013+
.. command-output:: python ./series/str/series_str_isspace.py
1014+
:cwd: ../../../examples
1015+
1016+
.. seealso::
1017+
:ref:`Series.str.isalpha <pandas.Series.str.isalpha>`
1018+
Check whether all characters are alphabetic.
1019+
:ref:`Series.str.isnumeric <pandas.Series.str.isnumeric>`
1020+
Check whether all characters are numeric.
1021+
:ref:`Series.str.isalnum <pandas.Series.str.isalnum>`
1022+
Check whether all characters are alphanumeric.
1023+
:ref:`Series.str.isdigit <pandas.Series.str.isdigit>`
1024+
Check whether all characters are digits.
1025+
:ref:`Series.str.isdecimal <pandas.Series.str.isdecimal>`
1026+
Check whether all characters are decimal.
1027+
:ref:`Series.str.isspace <pandas.Series.str.isspace>`
1028+
Check whether all characters are whitespace.
1029+
:ref:`Series.str.islower <pandas.Series.str.islower>`
1030+
Check whether all characters are lowercase.
1031+
:ref:`Series.str.isupper <pandas.Series.str.isupper>`
1032+
Check whether all characters are uppercase.
1033+
:ref:`Series.str.istitle <pandas.Series.str.istitle>`
1034+
Check whether all characters are titlecase.
1035+
1036+
Intel Scalable Dataframe Compiler Developer Guide
1037+
*************************************************
1038+
1039+
Pandas Series method :meth:`pandas.core.strings.StringMethods.isspace()` implementation.
1040+
1041+
Note: Unicode type of list elements are supported only. Numpy.NaN is not supported as elements.
1042+
1043+
.. only:: developer
1044+
1045+
Test: python -m sdc.runtests sdc.tests.test_series.TestSeries.test_series_isspace_str
1046+
1047+
Parameters
1048+
----------
1049+
self: :class:`pandas.core.strings.StringMethods`
1050+
input arg
1051+
1052+
Returns
1053+
-------
1054+
:obj:`pandas.Series`
1055+
returns :obj:`pandas.Series` object
1056+
"""
1057+
1058+
ty_checker = TypeChecker('Method isspace().')
1059+
ty_checker.check(self, StringMethodsType)
1060+
1061+
def hpat_pandas_stringmethods_isspace_impl(self):
1062+
item_count = len(self._data)
1063+
result = numpy.empty(item_count, numba.types.boolean)
1064+
for idx, item in enumerate(self._data._data):
1065+
result[idx] = item.isspace()
1066+
1067+
return pandas.Series(result, self._data._index, name=self._data._name)
1068+
1069+
return hpat_pandas_stringmethods_isspace_impl
1070+
1071+
9941072
# _hpat_pandas_stringmethods_autogen_methods = sorted(dir(numba.types.misc.UnicodeType.__getattribute__.__qualname__))
9951073
_hpat_pandas_stringmethods_autogen_methods = ['upper', 'lower', 'lstrip', 'rstrip', 'strip']
9961074
"""

sdc/tests/test_series.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,10 @@ def istitle_usecase(series):
230230
return series.str.istitle()
231231

232232

233+
def isspace_usecase(series):
234+
return series.str.isspace()
235+
236+
233237
GLOBAL_VAL = 2
234238

235239

@@ -5492,6 +5496,18 @@ def test_series_istitle_str(self):
54925496
cfunc = self.jit(istitle_usecase)
54935497
pd.testing.assert_series_equal(cfunc(series), istitle_usecase(series))
54945498

5499+
@skip_sdc_jit("Series.str.isspace is not supported yet")
5500+
def test_series_isspace_str(self):
5501+
series = [['', ' ', ' ', ' '],
5502+
['', ' c ', ' b ', ' a '],
5503+
['aaaaaa', 'bb', 'c', ' d']
5504+
]
5505+
5506+
cfunc = self.jit(isspace_usecase)
5507+
for ser in series:
5508+
S = pd.Series(ser)
5509+
pd.testing.assert_series_equal(cfunc(S), isspace_usecase(S))
5510+
54955511

54965512
if __name__ == "__main__":
54975513
unittest.main()

0 commit comments

Comments
 (0)