Esempio n. 1
0
    def test_int_index(self):
        from pandas.core.series import Series

        arr = np.random.randn(100, 4)
        result = lib.reduce(arr, np.sum, labels=Index(np.arange(4)))
        expected = arr.sum(0)
        assert_almost_equal(result, expected)

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

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

        dummy = Series(0., index=np.arange(4))
        result = lib.reduce(arr, np.sum, axis=1, dummy=dummy,
                            labels=Index(np.arange(100)))
        expected = arr.sum(1)
        assert_almost_equal(result, expected)

        result = lib.reduce(arr, np.sum, axis=1, dummy=dummy,
                            labels=Index(np.arange(100)))
        assert_almost_equal(result, expected)
Esempio n. 2
0
    def test_int_index(self):
        from pandas.core.series import Series

        arr = np.random.randn(100, 4)
        result = lib.reduce(arr, np.sum, labels=Index(np.arange(4)))
        expected = arr.sum(0)
        assert_almost_equal(result, expected)

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

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

        dummy = Series(0., index=np.arange(4))
        result = lib.reduce(arr, np.sum, axis=1, dummy=dummy,
                            labels=Index(np.arange(100)))
        expected = arr.sum(1)
        assert_almost_equal(result, expected)

        result = lib.reduce(arr, np.sum, axis=1, dummy=dummy,
                            labels=Index(np.arange(100)))
        assert_almost_equal(result, expected)
Esempio n. 3
0
    def apply_raw(self):
        try:
            result = lib.reduce(self.values, self.f, axis=self.axis)
        except Exception:
            result = np.apply_along_axis(self.f, self.axis, self.values)

        # TODO: mixed type case
        from pandas import DataFrame, Series
        if result.ndim == 2:
            return DataFrame(result, index=self.index, columns=self.columns)
        else:
            return Series(result, index=self.agg_axis)
Esempio n. 4
0
    def apply_raw(self):
        try:
            result = lib.reduce(self.values, self.f, axis=self.axis)
        except Exception:
            result = np.apply_along_axis(self.f, self.axis, self.values)

        # TODO: mixed type case
        from pandas import DataFrame, Series
        if result.ndim == 2:
            return DataFrame(result, index=self.index, columns=self.columns)
        else:
            return Series(result, index=self.agg_axis)
Esempio n. 5
0
    def apply_standard(self):
        from pandas import Series

        reduce = self.reduce
        if reduce is None:
            reduce = True

        # 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
        if reduce:
            values = self.values

            # we cannot reduce using non-numpy dtypes,
            # as demonstrated in gh-12244
            if not is_extension_type(values):

                # Create a dummy Series from an empty array
                index = self.obj._get_axis(self.axis)
                empty_arr = np.empty(len(index), dtype=values.dtype)

                dummy = Series(empty_arr, index=index, dtype=values.dtype)

                try:
                    labels = self.agg_axis
                    result = lib.reduce(values,
                                        self.f,
                                        axis=self.axis,
                                        dummy=dummy,
                                        labels=labels)
                    return Series(result, index=labels)
                except Exception:
                    pass

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

        # wrap results
        return self.wrap_results(results, res_index, res_columns)
Esempio n. 6
0
    def apply_standard(self):
        from pandas import Series

        reduce = self.reduce
        if reduce is None:
            reduce = True

        # 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
        if reduce:
            values = self.values

            # we cannot reduce using non-numpy dtypes,
            # as demonstrated in gh-12244
            if not is_extension_type(values):

                # Create a dummy Series from an empty array
                index = self.obj._get_axis(self.axis)
                empty_arr = np.empty(len(index), dtype=values.dtype)

                dummy = Series(empty_arr, index=index, dtype=values.dtype)

                try:
                    labels = self.agg_axis
                    result = lib.reduce(values, self.f,
                                        axis=self.axis,
                                        dummy=dummy,
                                        labels=labels)
                    return Series(result, index=labels)
                except Exception:
                    pass

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

        # wrap results
        return self.wrap_results(results, res_index, res_columns)