Exemplo n.º 1
0
    def test_int_index(self):
        arr = np.random.randn(100, 4)
        result = reduction.compute_reduction(arr, np.sum, labels=Index(np.arange(4)))
        expected = arr.sum(0)
        assert_almost_equal(result, expected)

        result = reduction.compute_reduction(
            arr, np.sum, axis=1, labels=Index(np.arange(100))
        )
        expected = arr.sum(1)
        assert_almost_equal(result, expected)

        dummy = Series(0.0, index=np.arange(100))
        result = reduction.compute_reduction(
            arr, np.sum, dummy=dummy, labels=Index(np.arange(4))
        )
        expected = arr.sum(0)
        assert_almost_equal(result, expected)

        dummy = Series(0.0, index=np.arange(4))
        result = reduction.compute_reduction(
            arr, np.sum, axis=1, dummy=dummy, labels=Index(np.arange(100))
        )
        expected = arr.sum(1)
        assert_almost_equal(result, expected)

        result = reduction.compute_reduction(
            arr, np.sum, axis=1, dummy=dummy, labels=Index(np.arange(100))
        )
        assert_almost_equal(result, expected)
Exemplo n.º 2
0
    def apply_standard(self):

        # try to reduce first (by default)
        # this only matters if the reduction in values is of different dtype
        # e.g. if we want to apply to a SparseFrame, then can't directly reduce

        # we cannot reduce using non-numpy dtypes,
        # as demonstrated in gh-12244
        if (self.result_type in ["reduce", None]
                and not self.dtypes.apply(is_extension_type).any()):

            # Create a dummy Series from an empty array
            from pandas import Series

            values = self.values
            index = self.obj._get_axis(self.axis)
            labels = self.agg_axis
            empty_arr = np.empty(len(index), dtype=values.dtype)
            dummy = Series(empty_arr, index=index, dtype=values.dtype)

            try:
                result = libreduction.compute_reduction(values,
                                                        self.f,
                                                        axis=self.axis,
                                                        dummy=dummy,
                                                        labels=labels)
                return self.obj._constructor_sliced(result, index=labels)
            except Exception:
                pass

        # compute the result using the series generator
        self.apply_series_generator()

        # wrap results
        return self.wrap_results()
Exemplo n.º 3
0
    def apply_standard(self):

        # partial result that may be returned from reduction
        partial_result = None

        # try to reduce first (by default)
        # this only matters if the reduction in values is of different dtype
        # e.g. if we want to apply to a SparseFrame, then can't directly reduce

        # we cannot reduce using non-numpy dtypes,
        # as demonstrated in gh-12244
        if (
            self.result_type in ["reduce", None]
            and not self.dtypes.apply(is_extension_array_dtype).any()
            # Disallow dtypes where setting _index_data will break
            #  ExtensionArray values, see GH#31182
            and not self.dtypes.apply(lambda x: x.kind in ["m", "M"]).any()
            # Disallow complex_internals since libreduction shortcut raises a TypeError
            and not self.agg_axis._has_complex_internals
        ):

            values = self.values
            index = self.obj._get_axis(self.axis)
            labels = self.agg_axis
            empty_arr = np.empty(len(index), dtype=values.dtype)

            # Preserve subclass for e.g. test_subclassed_apply
            dummy = self.obj._constructor_sliced(
                empty_arr, index=index, dtype=values.dtype
            )

            try:
                result, reduction_success = libreduction.compute_reduction(
                    values, self.f, axis=self.axis, dummy=dummy, labels=labels
                )
            except TypeError:
                # e.g. test_apply_ignore_failures we just ignore
                if not self.ignore_failures:
                    raise
            except ZeroDivisionError:
                # reached via numexpr; fall back to python implementation
                pass
            else:
                if reduction_success:
                    return self.obj._constructor_sliced(result, index=labels)

                # no exceptions - however reduction was unsuccessful,
                # use the computed function result for first element
                partial_result = result[0]
                if isinstance(partial_result, ABCSeries):
                    partial_result = partial_result.infer_objects()

        # compute the result using the series generator,
        # use the result computed while trying to reduce if available.
        results, res_index = self.apply_series_generator(partial_result)

        # wrap results
        return self.wrap_results(results, res_index)
Exemplo n.º 4
0
    def test_int_index(self):
        arr = np.random.randn(100, 4)

        msg = "Must pass either dummy and labels, or neither"
        # we must pass either both labels and dummy, or neither
        with pytest.raises(ValueError, match=msg):
            libreduction.compute_reduction(arr, np.sum, labels=Index(np.arange(4)))

        with pytest.raises(ValueError, match=msg):
            libreduction.compute_reduction(
                arr, np.sum, axis=1, labels=Index(np.arange(100))
            )

        dummy = Series(0.0, index=np.arange(100))
        result = libreduction.compute_reduction(
            arr, np.sum, dummy=dummy, labels=Index(np.arange(4))
        )
        expected = arr.sum(0)
        tm.assert_almost_equal(result, expected)

        dummy = Series(0.0, index=np.arange(4))
        result = libreduction.compute_reduction(
            arr, np.sum, axis=1, dummy=dummy, labels=Index(np.arange(100))
        )
        expected = arr.sum(1)
        tm.assert_almost_equal(result, expected)

        result = libreduction.compute_reduction(
            arr, np.sum, axis=1, dummy=dummy, labels=Index(np.arange(100))
        )
        tm.assert_almost_equal(result, expected)
Exemplo n.º 5
0
    def apply_standard(self):

        # try to reduce first (by default)
        # this only matters if the reduction in values is of different dtype
        # e.g. if we want to apply to a SparseFrame, then can't directly reduce

        # we cannot reduce using non-numpy dtypes,
        # as demonstrated in gh-12244
        if (self.result_type in ["reduce", None]
                and not self.dtypes.apply(is_extension_array_dtype).any()
                # Disallow dtypes where setting _index_data will break
                #  ExtensionArray values, see GH#31182
                and
                not self.dtypes.apply(lambda x: x.kind in ["m", "M"]).any()
                # Disallow complex_internals since libreduction shortcut raises a TypeError
                and not self.agg_axis._has_complex_internals):

            values = self.values
            index = self.obj._get_axis(self.axis)
            labels = self.agg_axis
            empty_arr = np.empty(len(index), dtype=values.dtype)

            # Preserve subclass for e.g. test_subclassed_apply
            dummy = self.obj._constructor_sliced(empty_arr,
                                                 index=index,
                                                 dtype=values.dtype)

            try:
                result = libreduction.compute_reduction(values,
                                                        self.f,
                                                        axis=self.axis,
                                                        dummy=dummy,
                                                        labels=labels)
            except ValueError as err:
                if "Function does not reduce" not in str(err):
                    # catch only ValueError raised intentionally in libreduction
                    raise
            except TypeError:
                # e.g. test_apply_ignore_failures we just ignore
                if not self.ignore_failures:
                    raise
            except ZeroDivisionError:
                # reached via numexpr; fall back to python implementation
                pass
            else:
                return self.obj._constructor_sliced(result, index=labels)

        # compute the result using the series generator
        results, res_index = self.apply_series_generator()

        # wrap results
        return self.wrap_results(results, res_index)
Exemplo n.º 6
0
    def apply_raw(self):
        """ apply to the values as a numpy array """

        try:
            result = libreduction.compute_reduction(self.values, self.f, axis=self.axis)
        except Exception:
            result = np.apply_along_axis(self.f, self.axis, self.values)

        # TODO: mixed type case
        if result.ndim == 2:
            return self.obj._constructor(result, index=self.index, columns=self.columns)
        else:
            return self.obj._constructor_sliced(result, index=self.agg_axis)
Exemplo n.º 7
0
    def apply_raw(self):
        """ apply to the values as a numpy array """
        result, reduction_success = libreduction.compute_reduction(
            self.values, self.f, axis=self.axis
        )

        # We expect np.apply_along_axis to give a two-dimensional result, or raise.
        if not reduction_success:
            result = np.apply_along_axis(self.f, self.axis, self.values)

        # TODO: mixed type case
        if result.ndim == 2:
            return self.obj._constructor(result, index=self.index, columns=self.columns)
        else:
            return self.obj._constructor_sliced(result, index=self.agg_axis)
Exemplo n.º 8
0
    def apply_raw(self):
        """ apply to the values as a numpy array """
        try:
            result = libreduction.compute_reduction(self.values, self.f, axis=self.axis)
        except ValueError as err:
            if "Function does not reduce" not in str(err):
                # catch only ValueError raised intentionally in libreduction
                raise
            result = np.apply_along_axis(self.f, self.axis, self.values)

        # TODO: mixed type case
        if result.ndim == 2:
            return self.obj._constructor(result, index=self.index, columns=self.columns)
        else:
            return self.obj._constructor_sliced(result, index=self.agg_axis)