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

Commit

Permalink
Minor changes for series.var()
Browse files Browse the repository at this point in the history
  • Loading branch information
densmirn committed Oct 16, 2019
1 parent dabf1c4 commit 9f57b16
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 18 deletions.
11 changes: 6 additions & 5 deletions hpat/datatypes/hpat_pandas_series_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ def hpat_pandas_series_values_impl(self):


@overload_method(SeriesType, 'var')
def hapt_pandas_series_var(self, axis=None, skipna=None, level=None, ddof=1, numeric_only=None):
def hpat_pandas_series_var(self, axis=None, skipna=None, level=None, ddof=1, numeric_only=None):
"""
Pandas Series method :meth:`pandas.Series.var` implementation.
Expand All @@ -215,8 +215,8 @@ def hapt_pandas_series_var(self, axis=None, skipna=None, level=None, ddof=1, num
input series
axis: :obj:`int`, :obj:`str`
Axis along which the operation acts
0/None - row-wise operation
1 - column-wise operation
0/None/'index' - row-wise operation
1/'columns' - column-wise operation
*unsupported*
skipna: :obj:`bool`
exclude NA/null values
Expand Down Expand Up @@ -245,8 +245,9 @@ def hapt_pandas_series_var(self, axis=None, skipna=None, level=None, ddof=1, num
if not isinstance(self, SeriesType):
raise TypingError('{} The object must be a pandas.series. Given: {}'.format(_func_name, self))

if not isinstance(self.dtype, types.Number):
raise TypingError('{} The object must be a number. Given self.dtype: {}'.format(_func_name, self.dtype))
if not isinstance(self.data.dtype, types.Number):
msg = '{} The object must be a number. Given self.data.dtype: {}'
raise TypingError(msg.format(_func_name, self.data.dtype))

if not isinstance(skipna, (types.Omitted, types.Boolean, types.NoneType)) and skipna is not None:
raise TypingError('{} The object must be a boolean. Given skipna: {}'.format(_func_name, skipna))
Expand Down
50 changes: 37 additions & 13 deletions hpat/tests/test_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,28 @@
),
]]

FLOAT_EXAMPLES = [
[1.0, np.nan, -1.0, 0.0, 5e-324],
[np.nan, np.inf, np.NINF, np.NZERO]
]
FLOAT_EXAMPLE, *_ = FLOAT_EXAMPLES
INT_EXAMPLE = [1, -1, 0, 18446744073709551615]
NUM_EXAMPLES = [INT_EXAMPLE] + FLOAT_EXAMPLES

UNICODE_EXAMPLES = [
'ascii',
'12345',
'1234567890',
'¡Y tú quién te crees?',
'🐍⚡',
'大处着眼,小处着手。',
]

STR_EXAMPLES = [
['', 'a' 'aa', 'aaa', 'b', 'aab', 'ab', 'abababab'],
UNICODE_EXAMPLES
]


def _make_func_from_text(func_text, func_name='test_impl'):
loc_vars = {}
Expand Down Expand Up @@ -2103,7 +2125,7 @@ def test_series_nunique_param1_impl(S, dropna):

def test_series_var(self):
def pyfunc():
series = pd.Series([1.3, -2.7, np.nan, 0.1, 10.9])
series = pd.Series([1.0, np.nan, -1.0, 0.0, 5e-324])
return series.var()

cfunc = hpat.jit(pyfunc)
Expand All @@ -2116,30 +2138,32 @@ def pyfunc(series, skipna, ddof):
return series.var(skipna=skipna, ddof=ddof)

cfunc = hpat.jit(pyfunc)
series = pd.Series([1.3, -2.7, np.nan, 0.1, 10.9])
for ddof in [0, 1]:
for skipna in [True, False]:
ref_result = pyfunc(series, skipna=skipna, ddof=ddof)
result = cfunc(series, skipna=skipna, ddof=ddof)
np.testing.assert_equal(ref_result, result)
for data in FLOAT_EXAMPLES:
series = pd.Series(data)
for ddof in [0, 1]:
for skipna in [True, False]:
ref_result = pyfunc(series, skipna=skipna, ddof=ddof)
result = cfunc(series, skipna=skipna, ddof=ddof)
np.testing.assert_equal(ref_result, result)

def test_series_var_str(self):
def pyfunc(series):
return series.var()

cfunc = hpat.jit(pyfunc)
series = pd.Series(['test', 'series', 'var', 'str'])
with self.assertRaises(TypingError) as raises:
cfunc(series)
msg = 'Method var(). The object must be a number. Given self.dtype: {}'
self.assertIn(msg.format(types.unicode_type), str(raises.exception))
for data in STR_EXAMPLES:
series = pd.Series(data)
with self.assertRaises(TypingError) as raises:
cfunc(series)
msg = 'Method var(). The object must be a number. Given self.data.dtype: {}'
self.assertIn(msg.format(types.unicode_type), str(raises.exception))

def test_series_var_unsupported_params(self):
def pyfunc(series, axis, level, numeric_only):
return series.var(axis=axis, level=level, numeric_only=numeric_only)

cfunc = hpat.jit(pyfunc)
series = pd.Series([1.3, -2.7, np.nan, 0.1, 10.9])
series = pd.Series(FLOAT_EXAMPLE)
msg = 'Method var(). Unsupported parameters. Given {}: {}'
with self.assertRaises(TypingError) as raises:
cfunc(series, axis=1, level=None, numeric_only=None)
Expand Down

0 comments on commit 9f57b16

Please sign in to comment.