def test_round(self, tz_naive_fixture):
        tz = tz_naive_fixture
        rng = date_range(start="2016-01-01", periods=5, freq="30Min", tz=tz)
        elt = rng[1]

        expected_rng = DatetimeIndex(
            [
                Timestamp("2016-01-01 00:00:00", tz=tz),
                Timestamp("2016-01-01 00:00:00", tz=tz),
                Timestamp("2016-01-01 01:00:00", tz=tz),
                Timestamp("2016-01-01 02:00:00", tz=tz),
                Timestamp("2016-01-01 02:00:00", tz=tz),
            ]
        )
        expected_elt = expected_rng[1]

        tm.assert_index_equal(rng.round(freq="H"), expected_rng)
        assert elt.round(freq="H") == expected_elt

        msg = INVALID_FREQ_ERR_MSG
        with pytest.raises(ValueError, match=msg):
            rng.round(freq="foo")
        with pytest.raises(ValueError, match=msg):
            elt.round(freq="foo")

        msg = "<MonthEnd> is a non-fixed frequency"
        with pytest.raises(ValueError, match=msg):
            rng.round(freq="M")
        with pytest.raises(ValueError, match=msg):
            elt.round(freq="M")

        # GH#14440 & GH#15578
        index = DatetimeIndex(["2016-10-17 12:00:00.0015"], tz=tz)
        result = index.round("ms")
        expected = DatetimeIndex(["2016-10-17 12:00:00.002000"], tz=tz)
        tm.assert_index_equal(result, expected)

        for freq in ["us", "ns"]:
            tm.assert_index_equal(index, index.round(freq))

        index = DatetimeIndex(["2016-10-17 12:00:00.00149"], tz=tz)
        result = index.round("ms")
        expected = DatetimeIndex(["2016-10-17 12:00:00.001000"], tz=tz)
        tm.assert_index_equal(result, expected)

        index = DatetimeIndex(["2016-10-17 12:00:00.001501031"])
        result = index.round("10ns")
        expected = DatetimeIndex(["2016-10-17 12:00:00.001501030"])
        tm.assert_index_equal(result, expected)

        with tm.assert_produces_warning(False):
            ts = "2016-10-17 12:00:00.001501031"
            DatetimeIndex([ts]).round("1010ns")
Пример #2
0
    def test_round(self, tz_naive_fixture):
        tz = tz_naive_fixture
        rng = date_range(start='2016-01-01', periods=5,
                         freq='30Min', tz=tz)
        elt = rng[1]

        expected_rng = DatetimeIndex([
            Timestamp('2016-01-01 00:00:00', tz=tz, freq='30T'),
            Timestamp('2016-01-01 00:00:00', tz=tz, freq='30T'),
            Timestamp('2016-01-01 01:00:00', tz=tz, freq='30T'),
            Timestamp('2016-01-01 02:00:00', tz=tz, freq='30T'),
            Timestamp('2016-01-01 02:00:00', tz=tz, freq='30T'),
        ])
        expected_elt = expected_rng[1]

        tm.assert_index_equal(rng.round(freq='H'), expected_rng)
        assert elt.round(freq='H') == expected_elt

        msg = pd._libs.tslibs.frequencies.INVALID_FREQ_ERR_MSG
        with pytest.raises(ValueError, match=msg):
            rng.round(freq='foo')
        with pytest.raises(ValueError, match=msg):
            elt.round(freq='foo')

        msg = "<MonthEnd> is a non-fixed frequency"
        with pytest.raises(ValueError, match=msg):
            rng.round(freq='M')
        with pytest.raises(ValueError, match=msg):
            elt.round(freq='M')

        # GH#14440 & GH#15578
        index = DatetimeIndex(['2016-10-17 12:00:00.0015'], tz=tz)
        result = index.round('ms')
        expected = DatetimeIndex(['2016-10-17 12:00:00.002000'], tz=tz)
        tm.assert_index_equal(result, expected)

        for freq in ['us', 'ns']:
            tm.assert_index_equal(index, index.round(freq))

        index = DatetimeIndex(['2016-10-17 12:00:00.00149'], tz=tz)
        result = index.round('ms')
        expected = DatetimeIndex(['2016-10-17 12:00:00.001000'], tz=tz)
        tm.assert_index_equal(result, expected)

        index = DatetimeIndex(['2016-10-17 12:00:00.001501031'])
        result = index.round('10ns')
        expected = DatetimeIndex(['2016-10-17 12:00:00.001501030'])
        tm.assert_index_equal(result, expected)

        with tm.assert_produces_warning(False):
            ts = '2016-10-17 12:00:00.001501031'
            DatetimeIndex([ts]).round('1010ns')
Пример #3
0
    def test_round(self, tz_naive_fixture):
        tz = tz_naive_fixture
        rng = date_range(start='2016-01-01', periods=5,
                         freq='30Min', tz=tz)
        elt = rng[1]

        expected_rng = DatetimeIndex([
            Timestamp('2016-01-01 00:00:00', tz=tz, freq='30T'),
            Timestamp('2016-01-01 00:00:00', tz=tz, freq='30T'),
            Timestamp('2016-01-01 01:00:00', tz=tz, freq='30T'),
            Timestamp('2016-01-01 02:00:00', tz=tz, freq='30T'),
            Timestamp('2016-01-01 02:00:00', tz=tz, freq='30T'),
        ])
        expected_elt = expected_rng[1]

        tm.assert_index_equal(rng.round(freq='H'), expected_rng)
        assert elt.round(freq='H') == expected_elt

        msg = pd._libs.tslibs.frequencies.INVALID_FREQ_ERR_MSG
        with pytest.raises(ValueError, match=msg):
            rng.round(freq='foo')
        with pytest.raises(ValueError, match=msg):
            elt.round(freq='foo')

        msg = "<MonthEnd> is a non-fixed frequency"
        with pytest.raises(ValueError, match=msg):
            rng.round(freq='M')
        with pytest.raises(ValueError, match=msg):
            elt.round(freq='M')

        # GH#14440 & GH#15578
        index = DatetimeIndex(['2016-10-17 12:00:00.0015'], tz=tz)
        result = index.round('ms')
        expected = DatetimeIndex(['2016-10-17 12:00:00.002000'], tz=tz)
        tm.assert_index_equal(result, expected)

        for freq in ['us', 'ns']:
            tm.assert_index_equal(index, index.round(freq))

        index = DatetimeIndex(['2016-10-17 12:00:00.00149'], tz=tz)
        result = index.round('ms')
        expected = DatetimeIndex(['2016-10-17 12:00:00.001000'], tz=tz)
        tm.assert_index_equal(result, expected)

        index = DatetimeIndex(['2016-10-17 12:00:00.001501031'])
        result = index.round('10ns')
        expected = DatetimeIndex(['2016-10-17 12:00:00.001501030'])
        tm.assert_index_equal(result, expected)

        with tm.assert_produces_warning(False):
            ts = '2016-10-17 12:00:00.001501031'
            DatetimeIndex([ts]).round('1010ns')
Пример #4
0
    def test_round_int64(self, start, index_freq, periods, round_freq):
        dt = DatetimeIndex(start=start, freq=index_freq, periods=periods)
        unit = to_offset(round_freq).nanos

        # test floor
        result = dt.floor(round_freq)
        diff = dt.asi8 - result.asi8
        mod = result.asi8 % unit
        assert (mod == 0).all(), "floor not a {} multiple".format(round_freq)
        assert (0 <= diff).all() and (diff < unit).all(), "floor error"

        # test ceil
        result = dt.ceil(round_freq)
        diff = result.asi8 - dt.asi8
        mod = result.asi8 % unit
        assert (mod == 0).all(), "ceil not a {} multiple".format(round_freq)
        assert (0 <= diff).all() and (diff < unit).all(), "ceil error"

        # test round
        result = dt.round(round_freq)
        diff = abs(result.asi8 - dt.asi8)
        mod = result.asi8 % unit
        assert (mod == 0).all(), "round not a {} multiple".format(round_freq)
        assert (diff <= unit // 2).all(), "round error"
        if unit % 2 == 0:
            assert (
                result.asi8[diff == unit // 2] % 2 == 0
            ).all(), "round half to even error"
Пример #5
0
    def test_round_int64(self, start, index_freq, periods, round_freq):
        dt = DatetimeIndex(start=start, freq=index_freq, periods=periods)
        unit = to_offset(round_freq).nanos

        # test floor
        result = dt.floor(round_freq)
        diff = dt.asi8 - result.asi8
        mod = result.asi8 % unit
        assert (mod == 0).all(), "floor not a {} multiple".format(round_freq)
        assert (0 <= diff).all() and (diff < unit).all(), "floor error"

        # test ceil
        result = dt.ceil(round_freq)
        diff = result.asi8 - dt.asi8
        mod = result.asi8 % unit
        assert (mod == 0).all(), "ceil not a {} multiple".format(round_freq)
        assert (0 <= diff).all() and (diff < unit).all(), "ceil error"

        # test round
        result = dt.round(round_freq)
        diff = abs(result.asi8 - dt.asi8)
        mod = result.asi8 % unit
        assert (mod == 0).all(), "round not a {} multiple".format(round_freq)
        assert (diff <= unit // 2).all(), "round error"
        if unit % 2 == 0:
            assert (result.asi8[diff == unit // 2] %
                    2 == 0).all(), "round half to even error"