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

Commit 58f2858

Browse files
densmirnAlexanderKalistratov
authored andcommitted
Overload df.rolling.kurt() (#475)
* Overload df.rolling.kurt() * Add perf.test for df.rolling.kurt()
1 parent 3e72cc5 commit 58f2858

File tree

4 files changed

+86
-12
lines changed

4 files changed

+86
-12
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# *****************************************************************************
2+
# Copyright (c) 2020, 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 df_rolling_kurt():
33+
df = pd.DataFrame({'A': [4, 3, 5, 2, 6], 'B': [-4, -3, -5, -2, -6]})
34+
out_df = df.rolling(4).kurt()
35+
36+
# Expect DataFrame of
37+
# {'A': [NaN, NaN, NaN, -1.2, -3.3], 'B': [NaN, NaN, NaN, -1.2, -3.3]}
38+
return out_df
39+
40+
41+
print(df_rolling_kurt())

sdc/datatypes/hpat_pandas_dataframe_rolling_functions.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,15 @@ def sdc_pandas_dataframe_rolling_count(self):
149149
return gen_df_rolling_method_impl('count', self)
150150

151151

152+
@sdc_overload_method(DataFrameRollingType, 'kurt')
153+
def sdc_pandas_dataframe_rolling_kurt(self):
154+
155+
ty_checker = TypeChecker('Method rolling.kurt().')
156+
ty_checker.check(self, DataFrameRollingType)
157+
158+
return gen_df_rolling_method_impl('kurt', self)
159+
160+
152161
@sdc_overload_method(DataFrameRollingType, 'max')
153162
def sdc_pandas_dataframe_rolling_max(self):
154163

@@ -194,6 +203,13 @@ def sdc_pandas_dataframe_rolling_min(self):
194203
'extra_params': ''
195204
})
196205

206+
sdc_pandas_dataframe_rolling_kurt.__doc__ = sdc_pandas_dataframe_rolling_docstring_tmpl.format(**{
207+
'method_name': 'kurt',
208+
'example_caption': 'Calculate unbiased rolling kurtosis.',
209+
'limitations_block': '',
210+
'extra_params': ''
211+
})
212+
197213
sdc_pandas_dataframe_rolling_max.__doc__ = sdc_pandas_dataframe_rolling_docstring_tmpl.format(**{
198214
'method_name': 'max',
199215
'example_caption': 'Calculate the rolling maximum.',

sdc/tests/test_rolling.py

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -583,6 +583,21 @@ def test_impl(obj, window, min_periods):
583583
ref_result = test_impl(obj, window, min_periods)
584584
assert_equal(jit_result, ref_result)
585585

586+
def _test_rolling_kurt(self, obj):
587+
def test_impl(obj, window, min_periods):
588+
return obj.rolling(window, min_periods).kurt()
589+
590+
hpat_func = self.jit(test_impl)
591+
assert_equal = self._get_assert_equal(obj)
592+
593+
for window in range(4, len(obj) + 1):
594+
for min_periods in range(window + 1):
595+
with self.subTest(obj=obj, window=window,
596+
min_periods=min_periods):
597+
ref_result = test_impl(obj, window, min_periods)
598+
jit_result = hpat_func(obj, window, min_periods)
599+
assert_equal(jit_result, ref_result)
600+
586601
def _test_rolling_max(self, obj):
587602
def test_impl(obj, window, min_periods):
588603
return obj.rolling(window, min_periods).max()
@@ -677,6 +692,15 @@ def test_df_rolling_count(self):
677692

678693
self._test_rolling_count(df)
679694

695+
@skip_sdc_jit('DataFrame.rolling.kurt() unsupported')
696+
def test_df_rolling_kurt(self):
697+
all_data = test_global_input_data_float64
698+
length = min(len(d) for d in all_data)
699+
data = {n: d[:length] for n, d in zip(string.ascii_uppercase, all_data)}
700+
df = pd.DataFrame(data)
701+
702+
self._test_rolling_kurt(df)
703+
680704
@skip_sdc_jit('DataFrame.rolling.max() unsupported')
681705
def test_df_rolling_max(self):
682706
all_data = test_global_input_data_float64
@@ -899,22 +923,11 @@ def test_impl(pairwise, ddof):
899923

900924
@skip_sdc_jit('Series.rolling.kurt() unsupported Series index')
901925
def test_series_rolling_kurt(self):
902-
def test_impl(series, window, min_periods):
903-
return series.rolling(window, min_periods).kurt()
904-
905-
hpat_func = self.jit(test_impl)
906-
907926
all_data = test_global_input_data_float64
908927
indices = [list(range(len(data)))[::-1] for data in all_data]
909928
for data, index in zip(all_data, indices):
910929
series = pd.Series(data, index, name='A')
911-
for window in range(4, len(series) + 1):
912-
for min_periods in range(window + 1):
913-
with self.subTest(series=series, window=window,
914-
min_periods=min_periods):
915-
ref_result = test_impl(series, window, min_periods)
916-
jit_result = hpat_func(series, window, min_periods)
917-
pd.testing.assert_series_equal(jit_result, ref_result)
930+
self._test_rolling_kurt(series)
918931

919932
@skip_sdc_jit('Series.rolling.max() unsupported Series index')
920933
def test_series_rolling_max(self):

sdc/tests/tests_perf/test_perf_df_rolling.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ def setUpClass(cls):
8686
cls.total_data_length = {
8787
'apply': [2 * 10 ** 5],
8888
'count': [8 * 10 ** 5],
89+
'kurt': [4 * 10 ** 5],
8990
'max': [2 * 10 ** 5],
9091
'min': [2 * 10 ** 5],
9192
}
@@ -144,6 +145,9 @@ def test_df_rolling_apply_mean(self):
144145
def test_df_rolling_count(self):
145146
self._test_df_rolling_method('count')
146147

148+
def test_df_rolling_kurt(self):
149+
self._test_df_rolling_method('kurt')
150+
147151
def test_df_rolling_max(self):
148152
self._test_df_rolling_method('max')
149153

0 commit comments

Comments
 (0)