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

Commit 32e1af1

Browse files
RubtsowaAlexanderKalistratov
authored andcommitted
Add istitle (#439)
* start Impl * Impl method Istitle * correction issues PEP8 * delete excess print * add example * correction doc * skip in SDC_CONFIG_PIPELINE_SDC=1, not supported * delete comment * add see also * add test with None and NaN (skip, not work) * correction doc * correction doc
1 parent 9b5c64b commit 32e1af1

3 files changed

Lines changed: 136 additions & 0 deletions

File tree

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_istitle():
33+
series = pd.Series(['Cat', 'dog', 'Bird'])
34+
out_series = series.str.istitle()
35+
36+
return out_series # Expect series of True, False, True
37+
38+
39+
print(series_str_istitle())

sdc/datatypes/hpat_pandas_stringmethods_functions.py

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -913,6 +913,84 @@ def _hpat_pandas_stringmethods_autogen(method_name):
913913
return _hpat_pandas_stringmethods_autogen_global_dict[global_dict_name]
914914

915915

916+
@overload_method(StringMethodsType, 'istitle')
917+
def hpat_pandas_stringmethods_istitle(self):
918+
"""
919+
Intel Scalable Dataframe Compiler User Guide
920+
********************************************
921+
Pandas API: pandas.Series.str.istitle
922+
923+
Limitations
924+
-----------
925+
Series elements are expected to be Unicode strings. Elements cannot be NaN.
926+
927+
Examples
928+
--------
929+
.. literalinclude:: ../../../examples/series/str/series_str_istitle.py
930+
:language: python
931+
:lines: 27-
932+
:caption: Check if each word start with an upper case letter
933+
:name: ex_series_str_istitle
934+
935+
.. command-output:: python ./series/str/series_str_istitle.py
936+
:cwd: ../../../examples
937+
938+
.. seealso::
939+
:ref:`Series.str.isalpha <pandas.Series.str.isalpha>`
940+
Check whether all characters are alphabetic.
941+
:ref:`Series.str.isnumeric <pandas.Series.str.isnumeric>`
942+
Check whether all characters are numeric.
943+
:ref:`Series.str.isalnum <pandas.Series.str.isalnum>`
944+
Check whether all characters are alphanumeric.
945+
:ref:`Series.str.isdigit <pandas.Series.str.isdigit>`
946+
Check whether all characters are digits.
947+
:ref:`Series.str.isdecimal <pandas.Series.str.isdecimal>`
948+
Check whether all characters are decimal.
949+
:ref:`Series.str.isspace <pandas.Series.str.isspace>`
950+
Check whether all characters are whitespace.
951+
:ref:`Series.str.islower <pandas.Series.str.islower>`
952+
Check whether all characters are lowercase.
953+
:ref:`Series.str.isupper <pandas.Series.str.isupper>`
954+
Check whether all characters are uppercase.
955+
:ref:`Series.str.istitle <pandas.Series.str.istitle>`
956+
Check whether all characters are titlecase.
957+
958+
Intel Scalable Dataframe Compiler Developer Guide
959+
*************************************************
960+
961+
Pandas Series method :meth:`pandas.core.strings.StringMethods.istitle()` implementation.
962+
963+
Note: Unicode type of list elements are supported only. Numpy.NaN is not supported as elements.
964+
965+
.. only:: developer
966+
967+
Test: python -m sdc.runtests sdc.tests.test_series.TestSeries.test_series_istitle_str
968+
969+
Parameters
970+
----------
971+
self: :class:`pandas.core.strings.StringMethods`
972+
input arg
973+
974+
Returns
975+
-------
976+
:obj:`pandas.Series`
977+
returns :obj:`pandas.Series` object
978+
"""
979+
980+
ty_checker = TypeChecker('Method istitle().')
981+
ty_checker.check(self, StringMethodsType)
982+
983+
def hpat_pandas_stringmethods_istitle_impl(self):
984+
item_count = len(self._data)
985+
result = numpy.empty(item_count, numba.types.boolean)
986+
for idx, item in enumerate(self._data._data):
987+
result[idx] = item.istitle()
988+
989+
return pandas.Series(result, self._data._index, name=self._data._name)
990+
991+
return hpat_pandas_stringmethods_istitle_impl
992+
993+
916994
# _hpat_pandas_stringmethods_autogen_methods = sorted(dir(numba.types.misc.UnicodeType.__getattribute__.__qualname__))
917995
_hpat_pandas_stringmethods_autogen_methods = ['upper', 'lower', 'lstrip', 'rstrip', 'strip']
918996
"""

sdc/tests/test_series.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,10 @@ def rjust_with_fillchar_usecase(series, width, fillchar):
226226
return series.str.rjust(width, fillchar)
227227

228228

229+
def istitle_usecase(series):
230+
return series.str.istitle()
231+
232+
229233
GLOBAL_VAL = 2
230234

231235

@@ -5285,6 +5289,21 @@ def test_impl(A, B):
52855289
B = pd.Series(['b', 'aa', '', 'b', 'o', None, 'oo'])
52865290
pd.testing.assert_series_equal(hpat_func(A, B), test_impl(A, B))
52875291

5292+
@skip_sdc_jit("Series.str.istitle is not supported yet")
5293+
def test_series_istitle_str(self):
5294+
series = pd.Series(['Cat', 'dog', 'Bird'])
5295+
5296+
cfunc = self.jit(istitle_usecase)
5297+
pd.testing.assert_series_equal(cfunc(series), istitle_usecase(series))
5298+
5299+
@skip_sdc_jit("Series.str.istitle is not supported yet")
5300+
@skip_numba_jit("Not work with None and np.nan")
5301+
def test_series_istitle_str(self):
5302+
series = pd.Series(['Cat', 'dog', 'Bird', None, np.nan])
5303+
5304+
cfunc = self.jit(istitle_usecase)
5305+
pd.testing.assert_series_equal(cfunc(series), istitle_usecase(series))
5306+
52885307

52895308
if __name__ == "__main__":
52905309
unittest.main()

0 commit comments

Comments
 (0)