From 5ea6970d7afb688381889d62ec63e5e43985d3f4 Mon Sep 17 00:00:00 2001 From: "Kozlov, Alexey" Date: Wed, 10 Jun 2020 03:13:24 +0300 Subject: [PATCH 1/2] Updating numpy_like.fillna to handle default fill value --- sdc/functions/numpy_like.py | 75 ++++--- sdc/sdc_autogenerated.py | 361 ++++++++++++++-------------------- sdc/sdc_function_templates.py | 51 ++--- sdc/tests/test_sdc_numpy.py | 99 ++++++++++ 4 files changed, 316 insertions(+), 270 deletions(-) diff --git a/sdc/functions/numpy_like.py b/sdc/functions/numpy_like.py index 3e950cae9..c2c173db4 100644 --- a/sdc/functions/numpy_like.py +++ b/sdc/functions/numpy_like.py @@ -528,26 +528,61 @@ def sdc_fillna_overload(self, inplace=False, value=None): dtype = self.dtype isnan = get_isnan(dtype) + if ( (isinstance(inplace, types.Literal) and inplace.literal_value == True) or # noqa (isinstance(inplace, bool) and inplace == True) # noqa ): + + def sdc_fillna_inplace_noop(self, inplace=False, value=None): + return None + + if isinstance(value, (types.NoneType, types.Omitted)) or value is None: + return sdc_fillna_inplace_noop + if isinstance(dtype, (types.Integer, types.Boolean)): - def sdc_fillna_inplace_int_impl(self, inplace=False, value=None): + return sdc_fillna_inplace_noop + + if isinstance(dtype, types.Float): + def sdc_fillna_inplace_float_impl(self, inplace=False, value=None): + _value = np.nan if value is None else value + length = len(self) + for i in prange(length): + if isnan(self[i]): + self[i] = _value return None - return sdc_fillna_inplace_int_impl + return sdc_fillna_inplace_float_impl - def sdc_fillna_inplace_float_impl(self, inplace=False, value=None): - length = len(self) - for i in prange(length): - if isnan(self[i]): - self[i] = value + if isinstance(dtype, types.UnicodeType): + # TO-DO: not supported, since no generic setitem for StringArray return None - return sdc_fillna_inplace_float_impl - else: + + def sdc_fillna_noop(self, inplace=False, value=None): + return copy(self) + + if isinstance(value, (types.NoneType, types.Omitted)) or value is None: + return sdc_fillna_noop + + if isinstance(dtype, (types.Integer, types.Boolean)): + return sdc_fillna_noop + + if isinstance(dtype, types.Float): + def sdc_fillna_impl(self, inplace=False, value=None): + _value = np.nan if value is None else value + length = len(self) + filled_data = numpy.empty(length, dtype=dtype) + for i in prange(length): + if isnan(self[i]): + filled_data[i] = _value + else: + filled_data[i] = self[i] + return filled_data + + return sdc_fillna_impl + if isinstance(self.dtype, types.UnicodeType): def sdc_fillna_str_impl(self, inplace=False, value=None): n = len(self) @@ -556,9 +591,9 @@ def sdc_fillna_str_impl(self, inplace=False, value=None): for i in prange(n): s = self[i] if sdc.hiframes.api.isna(self, i): - num_chars += len(value) + num_chars += get_utf8_size(value) else: - num_chars += len(s) + num_chars += get_utf8_size(s) filled_data = pre_alloc_string_array(n, num_chars) for i in prange(n): @@ -570,24 +605,6 @@ def sdc_fillna_str_impl(self, inplace=False, value=None): return sdc_fillna_str_impl - if isinstance(dtype, (types.Integer, types.Boolean)): - def sdc_fillna_int_impl(self, inplace=False, value=None): - return copy(self) - - return sdc_fillna_int_impl - - def sdc_fillna_impl(self, inplace=False, value=None): - length = len(self) - filled_data = numpy.empty(length, dtype=dtype) - for i in prange(length): - if isnan(self[i]): - filled_data[i] = value - else: - filled_data[i] = self[i] - return filled_data - - return sdc_fillna_impl - def nanmin(a): pass diff --git a/sdc/sdc_autogenerated.py b/sdc/sdc_autogenerated.py index 965d3636b..9abffdaf0 100644 --- a/sdc/sdc_autogenerated.py +++ b/sdc/sdc_autogenerated.py @@ -79,7 +79,8 @@ def sdc_pandas_series_add(self, other, level=None, fill_value=None, axis=0): Test: python -m sdc.runtests sdc.tests.test_series.TestSeries.test_series_op5 """ - ty_checker = TypeChecker('Method add().') + _func_name = 'Method add().' + ty_checker = TypeChecker(_func_name) self_is_series, other_is_series = isinstance(self, SeriesType), isinstance(other, SeriesType) if not (self_is_series or other_is_series): return None @@ -115,23 +116,20 @@ def sdc_pandas_series_add(self, other, level=None, fill_value=None, axis=0): if not isinstance(fill_value, (types.Omitted, types.Number, types.NoneType)) and fill_value is not None: ty_checker.raise_exc(fill_value, 'number', 'fill_value') + fill_value_is_none = isinstance(fill_value, (types.NoneType, types.Omitted)) or fill_value is None if not isinstance(axis, types.Omitted) and axis != 0: ty_checker.raise_exc(axis, 'int', 'axis') - fill_value_is_none = isinstance(fill_value, (types.NoneType, types.Omitted)) or fill_value is None # specializations for numeric series only if not operands_are_series: def _series_add_scalar_impl(self, other, level=None, fill_value=None, axis=0): - _fill_value = numpy.nan if fill_value_is_none == True else fill_value # noqa if self_is_series == True: # noqa - if not (fill_value is None or numpy.isnan(fill_value)): - numpy_like.fillna(self._data, inplace=True, value=_fill_value) + numpy_like.fillna(self._data, inplace=True, value=fill_value) result_data = numpy.empty(len(self._data), dtype=numpy.float64) result_data[:] = self._data + numpy.float64(other) return pandas.Series(result_data, index=self._index, name=self._name) else: - if not (fill_value is None or numpy.isnan(fill_value)): - numpy_like.fillna(other._data, inplace=True, value=_fill_value) + numpy_like.fillna(other._data, inplace=True, value=fill_value) result_data = numpy.empty(len(other._data), dtype=numpy.float64) result_data[:] = numpy.float64(self) + other._data return pandas.Series(result_data, index=other._index, name=other._name) @@ -142,10 +140,8 @@ def _series_add_scalar_impl(self, other, level=None, fill_value=None, axis=0): # optimization for series with default indexes, that can be aligned differently if (isinstance(self.index, types.NoneType) and isinstance(other.index, types.NoneType)): def _series_add_none_indexes_impl(self, other, level=None, fill_value=None, axis=0): - _fill_value = numpy.nan if fill_value_is_none == True else fill_value # noqa - if not (fill_value is None or numpy.isnan(fill_value)): - numpy_like.fillna(self._data, inplace=True, value=_fill_value) - numpy_like.fillna(other._data, inplace=True, value=_fill_value) + numpy_like.fillna(self._data, inplace=True, value=fill_value) + numpy_like.fillna(other._data, inplace=True, value=fill_value) if (len(self._data) == len(other._data)): result_data = numpy_like.astype(self._data, numpy.float64) @@ -156,6 +152,7 @@ def _series_add_none_indexes_impl(self, other, level=None, fill_value=None, axis min_data_size = min(left_size, right_size) max_data_size = max(left_size, right_size) result_data = numpy.empty(max_data_size, dtype=numpy.float64) + _fill_value = numpy.nan if fill_value_is_none == True else fill_value # noqa if (left_size == min_data_size): result_data[:min_data_size] = self._data for i in range(min_data_size, len(result_data)): @@ -180,10 +177,8 @@ def _series_add_none_indexes_impl(self, other, level=None, fill_value=None, axis def _series_add_common_impl(self, other, level=None, fill_value=None, axis=0): left_index, right_index = self.index, other.index - _fill_value = numpy.nan if fill_value_is_none == True else fill_value # noqa - if not (fill_value is None or numpy.isnan(fill_value)): - numpy_like.fillna(self._data, inplace=True, value=_fill_value) - numpy_like.fillna(other._data, inplace=True, value=_fill_value) + numpy_like.fillna(self._data, inplace=True, value=fill_value) + numpy_like.fillna(other._data, inplace=True, value=fill_value) # check if indexes are equal and series don't have to be aligned if sdc_check_indexes_equal(left_index, right_index): result_data = numpy.empty(len(self._data), dtype=numpy.float64) @@ -201,6 +196,7 @@ def _series_add_common_impl(self, other, level=None, fill_value=None, axis=0): result_size = len(joined_index) left_values = numpy.empty(result_size, dtype=numpy.float64) right_values = numpy.empty(result_size, dtype=numpy.float64) + _fill_value = numpy.nan if fill_value_is_none == True else fill_value # noqa for i in range(result_size): left_pos, right_pos = left_indexer[i], right_indexer[i] left_values[i] = self._data[left_pos] if left_pos != -1 else _fill_value @@ -244,7 +240,8 @@ def sdc_pandas_series_div(self, other, level=None, fill_value=None, axis=0): Test: python -m sdc.runtests sdc.tests.test_series.TestSeries.test_series_op5 """ - ty_checker = TypeChecker('Method div().') + _func_name = 'Method div().' + ty_checker = TypeChecker(_func_name) self_is_series, other_is_series = isinstance(self, SeriesType), isinstance(other, SeriesType) if not (self_is_series or other_is_series): return None @@ -280,23 +277,20 @@ def sdc_pandas_series_div(self, other, level=None, fill_value=None, axis=0): if not isinstance(fill_value, (types.Omitted, types.Number, types.NoneType)) and fill_value is not None: ty_checker.raise_exc(fill_value, 'number', 'fill_value') + fill_value_is_none = isinstance(fill_value, (types.NoneType, types.Omitted)) or fill_value is None if not isinstance(axis, types.Omitted) and axis != 0: ty_checker.raise_exc(axis, 'int', 'axis') - fill_value_is_none = isinstance(fill_value, (types.NoneType, types.Omitted)) or fill_value is None # specializations for numeric series only if not operands_are_series: def _series_div_scalar_impl(self, other, level=None, fill_value=None, axis=0): - _fill_value = numpy.nan if fill_value_is_none == True else fill_value # noqa if self_is_series == True: # noqa - if not (fill_value is None or numpy.isnan(fill_value)): - numpy_like.fillna(self._data, inplace=True, value=_fill_value) + numpy_like.fillna(self._data, inplace=True, value=fill_value) result_data = numpy.empty(len(self._data), dtype=numpy.float64) result_data[:] = self._data / numpy.float64(other) return pandas.Series(result_data, index=self._index, name=self._name) else: - if not (fill_value is None or numpy.isnan(fill_value)): - numpy_like.fillna(other._data, inplace=True, value=_fill_value) + numpy_like.fillna(other._data, inplace=True, value=fill_value) result_data = numpy.empty(len(other._data), dtype=numpy.float64) result_data[:] = numpy.float64(self) / other._data return pandas.Series(result_data, index=other._index, name=other._name) @@ -307,10 +301,8 @@ def _series_div_scalar_impl(self, other, level=None, fill_value=None, axis=0): # optimization for series with default indexes, that can be aligned differently if (isinstance(self.index, types.NoneType) and isinstance(other.index, types.NoneType)): def _series_div_none_indexes_impl(self, other, level=None, fill_value=None, axis=0): - _fill_value = numpy.nan if fill_value_is_none == True else fill_value # noqa - if not (fill_value is None or numpy.isnan(fill_value)): - numpy_like.fillna(self._data, inplace=True, value=_fill_value) - numpy_like.fillna(other._data, inplace=True, value=_fill_value) + numpy_like.fillna(self._data, inplace=True, value=fill_value) + numpy_like.fillna(other._data, inplace=True, value=fill_value) if (len(self._data) == len(other._data)): result_data = numpy_like.astype(self._data, numpy.float64) @@ -321,6 +313,7 @@ def _series_div_none_indexes_impl(self, other, level=None, fill_value=None, axis min_data_size = min(left_size, right_size) max_data_size = max(left_size, right_size) result_data = numpy.empty(max_data_size, dtype=numpy.float64) + _fill_value = numpy.nan if fill_value_is_none == True else fill_value # noqa if (left_size == min_data_size): result_data[:min_data_size] = self._data for i in range(min_data_size, len(result_data)): @@ -345,10 +338,8 @@ def _series_div_none_indexes_impl(self, other, level=None, fill_value=None, axis def _series_div_common_impl(self, other, level=None, fill_value=None, axis=0): left_index, right_index = self.index, other.index - _fill_value = numpy.nan if fill_value_is_none == True else fill_value # noqa - if not (fill_value is None or numpy.isnan(fill_value)): - numpy_like.fillna(self._data, inplace=True, value=_fill_value) - numpy_like.fillna(other._data, inplace=True, value=_fill_value) + numpy_like.fillna(self._data, inplace=True, value=fill_value) + numpy_like.fillna(other._data, inplace=True, value=fill_value) # check if indexes are equal and series don't have to be aligned if sdc_check_indexes_equal(left_index, right_index): result_data = numpy.empty(len(self._data), dtype=numpy.float64) @@ -366,6 +357,7 @@ def _series_div_common_impl(self, other, level=None, fill_value=None, axis=0): result_size = len(joined_index) left_values = numpy.empty(result_size, dtype=numpy.float64) right_values = numpy.empty(result_size, dtype=numpy.float64) + _fill_value = numpy.nan if fill_value_is_none == True else fill_value # noqa for i in range(result_size): left_pos, right_pos = left_indexer[i], right_indexer[i] left_values[i] = self._data[left_pos] if left_pos != -1 else _fill_value @@ -409,7 +401,8 @@ def sdc_pandas_series_sub(self, other, level=None, fill_value=None, axis=0): Test: python -m sdc.runtests sdc.tests.test_series.TestSeries.test_series_op5 """ - ty_checker = TypeChecker('Method sub().') + _func_name = 'Method sub().' + ty_checker = TypeChecker(_func_name) self_is_series, other_is_series = isinstance(self, SeriesType), isinstance(other, SeriesType) if not (self_is_series or other_is_series): return None @@ -445,23 +438,20 @@ def sdc_pandas_series_sub(self, other, level=None, fill_value=None, axis=0): if not isinstance(fill_value, (types.Omitted, types.Number, types.NoneType)) and fill_value is not None: ty_checker.raise_exc(fill_value, 'number', 'fill_value') + fill_value_is_none = isinstance(fill_value, (types.NoneType, types.Omitted)) or fill_value is None if not isinstance(axis, types.Omitted) and axis != 0: ty_checker.raise_exc(axis, 'int', 'axis') - fill_value_is_none = isinstance(fill_value, (types.NoneType, types.Omitted)) or fill_value is None # specializations for numeric series only if not operands_are_series: def _series_sub_scalar_impl(self, other, level=None, fill_value=None, axis=0): - _fill_value = numpy.nan if fill_value_is_none == True else fill_value # noqa if self_is_series == True: # noqa - if not (fill_value is None or numpy.isnan(fill_value)): - numpy_like.fillna(self._data, inplace=True, value=_fill_value) + numpy_like.fillna(self._data, inplace=True, value=fill_value) result_data = numpy.empty(len(self._data), dtype=numpy.float64) result_data[:] = self._data - numpy.float64(other) return pandas.Series(result_data, index=self._index, name=self._name) else: - if not (fill_value is None or numpy.isnan(fill_value)): - numpy_like.fillna(other._data, inplace=True, value=_fill_value) + numpy_like.fillna(other._data, inplace=True, value=fill_value) result_data = numpy.empty(len(other._data), dtype=numpy.float64) result_data[:] = numpy.float64(self) - other._data return pandas.Series(result_data, index=other._index, name=other._name) @@ -472,10 +462,8 @@ def _series_sub_scalar_impl(self, other, level=None, fill_value=None, axis=0): # optimization for series with default indexes, that can be aligned differently if (isinstance(self.index, types.NoneType) and isinstance(other.index, types.NoneType)): def _series_sub_none_indexes_impl(self, other, level=None, fill_value=None, axis=0): - _fill_value = numpy.nan if fill_value_is_none == True else fill_value # noqa - if not (fill_value is None or numpy.isnan(fill_value)): - numpy_like.fillna(self._data, inplace=True, value=_fill_value) - numpy_like.fillna(other._data, inplace=True, value=_fill_value) + numpy_like.fillna(self._data, inplace=True, value=fill_value) + numpy_like.fillna(other._data, inplace=True, value=fill_value) if (len(self._data) == len(other._data)): result_data = numpy_like.astype(self._data, numpy.float64) @@ -486,6 +474,7 @@ def _series_sub_none_indexes_impl(self, other, level=None, fill_value=None, axis min_data_size = min(left_size, right_size) max_data_size = max(left_size, right_size) result_data = numpy.empty(max_data_size, dtype=numpy.float64) + _fill_value = numpy.nan if fill_value_is_none == True else fill_value # noqa if (left_size == min_data_size): result_data[:min_data_size] = self._data for i in range(min_data_size, len(result_data)): @@ -510,10 +499,8 @@ def _series_sub_none_indexes_impl(self, other, level=None, fill_value=None, axis def _series_sub_common_impl(self, other, level=None, fill_value=None, axis=0): left_index, right_index = self.index, other.index - _fill_value = numpy.nan if fill_value_is_none == True else fill_value # noqa - if not (fill_value is None or numpy.isnan(fill_value)): - numpy_like.fillna(self._data, inplace=True, value=_fill_value) - numpy_like.fillna(other._data, inplace=True, value=_fill_value) + numpy_like.fillna(self._data, inplace=True, value=fill_value) + numpy_like.fillna(other._data, inplace=True, value=fill_value) # check if indexes are equal and series don't have to be aligned if sdc_check_indexes_equal(left_index, right_index): result_data = numpy.empty(len(self._data), dtype=numpy.float64) @@ -531,6 +518,7 @@ def _series_sub_common_impl(self, other, level=None, fill_value=None, axis=0): result_size = len(joined_index) left_values = numpy.empty(result_size, dtype=numpy.float64) right_values = numpy.empty(result_size, dtype=numpy.float64) + _fill_value = numpy.nan if fill_value_is_none == True else fill_value # noqa for i in range(result_size): left_pos, right_pos = left_indexer[i], right_indexer[i] left_values[i] = self._data[left_pos] if left_pos != -1 else _fill_value @@ -574,7 +562,8 @@ def sdc_pandas_series_mul(self, other, level=None, fill_value=None, axis=0): Test: python -m sdc.runtests sdc.tests.test_series.TestSeries.test_series_op5 """ - ty_checker = TypeChecker('Method mul().') + _func_name = 'Method mul().' + ty_checker = TypeChecker(_func_name) self_is_series, other_is_series = isinstance(self, SeriesType), isinstance(other, SeriesType) if not (self_is_series or other_is_series): return None @@ -610,23 +599,20 @@ def sdc_pandas_series_mul(self, other, level=None, fill_value=None, axis=0): if not isinstance(fill_value, (types.Omitted, types.Number, types.NoneType)) and fill_value is not None: ty_checker.raise_exc(fill_value, 'number', 'fill_value') + fill_value_is_none = isinstance(fill_value, (types.NoneType, types.Omitted)) or fill_value is None if not isinstance(axis, types.Omitted) and axis != 0: ty_checker.raise_exc(axis, 'int', 'axis') - fill_value_is_none = isinstance(fill_value, (types.NoneType, types.Omitted)) or fill_value is None # specializations for numeric series only if not operands_are_series: def _series_mul_scalar_impl(self, other, level=None, fill_value=None, axis=0): - _fill_value = numpy.nan if fill_value_is_none == True else fill_value # noqa if self_is_series == True: # noqa - if not (fill_value is None or numpy.isnan(fill_value)): - numpy_like.fillna(self._data, inplace=True, value=_fill_value) + numpy_like.fillna(self._data, inplace=True, value=fill_value) result_data = numpy.empty(len(self._data), dtype=numpy.float64) result_data[:] = self._data * numpy.float64(other) return pandas.Series(result_data, index=self._index, name=self._name) else: - if not (fill_value is None or numpy.isnan(fill_value)): - numpy_like.fillna(other._data, inplace=True, value=_fill_value) + numpy_like.fillna(other._data, inplace=True, value=fill_value) result_data = numpy.empty(len(other._data), dtype=numpy.float64) result_data[:] = numpy.float64(self) * other._data return pandas.Series(result_data, index=other._index, name=other._name) @@ -637,10 +623,8 @@ def _series_mul_scalar_impl(self, other, level=None, fill_value=None, axis=0): # optimization for series with default indexes, that can be aligned differently if (isinstance(self.index, types.NoneType) and isinstance(other.index, types.NoneType)): def _series_mul_none_indexes_impl(self, other, level=None, fill_value=None, axis=0): - _fill_value = numpy.nan if fill_value_is_none == True else fill_value # noqa - if not (fill_value is None or numpy.isnan(fill_value)): - numpy_like.fillna(self._data, inplace=True, value=_fill_value) - numpy_like.fillna(other._data, inplace=True, value=_fill_value) + numpy_like.fillna(self._data, inplace=True, value=fill_value) + numpy_like.fillna(other._data, inplace=True, value=fill_value) if (len(self._data) == len(other._data)): result_data = numpy_like.astype(self._data, numpy.float64) @@ -651,6 +635,7 @@ def _series_mul_none_indexes_impl(self, other, level=None, fill_value=None, axis min_data_size = min(left_size, right_size) max_data_size = max(left_size, right_size) result_data = numpy.empty(max_data_size, dtype=numpy.float64) + _fill_value = numpy.nan if fill_value_is_none == True else fill_value # noqa if (left_size == min_data_size): result_data[:min_data_size] = self._data for i in range(min_data_size, len(result_data)): @@ -675,10 +660,8 @@ def _series_mul_none_indexes_impl(self, other, level=None, fill_value=None, axis def _series_mul_common_impl(self, other, level=None, fill_value=None, axis=0): left_index, right_index = self.index, other.index - _fill_value = numpy.nan if fill_value_is_none == True else fill_value # noqa - if not (fill_value is None or numpy.isnan(fill_value)): - numpy_like.fillna(self._data, inplace=True, value=_fill_value) - numpy_like.fillna(other._data, inplace=True, value=_fill_value) + numpy_like.fillna(self._data, inplace=True, value=fill_value) + numpy_like.fillna(other._data, inplace=True, value=fill_value) # check if indexes are equal and series don't have to be aligned if sdc_check_indexes_equal(left_index, right_index): result_data = numpy.empty(len(self._data), dtype=numpy.float64) @@ -696,6 +679,7 @@ def _series_mul_common_impl(self, other, level=None, fill_value=None, axis=0): result_size = len(joined_index) left_values = numpy.empty(result_size, dtype=numpy.float64) right_values = numpy.empty(result_size, dtype=numpy.float64) + _fill_value = numpy.nan if fill_value_is_none == True else fill_value # noqa for i in range(result_size): left_pos, right_pos = left_indexer[i], right_indexer[i] left_values[i] = self._data[left_pos] if left_pos != -1 else _fill_value @@ -739,7 +723,8 @@ def sdc_pandas_series_truediv(self, other, level=None, fill_value=None, axis=0): Test: python -m sdc.runtests sdc.tests.test_series.TestSeries.test_series_op5 """ - ty_checker = TypeChecker('Method truediv().') + _func_name = 'Method truediv().' + ty_checker = TypeChecker(_func_name) self_is_series, other_is_series = isinstance(self, SeriesType), isinstance(other, SeriesType) if not (self_is_series or other_is_series): return None @@ -775,23 +760,20 @@ def sdc_pandas_series_truediv(self, other, level=None, fill_value=None, axis=0): if not isinstance(fill_value, (types.Omitted, types.Number, types.NoneType)) and fill_value is not None: ty_checker.raise_exc(fill_value, 'number', 'fill_value') + fill_value_is_none = isinstance(fill_value, (types.NoneType, types.Omitted)) or fill_value is None if not isinstance(axis, types.Omitted) and axis != 0: ty_checker.raise_exc(axis, 'int', 'axis') - fill_value_is_none = isinstance(fill_value, (types.NoneType, types.Omitted)) or fill_value is None # specializations for numeric series only if not operands_are_series: def _series_truediv_scalar_impl(self, other, level=None, fill_value=None, axis=0): - _fill_value = numpy.nan if fill_value_is_none == True else fill_value # noqa if self_is_series == True: # noqa - if not (fill_value is None or numpy.isnan(fill_value)): - numpy_like.fillna(self._data, inplace=True, value=_fill_value) + numpy_like.fillna(self._data, inplace=True, value=fill_value) result_data = numpy.empty(len(self._data), dtype=numpy.float64) result_data[:] = self._data / numpy.float64(other) return pandas.Series(result_data, index=self._index, name=self._name) else: - if not (fill_value is None or numpy.isnan(fill_value)): - numpy_like.fillna(other._data, inplace=True, value=_fill_value) + numpy_like.fillna(other._data, inplace=True, value=fill_value) result_data = numpy.empty(len(other._data), dtype=numpy.float64) result_data[:] = numpy.float64(self) / other._data return pandas.Series(result_data, index=other._index, name=other._name) @@ -802,10 +784,8 @@ def _series_truediv_scalar_impl(self, other, level=None, fill_value=None, axis=0 # optimization for series with default indexes, that can be aligned differently if (isinstance(self.index, types.NoneType) and isinstance(other.index, types.NoneType)): def _series_truediv_none_indexes_impl(self, other, level=None, fill_value=None, axis=0): - _fill_value = numpy.nan if fill_value_is_none == True else fill_value # noqa - if not (fill_value is None or numpy.isnan(fill_value)): - numpy_like.fillna(self._data, inplace=True, value=_fill_value) - numpy_like.fillna(other._data, inplace=True, value=_fill_value) + numpy_like.fillna(self._data, inplace=True, value=fill_value) + numpy_like.fillna(other._data, inplace=True, value=fill_value) if (len(self._data) == len(other._data)): result_data = numpy_like.astype(self._data, numpy.float64) @@ -816,6 +796,7 @@ def _series_truediv_none_indexes_impl(self, other, level=None, fill_value=None, min_data_size = min(left_size, right_size) max_data_size = max(left_size, right_size) result_data = numpy.empty(max_data_size, dtype=numpy.float64) + _fill_value = numpy.nan if fill_value_is_none == True else fill_value # noqa if (left_size == min_data_size): result_data[:min_data_size] = self._data for i in range(min_data_size, len(result_data)): @@ -840,10 +821,8 @@ def _series_truediv_none_indexes_impl(self, other, level=None, fill_value=None, def _series_truediv_common_impl(self, other, level=None, fill_value=None, axis=0): left_index, right_index = self.index, other.index - _fill_value = numpy.nan if fill_value_is_none == True else fill_value # noqa - if not (fill_value is None or numpy.isnan(fill_value)): - numpy_like.fillna(self._data, inplace=True, value=_fill_value) - numpy_like.fillna(other._data, inplace=True, value=_fill_value) + numpy_like.fillna(self._data, inplace=True, value=fill_value) + numpy_like.fillna(other._data, inplace=True, value=fill_value) # check if indexes are equal and series don't have to be aligned if sdc_check_indexes_equal(left_index, right_index): result_data = numpy.empty(len(self._data), dtype=numpy.float64) @@ -861,6 +840,7 @@ def _series_truediv_common_impl(self, other, level=None, fill_value=None, axis=0 result_size = len(joined_index) left_values = numpy.empty(result_size, dtype=numpy.float64) right_values = numpy.empty(result_size, dtype=numpy.float64) + _fill_value = numpy.nan if fill_value_is_none == True else fill_value # noqa for i in range(result_size): left_pos, right_pos = left_indexer[i], right_indexer[i] left_values[i] = self._data[left_pos] if left_pos != -1 else _fill_value @@ -904,7 +884,8 @@ def sdc_pandas_series_floordiv(self, other, level=None, fill_value=None, axis=0) Test: python -m sdc.runtests sdc.tests.test_series.TestSeries.test_series_op5 """ - ty_checker = TypeChecker('Method floordiv().') + _func_name = 'Method floordiv().' + ty_checker = TypeChecker(_func_name) self_is_series, other_is_series = isinstance(self, SeriesType), isinstance(other, SeriesType) if not (self_is_series or other_is_series): return None @@ -940,23 +921,20 @@ def sdc_pandas_series_floordiv(self, other, level=None, fill_value=None, axis=0) if not isinstance(fill_value, (types.Omitted, types.Number, types.NoneType)) and fill_value is not None: ty_checker.raise_exc(fill_value, 'number', 'fill_value') + fill_value_is_none = isinstance(fill_value, (types.NoneType, types.Omitted)) or fill_value is None if not isinstance(axis, types.Omitted) and axis != 0: ty_checker.raise_exc(axis, 'int', 'axis') - fill_value_is_none = isinstance(fill_value, (types.NoneType, types.Omitted)) or fill_value is None # specializations for numeric series only if not operands_are_series: def _series_floordiv_scalar_impl(self, other, level=None, fill_value=None, axis=0): - _fill_value = numpy.nan if fill_value_is_none == True else fill_value # noqa if self_is_series == True: # noqa - if not (fill_value is None or numpy.isnan(fill_value)): - numpy_like.fillna(self._data, inplace=True, value=_fill_value) + numpy_like.fillna(self._data, inplace=True, value=fill_value) result_data = numpy.empty(len(self._data), dtype=numpy.float64) result_data[:] = self._data // numpy.float64(other) return pandas.Series(result_data, index=self._index, name=self._name) else: - if not (fill_value is None or numpy.isnan(fill_value)): - numpy_like.fillna(other._data, inplace=True, value=_fill_value) + numpy_like.fillna(other._data, inplace=True, value=fill_value) result_data = numpy.empty(len(other._data), dtype=numpy.float64) result_data[:] = numpy.float64(self) // other._data return pandas.Series(result_data, index=other._index, name=other._name) @@ -967,10 +945,8 @@ def _series_floordiv_scalar_impl(self, other, level=None, fill_value=None, axis= # optimization for series with default indexes, that can be aligned differently if (isinstance(self.index, types.NoneType) and isinstance(other.index, types.NoneType)): def _series_floordiv_none_indexes_impl(self, other, level=None, fill_value=None, axis=0): - _fill_value = numpy.nan if fill_value_is_none == True else fill_value # noqa - if not (fill_value is None or numpy.isnan(fill_value)): - numpy_like.fillna(self._data, inplace=True, value=_fill_value) - numpy_like.fillna(other._data, inplace=True, value=_fill_value) + numpy_like.fillna(self._data, inplace=True, value=fill_value) + numpy_like.fillna(other._data, inplace=True, value=fill_value) if (len(self._data) == len(other._data)): result_data = numpy_like.astype(self._data, numpy.float64) @@ -981,6 +957,7 @@ def _series_floordiv_none_indexes_impl(self, other, level=None, fill_value=None, min_data_size = min(left_size, right_size) max_data_size = max(left_size, right_size) result_data = numpy.empty(max_data_size, dtype=numpy.float64) + _fill_value = numpy.nan if fill_value_is_none == True else fill_value # noqa if (left_size == min_data_size): result_data[:min_data_size] = self._data for i in range(min_data_size, len(result_data)): @@ -1005,10 +982,8 @@ def _series_floordiv_none_indexes_impl(self, other, level=None, fill_value=None, def _series_floordiv_common_impl(self, other, level=None, fill_value=None, axis=0): left_index, right_index = self.index, other.index - _fill_value = numpy.nan if fill_value_is_none == True else fill_value # noqa - if not (fill_value is None or numpy.isnan(fill_value)): - numpy_like.fillna(self._data, inplace=True, value=_fill_value) - numpy_like.fillna(other._data, inplace=True, value=_fill_value) + numpy_like.fillna(self._data, inplace=True, value=fill_value) + numpy_like.fillna(other._data, inplace=True, value=fill_value) # check if indexes are equal and series don't have to be aligned if sdc_check_indexes_equal(left_index, right_index): result_data = numpy.empty(len(self._data), dtype=numpy.float64) @@ -1026,6 +1001,7 @@ def _series_floordiv_common_impl(self, other, level=None, fill_value=None, axis= result_size = len(joined_index) left_values = numpy.empty(result_size, dtype=numpy.float64) right_values = numpy.empty(result_size, dtype=numpy.float64) + _fill_value = numpy.nan if fill_value_is_none == True else fill_value # noqa for i in range(result_size): left_pos, right_pos = left_indexer[i], right_indexer[i] left_values[i] = self._data[left_pos] if left_pos != -1 else _fill_value @@ -1069,7 +1045,8 @@ def sdc_pandas_series_mod(self, other, level=None, fill_value=None, axis=0): Test: python -m sdc.runtests sdc.tests.test_series.TestSeries.test_series_op5 """ - ty_checker = TypeChecker('Method mod().') + _func_name = 'Method mod().' + ty_checker = TypeChecker(_func_name) self_is_series, other_is_series = isinstance(self, SeriesType), isinstance(other, SeriesType) if not (self_is_series or other_is_series): return None @@ -1105,23 +1082,20 @@ def sdc_pandas_series_mod(self, other, level=None, fill_value=None, axis=0): if not isinstance(fill_value, (types.Omitted, types.Number, types.NoneType)) and fill_value is not None: ty_checker.raise_exc(fill_value, 'number', 'fill_value') + fill_value_is_none = isinstance(fill_value, (types.NoneType, types.Omitted)) or fill_value is None if not isinstance(axis, types.Omitted) and axis != 0: ty_checker.raise_exc(axis, 'int', 'axis') - fill_value_is_none = isinstance(fill_value, (types.NoneType, types.Omitted)) or fill_value is None # specializations for numeric series only if not operands_are_series: def _series_mod_scalar_impl(self, other, level=None, fill_value=None, axis=0): - _fill_value = numpy.nan if fill_value_is_none == True else fill_value # noqa if self_is_series == True: # noqa - if not (fill_value is None or numpy.isnan(fill_value)): - numpy_like.fillna(self._data, inplace=True, value=_fill_value) + numpy_like.fillna(self._data, inplace=True, value=fill_value) result_data = numpy.empty(len(self._data), dtype=numpy.float64) result_data[:] = self._data % numpy.float64(other) return pandas.Series(result_data, index=self._index, name=self._name) else: - if not (fill_value is None or numpy.isnan(fill_value)): - numpy_like.fillna(other._data, inplace=True, value=_fill_value) + numpy_like.fillna(other._data, inplace=True, value=fill_value) result_data = numpy.empty(len(other._data), dtype=numpy.float64) result_data[:] = numpy.float64(self) % other._data return pandas.Series(result_data, index=other._index, name=other._name) @@ -1132,10 +1106,8 @@ def _series_mod_scalar_impl(self, other, level=None, fill_value=None, axis=0): # optimization for series with default indexes, that can be aligned differently if (isinstance(self.index, types.NoneType) and isinstance(other.index, types.NoneType)): def _series_mod_none_indexes_impl(self, other, level=None, fill_value=None, axis=0): - _fill_value = numpy.nan if fill_value_is_none == True else fill_value # noqa - if not (fill_value is None or numpy.isnan(fill_value)): - numpy_like.fillna(self._data, inplace=True, value=_fill_value) - numpy_like.fillna(other._data, inplace=True, value=_fill_value) + numpy_like.fillna(self._data, inplace=True, value=fill_value) + numpy_like.fillna(other._data, inplace=True, value=fill_value) if (len(self._data) == len(other._data)): result_data = numpy_like.astype(self._data, numpy.float64) @@ -1146,6 +1118,7 @@ def _series_mod_none_indexes_impl(self, other, level=None, fill_value=None, axis min_data_size = min(left_size, right_size) max_data_size = max(left_size, right_size) result_data = numpy.empty(max_data_size, dtype=numpy.float64) + _fill_value = numpy.nan if fill_value_is_none == True else fill_value # noqa if (left_size == min_data_size): result_data[:min_data_size] = self._data for i in range(min_data_size, len(result_data)): @@ -1170,10 +1143,8 @@ def _series_mod_none_indexes_impl(self, other, level=None, fill_value=None, axis def _series_mod_common_impl(self, other, level=None, fill_value=None, axis=0): left_index, right_index = self.index, other.index - _fill_value = numpy.nan if fill_value_is_none == True else fill_value # noqa - if not (fill_value is None or numpy.isnan(fill_value)): - numpy_like.fillna(self._data, inplace=True, value=_fill_value) - numpy_like.fillna(other._data, inplace=True, value=_fill_value) + numpy_like.fillna(self._data, inplace=True, value=fill_value) + numpy_like.fillna(other._data, inplace=True, value=fill_value) # check if indexes are equal and series don't have to be aligned if sdc_check_indexes_equal(left_index, right_index): result_data = numpy.empty(len(self._data), dtype=numpy.float64) @@ -1191,6 +1162,7 @@ def _series_mod_common_impl(self, other, level=None, fill_value=None, axis=0): result_size = len(joined_index) left_values = numpy.empty(result_size, dtype=numpy.float64) right_values = numpy.empty(result_size, dtype=numpy.float64) + _fill_value = numpy.nan if fill_value_is_none == True else fill_value # noqa for i in range(result_size): left_pos, right_pos = left_indexer[i], right_indexer[i] left_values[i] = self._data[left_pos] if left_pos != -1 else _fill_value @@ -1234,7 +1206,8 @@ def sdc_pandas_series_pow(self, other, level=None, fill_value=None, axis=0): Test: python -m sdc.runtests sdc.tests.test_series.TestSeries.test_series_op5 """ - ty_checker = TypeChecker('Method pow().') + _func_name = 'Method pow().' + ty_checker = TypeChecker(_func_name) self_is_series, other_is_series = isinstance(self, SeriesType), isinstance(other, SeriesType) if not (self_is_series or other_is_series): return None @@ -1270,23 +1243,20 @@ def sdc_pandas_series_pow(self, other, level=None, fill_value=None, axis=0): if not isinstance(fill_value, (types.Omitted, types.Number, types.NoneType)) and fill_value is not None: ty_checker.raise_exc(fill_value, 'number', 'fill_value') + fill_value_is_none = isinstance(fill_value, (types.NoneType, types.Omitted)) or fill_value is None if not isinstance(axis, types.Omitted) and axis != 0: ty_checker.raise_exc(axis, 'int', 'axis') - fill_value_is_none = isinstance(fill_value, (types.NoneType, types.Omitted)) or fill_value is None # specializations for numeric series only if not operands_are_series: def _series_pow_scalar_impl(self, other, level=None, fill_value=None, axis=0): - _fill_value = numpy.nan if fill_value_is_none == True else fill_value # noqa if self_is_series == True: # noqa - if not (fill_value is None or numpy.isnan(fill_value)): - numpy_like.fillna(self._data, inplace=True, value=_fill_value) + numpy_like.fillna(self._data, inplace=True, value=fill_value) result_data = numpy.empty(len(self._data), dtype=numpy.float64) result_data[:] = self._data ** numpy.float64(other) return pandas.Series(result_data, index=self._index, name=self._name) else: - if not (fill_value is None or numpy.isnan(fill_value)): - numpy_like.fillna(other._data, inplace=True, value=_fill_value) + numpy_like.fillna(other._data, inplace=True, value=fill_value) result_data = numpy.empty(len(other._data), dtype=numpy.float64) result_data[:] = numpy.float64(self) ** other._data return pandas.Series(result_data, index=other._index, name=other._name) @@ -1297,10 +1267,8 @@ def _series_pow_scalar_impl(self, other, level=None, fill_value=None, axis=0): # optimization for series with default indexes, that can be aligned differently if (isinstance(self.index, types.NoneType) and isinstance(other.index, types.NoneType)): def _series_pow_none_indexes_impl(self, other, level=None, fill_value=None, axis=0): - _fill_value = numpy.nan if fill_value_is_none == True else fill_value # noqa - if not (fill_value is None or numpy.isnan(fill_value)): - numpy_like.fillna(self._data, inplace=True, value=_fill_value) - numpy_like.fillna(other._data, inplace=True, value=_fill_value) + numpy_like.fillna(self._data, inplace=True, value=fill_value) + numpy_like.fillna(other._data, inplace=True, value=fill_value) if (len(self._data) == len(other._data)): result_data = numpy_like.astype(self._data, numpy.float64) @@ -1311,6 +1279,7 @@ def _series_pow_none_indexes_impl(self, other, level=None, fill_value=None, axis min_data_size = min(left_size, right_size) max_data_size = max(left_size, right_size) result_data = numpy.empty(max_data_size, dtype=numpy.float64) + _fill_value = numpy.nan if fill_value_is_none == True else fill_value # noqa if (left_size == min_data_size): result_data[:min_data_size] = self._data for i in range(min_data_size, len(result_data)): @@ -1335,10 +1304,8 @@ def _series_pow_none_indexes_impl(self, other, level=None, fill_value=None, axis def _series_pow_common_impl(self, other, level=None, fill_value=None, axis=0): left_index, right_index = self.index, other.index - _fill_value = numpy.nan if fill_value_is_none == True else fill_value # noqa - if not (fill_value is None or numpy.isnan(fill_value)): - numpy_like.fillna(self._data, inplace=True, value=_fill_value) - numpy_like.fillna(other._data, inplace=True, value=_fill_value) + numpy_like.fillna(self._data, inplace=True, value=fill_value) + numpy_like.fillna(other._data, inplace=True, value=fill_value) # check if indexes are equal and series don't have to be aligned if sdc_check_indexes_equal(left_index, right_index): result_data = numpy.empty(len(self._data), dtype=numpy.float64) @@ -1356,6 +1323,7 @@ def _series_pow_common_impl(self, other, level=None, fill_value=None, axis=0): result_size = len(joined_index) left_values = numpy.empty(result_size, dtype=numpy.float64) right_values = numpy.empty(result_size, dtype=numpy.float64) + _fill_value = numpy.nan if fill_value_is_none == True else fill_value # noqa for i in range(result_size): left_pos, right_pos = left_indexer[i], right_indexer[i] left_values[i] = self._data[left_pos] if left_pos != -1 else _fill_value @@ -1440,14 +1408,11 @@ def sdc_pandas_series_lt(self, other, level=None, fill_value=None, axis=0): fill_value_is_none = isinstance(fill_value, (types.NoneType, types.Omitted)) or fill_value is None if not operands_are_series: def _series_lt_scalar_impl(self, other, level=None, fill_value=None, axis=0): - _fill_value = numpy.nan if fill_value_is_none == True else fill_value # noqa if self_is_series == True: # noqa - if not (fill_value is None or numpy.isnan(fill_value)): - numpy_like.fillna(self._data, inplace=True, value=_fill_value) + numpy_like.fillna(self._data, inplace=True, value=fill_value) return pandas.Series(self._data < other, index=self._index, name=self._name) else: - if not (fill_value is None or numpy.isnan(fill_value)): - numpy_like.fillna(other._data, inplace=True, value=_fill_value) + numpy_like.fillna(other._data, inplace=True, value=fill_value) return pandas.Series(self < other._data, index=other._index, name=other._name) return _series_lt_scalar_impl @@ -1457,10 +1422,8 @@ def _series_lt_scalar_impl(self, other, level=None, fill_value=None, axis=0): # optimization for series with default indexes, that can be aligned differently if (isinstance(self.index, types.NoneType) and isinstance(other.index, types.NoneType)): def _series_lt_none_indexes_impl(self, other, level=None, fill_value=None, axis=0): - _fill_value = numpy.nan if fill_value_is_none == True else fill_value # noqa - if not (fill_value is None or numpy.isnan(fill_value)): - numpy_like.fillna(self._data, inplace=True, value=_fill_value) - numpy_like.fillna(other._data, inplace=True, value=_fill_value) + numpy_like.fillna(self._data, inplace=True, value=fill_value) + numpy_like.fillna(other._data, inplace=True, value=fill_value) left_size, right_size = len(self._data), len(other._data) if (left_size == right_size): return pandas.Series(self._data < other._data) @@ -1477,10 +1440,8 @@ def _series_lt_none_indexes_impl(self, other, level=None, fill_value=None, axis= [ty_left_index_dtype, ty_right_index_dtype], []) def _series_lt_common_impl(self, other, level=None, fill_value=None, axis=0): - _fill_value = numpy.nan if fill_value_is_none == True else fill_value # noqa - if not (fill_value is None or numpy.isnan(fill_value)): - numpy_like.fillna(self._data, inplace=True, value=_fill_value) - numpy_like.fillna(other._data, inplace=True, value=_fill_value) + numpy_like.fillna(self._data, inplace=True, value=fill_value) + numpy_like.fillna(other._data, inplace=True, value=fill_value) left_index, right_index = self.index, other.index if sdc_check_indexes_equal(left_index, right_index): @@ -1570,14 +1531,11 @@ def sdc_pandas_series_gt(self, other, level=None, fill_value=None, axis=0): fill_value_is_none = isinstance(fill_value, (types.NoneType, types.Omitted)) or fill_value is None if not operands_are_series: def _series_gt_scalar_impl(self, other, level=None, fill_value=None, axis=0): - _fill_value = numpy.nan if fill_value_is_none == True else fill_value # noqa if self_is_series == True: # noqa - if not (fill_value is None or numpy.isnan(fill_value)): - numpy_like.fillna(self._data, inplace=True, value=_fill_value) + numpy_like.fillna(self._data, inplace=True, value=fill_value) return pandas.Series(self._data > other, index=self._index, name=self._name) else: - if not (fill_value is None or numpy.isnan(fill_value)): - numpy_like.fillna(other._data, inplace=True, value=_fill_value) + numpy_like.fillna(other._data, inplace=True, value=fill_value) return pandas.Series(self > other._data, index=other._index, name=other._name) return _series_gt_scalar_impl @@ -1587,10 +1545,8 @@ def _series_gt_scalar_impl(self, other, level=None, fill_value=None, axis=0): # optimization for series with default indexes, that can be aligned differently if (isinstance(self.index, types.NoneType) and isinstance(other.index, types.NoneType)): def _series_gt_none_indexes_impl(self, other, level=None, fill_value=None, axis=0): - _fill_value = numpy.nan if fill_value_is_none == True else fill_value # noqa - if not (fill_value is None or numpy.isnan(fill_value)): - numpy_like.fillna(self._data, inplace=True, value=_fill_value) - numpy_like.fillna(other._data, inplace=True, value=_fill_value) + numpy_like.fillna(self._data, inplace=True, value=fill_value) + numpy_like.fillna(other._data, inplace=True, value=fill_value) left_size, right_size = len(self._data), len(other._data) if (left_size == right_size): return pandas.Series(self._data > other._data) @@ -1607,10 +1563,8 @@ def _series_gt_none_indexes_impl(self, other, level=None, fill_value=None, axis= [ty_left_index_dtype, ty_right_index_dtype], []) def _series_gt_common_impl(self, other, level=None, fill_value=None, axis=0): - _fill_value = numpy.nan if fill_value_is_none == True else fill_value # noqa - if not (fill_value is None or numpy.isnan(fill_value)): - numpy_like.fillna(self._data, inplace=True, value=_fill_value) - numpy_like.fillna(other._data, inplace=True, value=_fill_value) + numpy_like.fillna(self._data, inplace=True, value=fill_value) + numpy_like.fillna(other._data, inplace=True, value=fill_value) left_index, right_index = self.index, other.index if sdc_check_indexes_equal(left_index, right_index): @@ -1700,14 +1654,11 @@ def sdc_pandas_series_le(self, other, level=None, fill_value=None, axis=0): fill_value_is_none = isinstance(fill_value, (types.NoneType, types.Omitted)) or fill_value is None if not operands_are_series: def _series_le_scalar_impl(self, other, level=None, fill_value=None, axis=0): - _fill_value = numpy.nan if fill_value_is_none == True else fill_value # noqa if self_is_series == True: # noqa - if not (fill_value is None or numpy.isnan(fill_value)): - numpy_like.fillna(self._data, inplace=True, value=_fill_value) + numpy_like.fillna(self._data, inplace=True, value=fill_value) return pandas.Series(self._data <= other, index=self._index, name=self._name) else: - if not (fill_value is None or numpy.isnan(fill_value)): - numpy_like.fillna(other._data, inplace=True, value=_fill_value) + numpy_like.fillna(other._data, inplace=True, value=fill_value) return pandas.Series(self <= other._data, index=other._index, name=other._name) return _series_le_scalar_impl @@ -1717,10 +1668,8 @@ def _series_le_scalar_impl(self, other, level=None, fill_value=None, axis=0): # optimization for series with default indexes, that can be aligned differently if (isinstance(self.index, types.NoneType) and isinstance(other.index, types.NoneType)): def _series_le_none_indexes_impl(self, other, level=None, fill_value=None, axis=0): - _fill_value = numpy.nan if fill_value_is_none == True else fill_value # noqa - if not (fill_value is None or numpy.isnan(fill_value)): - numpy_like.fillna(self._data, inplace=True, value=_fill_value) - numpy_like.fillna(other._data, inplace=True, value=_fill_value) + numpy_like.fillna(self._data, inplace=True, value=fill_value) + numpy_like.fillna(other._data, inplace=True, value=fill_value) left_size, right_size = len(self._data), len(other._data) if (left_size == right_size): return pandas.Series(self._data <= other._data) @@ -1737,10 +1686,8 @@ def _series_le_none_indexes_impl(self, other, level=None, fill_value=None, axis= [ty_left_index_dtype, ty_right_index_dtype], []) def _series_le_common_impl(self, other, level=None, fill_value=None, axis=0): - _fill_value = numpy.nan if fill_value_is_none == True else fill_value # noqa - if not (fill_value is None or numpy.isnan(fill_value)): - numpy_like.fillna(self._data, inplace=True, value=_fill_value) - numpy_like.fillna(other._data, inplace=True, value=_fill_value) + numpy_like.fillna(self._data, inplace=True, value=fill_value) + numpy_like.fillna(other._data, inplace=True, value=fill_value) left_index, right_index = self.index, other.index if sdc_check_indexes_equal(left_index, right_index): @@ -1830,14 +1777,11 @@ def sdc_pandas_series_ge(self, other, level=None, fill_value=None, axis=0): fill_value_is_none = isinstance(fill_value, (types.NoneType, types.Omitted)) or fill_value is None if not operands_are_series: def _series_ge_scalar_impl(self, other, level=None, fill_value=None, axis=0): - _fill_value = numpy.nan if fill_value_is_none == True else fill_value # noqa if self_is_series == True: # noqa - if not (fill_value is None or numpy.isnan(fill_value)): - numpy_like.fillna(self._data, inplace=True, value=_fill_value) + numpy_like.fillna(self._data, inplace=True, value=fill_value) return pandas.Series(self._data >= other, index=self._index, name=self._name) else: - if not (fill_value is None or numpy.isnan(fill_value)): - numpy_like.fillna(other._data, inplace=True, value=_fill_value) + numpy_like.fillna(other._data, inplace=True, value=fill_value) return pandas.Series(self >= other._data, index=other._index, name=other._name) return _series_ge_scalar_impl @@ -1847,10 +1791,8 @@ def _series_ge_scalar_impl(self, other, level=None, fill_value=None, axis=0): # optimization for series with default indexes, that can be aligned differently if (isinstance(self.index, types.NoneType) and isinstance(other.index, types.NoneType)): def _series_ge_none_indexes_impl(self, other, level=None, fill_value=None, axis=0): - _fill_value = numpy.nan if fill_value_is_none == True else fill_value # noqa - if not (fill_value is None or numpy.isnan(fill_value)): - numpy_like.fillna(self._data, inplace=True, value=_fill_value) - numpy_like.fillna(other._data, inplace=True, value=_fill_value) + numpy_like.fillna(self._data, inplace=True, value=fill_value) + numpy_like.fillna(other._data, inplace=True, value=fill_value) left_size, right_size = len(self._data), len(other._data) if (left_size == right_size): return pandas.Series(self._data >= other._data) @@ -1867,10 +1809,8 @@ def _series_ge_none_indexes_impl(self, other, level=None, fill_value=None, axis= [ty_left_index_dtype, ty_right_index_dtype], []) def _series_ge_common_impl(self, other, level=None, fill_value=None, axis=0): - _fill_value = numpy.nan if fill_value_is_none == True else fill_value # noqa - if not (fill_value is None or numpy.isnan(fill_value)): - numpy_like.fillna(self._data, inplace=True, value=_fill_value) - numpy_like.fillna(other._data, inplace=True, value=_fill_value) + numpy_like.fillna(self._data, inplace=True, value=fill_value) + numpy_like.fillna(other._data, inplace=True, value=fill_value) left_index, right_index = self.index, other.index if sdc_check_indexes_equal(left_index, right_index): @@ -1960,14 +1900,11 @@ def sdc_pandas_series_ne(self, other, level=None, fill_value=None, axis=0): fill_value_is_none = isinstance(fill_value, (types.NoneType, types.Omitted)) or fill_value is None if not operands_are_series: def _series_ne_scalar_impl(self, other, level=None, fill_value=None, axis=0): - _fill_value = numpy.nan if fill_value_is_none == True else fill_value # noqa if self_is_series == True: # noqa - if not (fill_value is None or numpy.isnan(fill_value)): - numpy_like.fillna(self._data, inplace=True, value=_fill_value) + numpy_like.fillna(self._data, inplace=True, value=fill_value) return pandas.Series(self._data != other, index=self._index, name=self._name) else: - if not (fill_value is None or numpy.isnan(fill_value)): - numpy_like.fillna(other._data, inplace=True, value=_fill_value) + numpy_like.fillna(other._data, inplace=True, value=fill_value) return pandas.Series(self != other._data, index=other._index, name=other._name) return _series_ne_scalar_impl @@ -1977,10 +1914,8 @@ def _series_ne_scalar_impl(self, other, level=None, fill_value=None, axis=0): # optimization for series with default indexes, that can be aligned differently if (isinstance(self.index, types.NoneType) and isinstance(other.index, types.NoneType)): def _series_ne_none_indexes_impl(self, other, level=None, fill_value=None, axis=0): - _fill_value = numpy.nan if fill_value_is_none == True else fill_value # noqa - if not (fill_value is None or numpy.isnan(fill_value)): - numpy_like.fillna(self._data, inplace=True, value=_fill_value) - numpy_like.fillna(other._data, inplace=True, value=_fill_value) + numpy_like.fillna(self._data, inplace=True, value=fill_value) + numpy_like.fillna(other._data, inplace=True, value=fill_value) left_size, right_size = len(self._data), len(other._data) if (left_size == right_size): return pandas.Series(self._data != other._data) @@ -1997,10 +1932,8 @@ def _series_ne_none_indexes_impl(self, other, level=None, fill_value=None, axis= [ty_left_index_dtype, ty_right_index_dtype], []) def _series_ne_common_impl(self, other, level=None, fill_value=None, axis=0): - _fill_value = numpy.nan if fill_value_is_none == True else fill_value # noqa - if not (fill_value is None or numpy.isnan(fill_value)): - numpy_like.fillna(self._data, inplace=True, value=_fill_value) - numpy_like.fillna(other._data, inplace=True, value=_fill_value) + numpy_like.fillna(self._data, inplace=True, value=fill_value) + numpy_like.fillna(other._data, inplace=True, value=fill_value) left_index, right_index = self.index, other.index if sdc_check_indexes_equal(left_index, right_index): @@ -2090,14 +2023,11 @@ def sdc_pandas_series_eq(self, other, level=None, fill_value=None, axis=0): fill_value_is_none = isinstance(fill_value, (types.NoneType, types.Omitted)) or fill_value is None if not operands_are_series: def _series_eq_scalar_impl(self, other, level=None, fill_value=None, axis=0): - _fill_value = numpy.nan if fill_value_is_none == True else fill_value # noqa if self_is_series == True: # noqa - if not (fill_value is None or numpy.isnan(fill_value)): - numpy_like.fillna(self._data, inplace=True, value=_fill_value) + numpy_like.fillna(self._data, inplace=True, value=fill_value) return pandas.Series(self._data == other, index=self._index, name=self._name) else: - if not (fill_value is None or numpy.isnan(fill_value)): - numpy_like.fillna(other._data, inplace=True, value=_fill_value) + numpy_like.fillna(other._data, inplace=True, value=fill_value) return pandas.Series(self == other._data, index=other._index, name=other._name) return _series_eq_scalar_impl @@ -2107,10 +2037,8 @@ def _series_eq_scalar_impl(self, other, level=None, fill_value=None, axis=0): # optimization for series with default indexes, that can be aligned differently if (isinstance(self.index, types.NoneType) and isinstance(other.index, types.NoneType)): def _series_eq_none_indexes_impl(self, other, level=None, fill_value=None, axis=0): - _fill_value = numpy.nan if fill_value_is_none == True else fill_value # noqa - if not (fill_value is None or numpy.isnan(fill_value)): - numpy_like.fillna(self._data, inplace=True, value=_fill_value) - numpy_like.fillna(other._data, inplace=True, value=_fill_value) + numpy_like.fillna(self._data, inplace=True, value=fill_value) + numpy_like.fillna(other._data, inplace=True, value=fill_value) left_size, right_size = len(self._data), len(other._data) if (left_size == right_size): return pandas.Series(self._data == other._data) @@ -2127,10 +2055,8 @@ def _series_eq_none_indexes_impl(self, other, level=None, fill_value=None, axis= [ty_left_index_dtype, ty_right_index_dtype], []) def _series_eq_common_impl(self, other, level=None, fill_value=None, axis=0): - _fill_value = numpy.nan if fill_value_is_none == True else fill_value # noqa - if not (fill_value is None or numpy.isnan(fill_value)): - numpy_like.fillna(self._data, inplace=True, value=_fill_value) - numpy_like.fillna(other._data, inplace=True, value=_fill_value) + numpy_like.fillna(self._data, inplace=True, value=fill_value) + numpy_like.fillna(other._data, inplace=True, value=fill_value) left_index, right_index = self.index, other.index if sdc_check_indexes_equal(left_index, right_index): @@ -2175,7 +2101,8 @@ def sdc_pandas_series_operator_add(self, other): The result of the operation """ - ty_checker = TypeChecker('Operator add().') + _func_name = 'Operator add().' + ty_checker = TypeChecker(_func_name) self_is_series, other_is_series = isinstance(self, SeriesType), isinstance(other, SeriesType) if not (self_is_series or other_is_series): return None @@ -2239,7 +2166,8 @@ def sdc_pandas_series_operator_sub(self, other): The result of the operation """ - ty_checker = TypeChecker('Operator sub().') + _func_name = 'Operator sub().' + ty_checker = TypeChecker(_func_name) self_is_series, other_is_series = isinstance(self, SeriesType), isinstance(other, SeriesType) if not (self_is_series or other_is_series): return None @@ -2303,7 +2231,8 @@ def sdc_pandas_series_operator_mul(self, other): The result of the operation """ - ty_checker = TypeChecker('Operator mul().') + _func_name = 'Operator mul().' + ty_checker = TypeChecker(_func_name) self_is_series, other_is_series = isinstance(self, SeriesType), isinstance(other, SeriesType) if not (self_is_series or other_is_series): return None @@ -2367,7 +2296,8 @@ def sdc_pandas_series_operator_truediv(self, other): The result of the operation """ - ty_checker = TypeChecker('Operator truediv().') + _func_name = 'Operator truediv().' + ty_checker = TypeChecker(_func_name) self_is_series, other_is_series = isinstance(self, SeriesType), isinstance(other, SeriesType) if not (self_is_series or other_is_series): return None @@ -2431,7 +2361,8 @@ def sdc_pandas_series_operator_floordiv(self, other): The result of the operation """ - ty_checker = TypeChecker('Operator floordiv().') + _func_name = 'Operator floordiv().' + ty_checker = TypeChecker(_func_name) self_is_series, other_is_series = isinstance(self, SeriesType), isinstance(other, SeriesType) if not (self_is_series or other_is_series): return None @@ -2495,7 +2426,8 @@ def sdc_pandas_series_operator_mod(self, other): The result of the operation """ - ty_checker = TypeChecker('Operator mod().') + _func_name = 'Operator mod().' + ty_checker = TypeChecker(_func_name) self_is_series, other_is_series = isinstance(self, SeriesType), isinstance(other, SeriesType) if not (self_is_series or other_is_series): return None @@ -2559,7 +2491,8 @@ def sdc_pandas_series_operator_pow(self, other): The result of the operation """ - ty_checker = TypeChecker('Operator pow().') + _func_name = 'Operator pow().' + ty_checker = TypeChecker(_func_name) self_is_series, other_is_series = isinstance(self, SeriesType), isinstance(other, SeriesType) if not (self_is_series or other_is_series): return None @@ -2619,7 +2552,8 @@ def sdc_pandas_series_operator_lt(self, other): The result of the operation """ - ty_checker = TypeChecker('Operator lt().') + _func_name = 'Operator lt().' + ty_checker = TypeChecker(_func_name) self_is_series, other_is_series = isinstance(self, SeriesType), isinstance(other, SeriesType) if not (self_is_series or other_is_series): return None @@ -2673,7 +2607,8 @@ def sdc_pandas_series_operator_gt(self, other): The result of the operation """ - ty_checker = TypeChecker('Operator gt().') + _func_name = 'Operator gt().' + ty_checker = TypeChecker(_func_name) self_is_series, other_is_series = isinstance(self, SeriesType), isinstance(other, SeriesType) if not (self_is_series or other_is_series): return None @@ -2727,7 +2662,8 @@ def sdc_pandas_series_operator_le(self, other): The result of the operation """ - ty_checker = TypeChecker('Operator le().') + _func_name = 'Operator le().' + ty_checker = TypeChecker(_func_name) self_is_series, other_is_series = isinstance(self, SeriesType), isinstance(other, SeriesType) if not (self_is_series or other_is_series): return None @@ -2781,7 +2717,8 @@ def sdc_pandas_series_operator_ge(self, other): The result of the operation """ - ty_checker = TypeChecker('Operator ge().') + _func_name = 'Operator ge().' + ty_checker = TypeChecker(_func_name) self_is_series, other_is_series = isinstance(self, SeriesType), isinstance(other, SeriesType) if not (self_is_series or other_is_series): return None @@ -2835,7 +2772,8 @@ def sdc_pandas_series_operator_ne(self, other): The result of the operation """ - ty_checker = TypeChecker('Operator ne().') + _func_name = 'Operator ne().' + ty_checker = TypeChecker(_func_name) self_is_series, other_is_series = isinstance(self, SeriesType), isinstance(other, SeriesType) if not (self_is_series or other_is_series): return None @@ -2889,7 +2827,8 @@ def sdc_pandas_series_operator_eq(self, other): The result of the operation """ - ty_checker = TypeChecker('Operator eq().') + _func_name = 'Operator eq().' + ty_checker = TypeChecker(_func_name) self_is_series, other_is_series = isinstance(self, SeriesType), isinstance(other, SeriesType) if not (self_is_series or other_is_series): return None diff --git a/sdc/sdc_function_templates.py b/sdc/sdc_function_templates.py index d7e5c4dad..7f049c631 100644 --- a/sdc/sdc_function_templates.py +++ b/sdc/sdc_function_templates.py @@ -79,7 +79,8 @@ def sdc_pandas_series_binop(self, other, level=None, fill_value=None, axis=0): Test: python -m sdc.runtests sdc.tests.test_series.TestSeries.test_series_op5 """ - ty_checker = TypeChecker('Method binop().') + _func_name = 'Method binop().' + ty_checker = TypeChecker(_func_name) self_is_series, other_is_series = isinstance(self, SeriesType), isinstance(other, SeriesType) if not (self_is_series or other_is_series): return None @@ -115,23 +116,20 @@ def sdc_pandas_series_binop(self, other, level=None, fill_value=None, axis=0): if not isinstance(fill_value, (types.Omitted, types.Number, types.NoneType)) and fill_value is not None: ty_checker.raise_exc(fill_value, 'number', 'fill_value') + fill_value_is_none = isinstance(fill_value, (types.NoneType, types.Omitted)) or fill_value is None if not isinstance(axis, types.Omitted) and axis != 0: ty_checker.raise_exc(axis, 'int', 'axis') - fill_value_is_none = isinstance(fill_value, (types.NoneType, types.Omitted)) or fill_value is None # specializations for numeric series only if not operands_are_series: def _series_binop_scalar_impl(self, other, level=None, fill_value=None, axis=0): - _fill_value = numpy.nan if fill_value_is_none == True else fill_value # noqa if self_is_series == True: # noqa - if not (fill_value is None or numpy.isnan(fill_value)): - numpy_like.fillna(self._data, inplace=True, value=_fill_value) + numpy_like.fillna(self._data, inplace=True, value=fill_value) result_data = numpy.empty(len(self._data), dtype=numpy.float64) result_data[:] = self._data + numpy.float64(other) return pandas.Series(result_data, index=self._index, name=self._name) else: - if not (fill_value is None or numpy.isnan(fill_value)): - numpy_like.fillna(other._data, inplace=True, value=_fill_value) + numpy_like.fillna(other._data, inplace=True, value=fill_value) result_data = numpy.empty(len(other._data), dtype=numpy.float64) result_data[:] = numpy.float64(self) + other._data return pandas.Series(result_data, index=other._index, name=other._name) @@ -142,10 +140,8 @@ def _series_binop_scalar_impl(self, other, level=None, fill_value=None, axis=0): # optimization for series with default indexes, that can be aligned differently if (isinstance(self.index, types.NoneType) and isinstance(other.index, types.NoneType)): def _series_binop_none_indexes_impl(self, other, level=None, fill_value=None, axis=0): - _fill_value = numpy.nan if fill_value_is_none == True else fill_value # noqa - if not (fill_value is None or numpy.isnan(fill_value)): - numpy_like.fillna(self._data, inplace=True, value=_fill_value) - numpy_like.fillna(other._data, inplace=True, value=_fill_value) + numpy_like.fillna(self._data, inplace=True, value=fill_value) + numpy_like.fillna(other._data, inplace=True, value=fill_value) if (len(self._data) == len(other._data)): result_data = numpy_like.astype(self._data, numpy.float64) @@ -156,6 +152,7 @@ def _series_binop_none_indexes_impl(self, other, level=None, fill_value=None, ax min_data_size = min(left_size, right_size) max_data_size = max(left_size, right_size) result_data = numpy.empty(max_data_size, dtype=numpy.float64) + _fill_value = numpy.nan if fill_value_is_none == True else fill_value # noqa if (left_size == min_data_size): result_data[:min_data_size] = self._data for i in range(min_data_size, len(result_data)): @@ -180,10 +177,8 @@ def _series_binop_none_indexes_impl(self, other, level=None, fill_value=None, ax def _series_binop_common_impl(self, other, level=None, fill_value=None, axis=0): left_index, right_index = self.index, other.index - _fill_value = numpy.nan if fill_value_is_none == True else fill_value # noqa - if not (fill_value is None or numpy.isnan(fill_value)): - numpy_like.fillna(self._data, inplace=True, value=_fill_value) - numpy_like.fillna(other._data, inplace=True, value=_fill_value) + numpy_like.fillna(self._data, inplace=True, value=fill_value) + numpy_like.fillna(other._data, inplace=True, value=fill_value) # check if indexes are equal and series don't have to be aligned if sdc_check_indexes_equal(left_index, right_index): result_data = numpy.empty(len(self._data), dtype=numpy.float64) @@ -201,6 +196,7 @@ def _series_binop_common_impl(self, other, level=None, fill_value=None, axis=0): result_size = len(joined_index) left_values = numpy.empty(result_size, dtype=numpy.float64) right_values = numpy.empty(result_size, dtype=numpy.float64) + _fill_value = numpy.nan if fill_value_is_none == True else fill_value # noqa for i in range(result_size): left_pos, right_pos = left_indexer[i], right_indexer[i] left_values[i] = self._data[left_pos] if left_pos != -1 else _fill_value @@ -284,14 +280,11 @@ def sdc_pandas_series_comp_binop(self, other, level=None, fill_value=None, axis= fill_value_is_none = isinstance(fill_value, (types.NoneType, types.Omitted)) or fill_value is None if not operands_are_series: def _series_comp_binop_scalar_impl(self, other, level=None, fill_value=None, axis=0): - _fill_value = numpy.nan if fill_value_is_none == True else fill_value # noqa if self_is_series == True: # noqa - if not (fill_value is None or numpy.isnan(fill_value)): - numpy_like.fillna(self._data, inplace=True, value=_fill_value) + numpy_like.fillna(self._data, inplace=True, value=fill_value) return pandas.Series(self._data < other, index=self._index, name=self._name) else: - if not (fill_value is None or numpy.isnan(fill_value)): - numpy_like.fillna(other._data, inplace=True, value=_fill_value) + numpy_like.fillna(other._data, inplace=True, value=fill_value) return pandas.Series(self < other._data, index=other._index, name=other._name) return _series_comp_binop_scalar_impl @@ -301,10 +294,8 @@ def _series_comp_binop_scalar_impl(self, other, level=None, fill_value=None, axi # optimization for series with default indexes, that can be aligned differently if (isinstance(self.index, types.NoneType) and isinstance(other.index, types.NoneType)): def _series_comp_binop_none_indexes_impl(self, other, level=None, fill_value=None, axis=0): - _fill_value = numpy.nan if fill_value_is_none == True else fill_value # noqa - if not (fill_value is None or numpy.isnan(fill_value)): - numpy_like.fillna(self._data, inplace=True, value=_fill_value) - numpy_like.fillna(other._data, inplace=True, value=_fill_value) + numpy_like.fillna(self._data, inplace=True, value=fill_value) + numpy_like.fillna(other._data, inplace=True, value=fill_value) left_size, right_size = len(self._data), len(other._data) if (left_size == right_size): return pandas.Series(self._data < other._data) @@ -321,10 +312,8 @@ def _series_comp_binop_none_indexes_impl(self, other, level=None, fill_value=Non [ty_left_index_dtype, ty_right_index_dtype], []) def _series_comp_binop_common_impl(self, other, level=None, fill_value=None, axis=0): - _fill_value = numpy.nan if fill_value_is_none == True else fill_value # noqa - if not (fill_value is None or numpy.isnan(fill_value)): - numpy_like.fillna(self._data, inplace=True, value=_fill_value) - numpy_like.fillna(other._data, inplace=True, value=_fill_value) + numpy_like.fillna(self._data, inplace=True, value=fill_value) + numpy_like.fillna(other._data, inplace=True, value=fill_value) left_index, right_index = self.index, other.index if sdc_check_indexes_equal(left_index, right_index): @@ -368,7 +357,8 @@ def sdc_pandas_series_operator_binop(self, other): The result of the operation """ - ty_checker = TypeChecker('Operator binop().') + _func_name = 'Operator binop().' + ty_checker = TypeChecker(_func_name) self_is_series, other_is_series = isinstance(self, SeriesType), isinstance(other, SeriesType) if not (self_is_series or other_is_series): return None @@ -427,7 +417,8 @@ def sdc_pandas_series_operator_comp_binop(self, other): The result of the operation """ - ty_checker = TypeChecker('Operator comp_binop().') + _func_name = 'Operator comp_binop().' + ty_checker = TypeChecker(_func_name) self_is_series, other_is_series = isinstance(self, SeriesType), isinstance(other, SeriesType) if not (self_is_series or other_is_series): return None diff --git a/sdc/tests/test_sdc_numpy.py b/sdc/tests/test_sdc_numpy.py index 3646c5a39..ba5546691 100644 --- a/sdc/tests/test_sdc_numpy.py +++ b/sdc/tests/test_sdc_numpy.py @@ -30,6 +30,7 @@ import pandas as pd import sdc import unittest +from itertools import product from sdc.str_arr_ext import StringArray from sdc.str_ext import std_str_to_unicode, unicode_to_std_str @@ -359,6 +360,104 @@ def sdc_impl(a): with self.subTest(data=case): np.testing.assert_array_equal(ref_impl(array0), sdc_func(array1)) + def test_fillna_numeric_inplace_false(self): + def ref_impl(S, value): + if value is None: + return S.values.copy() + else: + return S.fillna(value=value, inplace=False).values + + def sdc_impl(a, value): + return numpy_like.fillna(a, inplace=False, value=value) + sdc_func = self.jit(sdc_impl) + + data_to_test = [ + [True, False, False, True, True], + [5, 2, 0, 333, -4], + [3.3, 5.4, 7.9], + [3.3, 5.4, np.nan, 7.9, np.nan], + ] + values_to_test = [ + None, + np.nan, + 2.1, + 2 + ] + for data, value in product(data_to_test, values_to_test): + a = np.asarray(data) + with self.subTest(data=data, value=value): + result = sdc_func(a, value) + result_ref = ref_impl(pd.Series(a), value) + np.testing.assert_array_equal(result, result_ref) + + def test_fillna_str_inplace_false(self): + def ref_impl(S, value): + if value is None: + return S.values.copy() + else: + return S.fillna(value=value, inplace=False).values + + def sdc_impl(S, value): + str_arr = S.values + return numpy_like.fillna(str_arr, inplace=False, value=value) + sdc_func = self.jit(sdc_impl) + + data_to_test = [ + ['a', 'b', 'c', 'd'], + ['a', 'b', None, 'c', None, 'd'], + ] + values_to_test = [ + None, + '', + 'asd' + ] + for data, value in product(data_to_test, values_to_test): + S = pd.Series(data) + with self.subTest(data=data, value=value): + result = sdc_func(S, value) + result_ref = ref_impl(S, value) + + # FIXME: str_arr unifies None with np.nan and StringArray boxing always return np.nan + # to avoid mismatch in results for fill value == None use custome comparing func + def is_same_unify_nones(a, b): + return a == b or ((a is None or np.isnan(a)) and (b is None or np.isnan(b))) + cmp_result = np.asarray( + list(map(is_same_unify_nones, result, result_ref)) + ) + self.assertEqual(np.all(cmp_result), True) + + def test_fillna_numeric_inplace_true(self): + def ref_impl(S, value): + if value is None: + return S.values + else: + S.fillna(value=value, inplace=True) + return S.values + + def sdc_impl(a, value): + return numpy_like.fillna(a, inplace=True, value=value) + sdc_func = self.jit(sdc_impl) + + data_to_test = [ + [True, False, False, True, True], + [5, 2, 0, 333, -4], + [3.3, 5.4, 7.9], + [3.3, 5.4, np.nan, 7.9, np.nan], + ] + values_to_test = [ + None, + np.nan, + 2.1, + 2 + ] + for data, value in product(data_to_test, values_to_test): + a1 = np.asarray(data) + a2 = np.copy(a1) + with self.subTest(data=data, value=value): + sdc_func(a1, value) + ref_impl(pd.Series(a2), value) + np.testing.assert_array_equal(a1, a2) + class TestArrayReductions(TestCase): From 2a5996cd2d27e5a30f6907074b66e9093e8dce09 Mon Sep 17 00:00:00 2001 From: "Kozlov, Alexey" Date: Tue, 16 Jun 2020 13:56:44 +0300 Subject: [PATCH 2/2] Applying remarks and updating new tests --- sdc/tests/test_sdc_numpy.py | 84 ++++++++++++++++--------------------- 1 file changed, 37 insertions(+), 47 deletions(-) diff --git a/sdc/tests/test_sdc_numpy.py b/sdc/tests/test_sdc_numpy.py index ba5546691..cf1ef3985 100644 --- a/sdc/tests/test_sdc_numpy.py +++ b/sdc/tests/test_sdc_numpy.py @@ -360,17 +360,7 @@ def sdc_impl(a): with self.subTest(data=case): np.testing.assert_array_equal(ref_impl(array0), sdc_func(array1)) - def test_fillna_numeric_inplace_false(self): - def ref_impl(S, value): - if value is None: - return S.values.copy() - else: - return S.fillna(value=value, inplace=False).values - - def sdc_impl(a, value): - return numpy_like.fillna(a, inplace=False, value=value) - sdc_func = self.jit(sdc_impl) - + def _test_fillna_numeric(self, pyfunc, cfunc, inplace): data_to_test = [ [True, False, False, True, True], [5, 2, 0, 333, -4], @@ -383,13 +373,45 @@ def sdc_impl(a, value): 2.1, 2 ] + for data, value in product(data_to_test, values_to_test): - a = np.asarray(data) + a1 = np.asarray(data) + a2 = pd.Series(np.copy(a1)) if inplace else pd.Series(a1) + with self.subTest(data=data, value=value): - result = sdc_func(a, value) - result_ref = ref_impl(pd.Series(a), value) + result = cfunc(a1, value) + result_ref = pyfunc(a2, value) + if inplace: + result, result_ref = a1, a2 np.testing.assert_array_equal(result, result_ref) + def test_fillna_numeric_inplace_false(self): + def ref_impl(S, value): + if value is None: + return S.values.copy() + else: + return S.fillna(value=value, inplace=False).values + + def sdc_impl(a, value): + return numpy_like.fillna(a, inplace=False, value=value) + sdc_func = self.jit(sdc_impl) + + self._test_fillna_numeric(ref_impl, sdc_func, inplace=False) + + def test_fillna_numeric_inplace_true(self): + def ref_impl(S, value): + if value is None: + return None + else: + S.fillna(value=value, inplace=True) + return None + + def sdc_impl(a, value): + return numpy_like.fillna(a, inplace=True, value=value) + sdc_func = self.jit(sdc_impl) + + self._test_fillna_numeric(ref_impl, sdc_func, inplace=True) + def test_fillna_str_inplace_false(self): def ref_impl(S, value): if value is None: @@ -418,7 +440,7 @@ def sdc_impl(S, value): result_ref = ref_impl(S, value) # FIXME: str_arr unifies None with np.nan and StringArray boxing always return np.nan - # to avoid mismatch in results for fill value == None use custome comparing func + # to avoid mismatch in results for fill value == None use custom comparing func def is_same_unify_nones(a, b): return a == b or ((a is None or np.isnan(a)) and (b is None or np.isnan(b))) cmp_result = np.asarray( @@ -426,38 +448,6 @@ def is_same_unify_nones(a, b): ) self.assertEqual(np.all(cmp_result), True) - def test_fillna_numeric_inplace_true(self): - def ref_impl(S, value): - if value is None: - return S.values - else: - S.fillna(value=value, inplace=True) - return S.values - - def sdc_impl(a, value): - return numpy_like.fillna(a, inplace=True, value=value) - sdc_func = self.jit(sdc_impl) - - data_to_test = [ - [True, False, False, True, True], - [5, 2, 0, 333, -4], - [3.3, 5.4, 7.9], - [3.3, 5.4, np.nan, 7.9, np.nan], - ] - values_to_test = [ - None, - np.nan, - 2.1, - 2 - ] - for data, value in product(data_to_test, values_to_test): - a1 = np.asarray(data) - a2 = np.copy(a1) - with self.subTest(data=data, value=value): - sdc_func(a1, value) - ref_impl(pd.Series(a2), value) - np.testing.assert_array_equal(a1, a2) - class TestArrayReductions(TestCase):