Ejemplo n.º 1
0
    def test_std(self):
        tdi = pd.TimedeltaIndex(["0H", "4H", "NaT", "4H", "0H", "2H"])
        arr = tdi.array

        result = arr.std(skipna=True)
        expected = pd.Timedelta(hours=2)
        assert isinstance(result, pd.Timedelta)
        assert result == expected

        result = tdi.std(skipna=True)
        assert isinstance(result, pd.Timedelta)
        assert result == expected

        result = nanops.nanstd(np.asarray(arr), skipna=True)
        assert isinstance(result, pd.Timedelta)
        assert result == expected

        result = arr.std(skipna=False)
        assert result is pd.NaT

        result = tdi.std(skipna=False)
        assert result is pd.NaT

        result = nanops.nanstd(np.asarray(arr), skipna=False)
        assert result is pd.NaT
Ejemplo n.º 2
0
    def test_std(self, add):
        tdi = pd.TimedeltaIndex(["0H", "4H", "NaT", "4H", "0H", "2H"]) + add
        arr = tdi.array

        result = arr.std(skipna=True)
        expected = Timedelta(hours=2)
        assert isinstance(result, Timedelta)
        assert result == expected

        result = tdi.std(skipna=True)
        assert isinstance(result, Timedelta)
        assert result == expected

        if getattr(arr, "tz", None) is None:
            result = nanops.nanstd(np.asarray(arr), skipna=True)
            assert isinstance(result, Timedelta)
            assert result == expected

        result = arr.std(skipna=False)
        assert result is pd.NaT

        result = tdi.std(skipna=False)
        assert result is pd.NaT

        if getattr(arr, "tz", None) is None:
            result = nanops.nanstd(np.asarray(arr), skipna=False)
            assert result is pd.NaT
Ejemplo n.º 3
0
    def test_ground_truth(self):
        # Test against values that were precomputed with Numpy.
        samples = np.empty((4, 4))
        samples[:3, :3] = np.array([[0.97303362, 0.21869576, 0.55560287],
                                    [0.72980153, 0.03109364, 0.99155171],
                                    [0.09317602, 0.60078248, 0.15871292]])
        samples[3] = samples[:, 3] = np.nan

        # Actual variances along axis=0, 1 for ddof=0, 1, 2
        variance = np.array([[[0.13762259, 0.05619224, 0.11568816],
                              [0.20643388, 0.08428837, 0.17353224],
                              [0.41286776, 0.16857673, 0.34706449]],
                             [[0.09519783, 0.16435395, 0.05082054],
                              [0.14279674, 0.24653093, 0.07623082],
                              [0.28559348, 0.49306186, 0.15246163]]])

        # Test nanvar.
        for axis in range(2):
            for ddof in range(3):
                var = nanops.nanvar(samples, skipna=True, axis=axis, ddof=ddof)
                np.testing.assert_array_almost_equal(var[:3], variance[axis,
                                                                       ddof])
                np.testing.assert_equal(var[3], np.nan)

        # Test nanstd.
        for axis in range(2):
            for ddof in range(3):
                std = nanops.nanstd(samples, skipna=True, axis=axis, ddof=ddof)
                np.testing.assert_array_almost_equal(std[:3],
                                                     variance[axis, ddof]**0.5)
                np.testing.assert_equal(std[3], np.nan)
Ejemplo n.º 4
0
 def std(self, axis=None, dtype=None, out=None, ddof=1, keepdims=False,
         skipna=True):
     nv.validate_stat_ddof_func((), dict(dtype=dtype, out=out,
                                         keepdims=keepdims),
                                fname='std')
     return nanops.nanstd(self._ndarray, axis=axis, skipna=skipna,
                          ddof=ddof)
Ejemplo n.º 5
0
    def test_ground_truth(self):
        # Test against values that were precomputed with Numpy.
        samples = np.empty((4, 4))
        samples[:3, :3] = np.array([[0.97303362, 0.21869576, 0.55560287
                                     ], [0.72980153, 0.03109364, 0.99155171],
                                    [0.09317602, 0.60078248, 0.15871292]])
        samples[3] = samples[:, 3] = np.nan

        # Actual variances along axis=0, 1 for ddof=0, 1, 2
        variance = np.array([[[0.13762259, 0.05619224, 0.11568816
                               ], [0.20643388, 0.08428837, 0.17353224],
                              [0.41286776, 0.16857673, 0.34706449]],
                             [[0.09519783, 0.16435395, 0.05082054
                               ], [0.14279674, 0.24653093, 0.07623082],
                              [0.28559348, 0.49306186, 0.15246163]]])

        # Test nanvar.
        for axis in range(2):
            for ddof in range(3):
                var = nanops.nanvar(samples, skipna=True, axis=axis, ddof=ddof)
                tm.assert_almost_equal(var[:3], variance[axis, ddof])
                self.assertTrue(np.isnan(var[3]))

        # Test nanstd.
        for axis in range(2):
            for ddof in range(3):
                std = nanops.nanstd(samples, skipna=True, axis=axis, ddof=ddof)
                tm.assert_almost_equal(std[:3], variance[axis, ddof] ** 0.5)
                self.assertTrue(np.isnan(std[3]))
Ejemplo n.º 6
0
 def std(
     self, *, axis=None, dtype=None, out=None, ddof=1, keepdims=False, skipna=True
 ):
     nv.validate_stat_ddof_func(
         (), dict(dtype=dtype, out=out, keepdims=keepdims), fname="std"
     )
     return nanops.nanstd(self._ndarray, axis=axis, skipna=skipna, ddof=ddof)
Ejemplo n.º 7
0
 def std(
     self, *, axis=None, dtype=None, out=None, ddof=1, keepdims=False, skipna=True
 ):
     nv.validate_stat_ddof_func(
         (), {"dtype": dtype, "out": out, "keepdims": keepdims}, fname="std"
     )
     result = nanops.nanstd(self._ndarray, axis=axis, skipna=skipna, ddof=ddof)
     return self._wrap_reduction_result(axis, result)
Ejemplo n.º 8
0
    def test_nanstd_nans(self):
        samples = np.nan * np.ones(2 * self.samples.shape[0])
        samples[::2] = self.samples

        actual_std = nanops.nanstd(samples, skipna=True)
        tm.assert_almost_equal(actual_std, self.variance**0.5, rtol=1e-2)

        actual_std = nanops.nanvar(samples, skipna=False)
        tm.assert_almost_equal(actual_std, np.nan, rtol=1e-2)
Ejemplo n.º 9
0
    def test_nanstd_nans(self):
        samples = np.nan * np.ones(2 * self.samples.shape[0])
        samples[::2] = self.samples

        actual_std = nanops.nanstd(samples, skipna=True)
        tm.assert_almost_equal(actual_std, self.variance ** 0.5, check_less_precise=2)

        actual_std = nanops.nanvar(samples, skipna=False)
        tm.assert_almost_equal(actual_std, np.nan, check_less_precise=2)
Ejemplo n.º 10
0
    def test_nanstd_nans(self):
        samples = np.nan * np.ones(2 * self.samples.shape[0])
        samples[::2] = self.samples

        actual_std = nanops.nanstd(samples, skipna=True)
        np.testing.assert_almost_equal(actual_std, self.variance ** 0.5, decimal=2)

        actual_std = nanops.nanvar(samples, skipna=False)
        np.testing.assert_almost_equal(actual_std, np.nan, decimal=2)
Ejemplo n.º 11
0
    def std(
        self,
        axis=None,
        dtype=None,
        out=None,
        ddof: int = 1,
        keepdims: bool = False,
        skipna: bool = True,
    ):
        nv.validate_stat_ddof_func(
            (), dict(dtype=dtype, out=out, keepdims=keepdims), fname="std"
        )

        result = nanops.nanstd(self._ndarray, axis=axis, skipna=skipna, ddof=ddof)
        if axis is None or self.ndim == 1:
            return self._box_func(result)
        return self._from_backing_data(result)
Ejemplo n.º 12
0
    def std(
        self,
        *,
        axis: int | None = None,
        dtype: NpDtype | None = None,
        out=None,
        ddof: int = 1,
        keepdims: bool = False,
        skipna: bool = True,
    ):
        nv.validate_stat_ddof_func(
            (), {"dtype": dtype, "out": out, "keepdims": keepdims}, fname="std"
        )

        result = nanops.nanstd(self._ndarray, axis=axis, skipna=skipna, ddof=ddof)
        if axis is None or self.ndim == 1:
            return self._box_func(result)
        return self._from_backing_data(result)
Ejemplo n.º 13
0
    def std(
        self,
        axis=None,
        dtype=None,
        out=None,
        ddof: int = 1,
        keepdims: bool = False,
        skipna: bool = True,
    ):
        nv.validate_stat_ddof_func(
            (), dict(dtype=dtype, out=out, keepdims=keepdims), fname="std"
        )
        if not len(self):
            return NaT
        if not skipna and self._hasnans:
            return NaT

        result = nanops.nanstd(self._data, axis=axis, skipna=skipna, ddof=ddof)
        return Timedelta(result)