Skip to content
This repository was archived by the owner on Feb 2, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions examples/series/series_skew.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# *****************************************************************************
# Copyright (c) 2020, Intel Corporation All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
#
# Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
# OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
# EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
# *****************************************************************************

import pandas as pd
import numpy as np
from numba import njit


@njit
def series_skew():
s = pd.Series([np.nan, -2., 3., 5.0])

return s.skew() # Expect -1.1520696383139375


print(series_skew())
86 changes: 86 additions & 0 deletions sdc/datatypes/hpat_pandas_series_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -5725,3 +5725,89 @@ def sdc_pandas_series_groupby_impl(self, by=None, axis=0, level=None, as_index=T
return init_series_groupby(self, by, grouped, sort)

return sdc_pandas_series_groupby_impl


@sdc_overload_method(SeriesType, 'skew')
def sdc_pandas_series_skew(self, axis=None, skipna=None, level=None, numeric_only=None):
"""
Intel Scalable Dataframe Compiler User Guide
********************************************

Pandas API: pandas.Series.skew

Limitations
-----------
- Parameters ``level`` and ``numeric_only`` are supported only with default value ``None``.

Examples
--------
.. literalinclude:: ../../../examples/series/series_skew.py
:language: python
:lines: 27-
:caption: Unbiased rolling skewness.
:name: ex_series_skew

.. command-output:: python ./series/series_skew.py
:cwd: ../../../examples

Intel Scalable Dataframe Compiler Developer Guide
*************************************************
Pandas Series method :meth:`pandas.Series.skew` implementation.

.. only:: developer
Test: python -m sdc.runtests -k sdc.tests.test_series.TestSeries.test_series_skew*
"""
_func_name = 'Method Series.skew()'

ty_checker = TypeChecker(_func_name)
ty_checker.check(self, SeriesType)

if not isinstance(axis, (types.Integer, types.NoneType, types.Omitted)) and axis is not None:
ty_checker.raise_exc(axis, 'int64', 'axis')

if not isinstance(skipna, (types.Boolean, types.NoneType, types.Omitted)) and skipna is not None:
ty_checker.raise_exc(skipna, 'bool', 'skipna')

if not isinstance(level, (types.Omitted, types.NoneType)) and level is not None:
ty_checker.raise_exc(level, 'None', 'level')

if not isinstance(numeric_only, (types.Omitted, types.NoneType)) and numeric_only is not None:
ty_checker.raise_exc(numeric_only, 'None', 'numeric_only')

def sdc_pandas_series_skew_impl(self, axis=None, skipna=None, level=None, numeric_only=None):
if axis != 0 and axis is not None:
raise ValueError('Parameter axis must be only 0 or None.')

if skipna is None:
_skipna = True
else:
_skipna = skipna

infinite_mask = numpy.isfinite(self._data)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No. that's kills performance and scalability

len_val = len(infinite_mask)
data = self._data[infinite_mask]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's too, actually

nfinite = len(data)

if not _skipna and nfinite < len_val:
return numpy.nan
else:
_sum = 0
square_sum = 0
cube_sum = 0

for i in numba.prange(nfinite):
_sum += data[i]
square_sum += data[i] ** 2
cube_sum += data[i] ** 3

n = nfinite
m2 = (square_sum - _sum * _sum / n) / n
m3 = (cube_sum - 3. * _sum * square_sum / n + 2. * _sum * _sum * _sum / n / n) / n
res = numpy.nan if m2 == 0 else m3 / m2 ** 1.5

if (n > 2) & (m2 > 0):
res = numpy.sqrt((n - 1.) * n) / (n - 2.) * m3 / m2 ** 1.5

return res

return sdc_pandas_series_skew_impl
56 changes: 56 additions & 0 deletions sdc/tests/test_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -7111,6 +7111,62 @@ def test_impl(A, idx):
msg = 'The index of boolean indexer is not comparable to Series index.'
self.assertIn(msg, str(raises.exception))

def test_series_skew(self):
def test_impl(series, axis, skipna):
return series.skew(axis=axis, skipna=skipna)

hpat_func = self.jit(test_impl)
for data in test_global_input_data_numeric:
with self.subTest(data=data):
s = pd.Series(data)
for axis in [0, None]:
with self.subTest(axis=axis):
for skipna in [None, False, True]:
with self.subTest(skipna=skipna):
res1 = test_impl(s, axis, skipna)
res2 = hpat_func(s, axis, skipna)
np.testing.assert_allclose(res1, res2)

def test_series_skew_default(self):
def test_impl():
s = pd.Series([np.nan, -2., 3., 9.1])
return s.skew()

hpat_func = self.jit(test_impl)
np.testing.assert_allclose(test_impl(), hpat_func())

def test_series_skew_not_supported(self):
def test_impl(series, axis=None, skipna=None, level=None, numeric_only=None):
return series.skew(axis=axis, skipna=skipna, level=level, numeric_only=numeric_only)

hpat_func = self.jit(test_impl)
s = pd.Series([1.1, 0.3, np.nan, 1, np.inf, 0, 1.1, np.nan, 2.2, np.inf, 2, 2])

with self.assertRaises(TypingError) as raises:
hpat_func(s, axis=0.75)
msg = 'TypingError: Method Series.skew() The object axis\n given: float64\n expected: int64'
self.assertIn(msg, str(raises.exception))

with self.assertRaises(TypingError) as raises:
hpat_func(s, skipna=0)
msg = 'TypingError: Method Series.skew() The object skipna\n given: int64\n expected: bool'
self.assertIn(msg, str(raises.exception))

with self.assertRaises(TypingError) as raises:
hpat_func(s, level=0)
msg = 'TypingError: Method Series.skew() The object level\n given: int64\n expected: None'
self.assertIn(msg, str(raises.exception))

with self.assertRaises(TypingError) as raises:
hpat_func(s, numeric_only=0)
msg = 'TypingError: Method Series.skew() The object numeric_only\n given: int64\n expected: None'
self.assertIn(msg, str(raises.exception))

with self.assertRaises(ValueError) as raises:
hpat_func(s, axis=5)
msg = 'Parameter axis must be only 0 or None.'
self.assertIn(msg, str(raises.exception))


if __name__ == "__main__":
unittest.main()
1 change: 1 addition & 0 deletions sdc/tests/tests_perf/test_perf_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ def _test_case(self, pyfunc, name, total_data_length, input_data=None, data_num=
TC(name='shape', size=[10 ** 7], call_expr='data.shape', usecase_params='data'),
TC(name='shift', size=[10 ** 8]),
TC(name='size', size=[10 ** 7], call_expr='data.size', usecase_params='data'),
TC(name='skew', size=[10 ** 8]),
TC(name='sort_values', size=[10 ** 5]),
TC(name='std', size=[10 ** 7], check_skipna=True),
TC(name='sub', size=[10 ** 7], params='other', data_num=2),
Expand Down