예제 #1
0
    def test_overflow_offset_raises(self):
        # xref https://github.com/statsmodels/statsmodels/issues/3374
        # ends up multiplying really large numbers which overflow

        stamp = Timestamp("2017-01-13 00:00:00")
        offset_overflow = 20169940 * offsets.Day(1)
        msg = ("the add operation between "
               r"\<-?\d+ \* Days\> and \d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2} "
               "will overflow")
        lmsg2 = r"Cannot cast <-?20169940 \* Days> to unit=ns without overflow"

        with pytest.raises(OutOfBoundsTimedelta, match=lmsg2):
            stamp + offset_overflow

        with pytest.raises(OverflowError, match=msg):
            offset_overflow + stamp

        with pytest.raises(OutOfBoundsTimedelta, match=lmsg2):
            stamp - offset_overflow

        # xref https://github.com/pandas-dev/pandas/issues/14080
        # used to crash, so check for proper overflow exception

        stamp = Timestamp("2000/1/1")
        offset_overflow = to_offset("D") * 100**5

        lmsg3 = r"Cannot cast <-?10000000000 \* Days> to unit=ns without overflow"
        with pytest.raises(OutOfBoundsTimedelta, match=lmsg3):
            stamp + offset_overflow

        with pytest.raises(OverflowError, match=msg):
            offset_overflow + stamp

        with pytest.raises(OutOfBoundsTimedelta, match=lmsg3):
            stamp - offset_overflow
예제 #2
0
    def test_overflow_offset_raises(self):
        # xref https://github.com/statsmodels/statsmodels/issues/3374
        # ends up multiplying really large numbers which overflow

        stamp = Timestamp("2017-01-13 00:00:00")
        offset_overflow = 20169940 * offsets.Day(1)
        msg = (
            "the add operation between "
            r"\<-?\d+ \* Days\> and \d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2} "
            "will overflow"
        )
        lmsg = "|".join(
            ["Python int too large to convert to C long", "int too big to convert"]
        )

        with pytest.raises(OverflowError, match=lmsg):
            stamp + offset_overflow

        with pytest.raises(OverflowError, match=msg):
            offset_overflow + stamp

        with pytest.raises(OverflowError, match=lmsg):
            stamp - offset_overflow

        # xref https://github.com/pandas-dev/pandas/issues/14080
        # used to crash, so check for proper overflow exception

        stamp = Timestamp("2000/1/1")
        offset_overflow = to_offset("D") * 100 ** 5

        with pytest.raises(OverflowError, match=lmsg):
            stamp + offset_overflow

        with pytest.raises(OverflowError, match=msg):
            offset_overflow + stamp

        with pytest.raises(OverflowError, match=lmsg):
            stamp - offset_overflow
예제 #3
0
        }, offsets.Second(86401)),
        ({
            "days": -1,
            "seconds": 1
        }, offsets.Second(-86399)),
        ({
            "hours": 1,
            "minutes": 10
        }, offsets.Minute(70)),
        ({
            "hours": 1,
            "minutes": -10
        }, offsets.Minute(50)),
        ({
            "weeks": 1
        }, offsets.Day(7)),
        ({
            "hours": 1
        }, offsets.Hour(1)),
        ({
            "hours": 1
        }, to_offset("60min")),
        ({
            "microseconds": 1
        }, offsets.Micro(1)),
        ({
            "microseconds": 0
        }, offsets.Nano(0)),
    ],
)
def test_to_offset_pd_timedelta(kwargs, expected):
예제 #4
0
@pytest.mark.parametrize(
    "freq_input,expected",
    [
        # Frequency string.
        ("A", (get_freq_code("A")[0], 1)),
        ("3D", (get_freq_code("D")[0], 3)),
        ("-2M", (get_freq_code("M")[0], -2)),
        # Tuple.
        (("D", 1), (get_freq_code("D")[0], 1)),
        (("A", 3), (get_freq_code("A")[0], 3)),
        (("M", -2), (get_freq_code("M")[0], -2)),
        ((5, "T"), (FreqGroup.FR_MIN, 5)),
        # Numeric Tuple.
        ((1000, 1), (1000, 1)),
        # Offsets.
        (offsets.Day(), (get_freq_code("D")[0], 1)),
        (offsets.Day(3), (get_freq_code("D")[0], 3)),
        (offsets.Day(-2), (get_freq_code("D")[0], -2)),
        (offsets.MonthEnd(), (get_freq_code("M")[0], 1)),
        (offsets.MonthEnd(3), (get_freq_code("M")[0], 3)),
        (offsets.MonthEnd(-2), (get_freq_code("M")[0], -2)),
        (offsets.Week(), (get_freq_code("W")[0], 1)),
        (offsets.Week(3), (get_freq_code("W")[0], 3)),
        (offsets.Week(-2), (get_freq_code("W")[0], -2)),
        (offsets.Hour(), (FreqGroup.FR_HR, 1)),
        # Monday is weekday=0.
        (offsets.Week(weekday=1), (get_freq_code("W-TUE")[0], 1)),
        (offsets.Week(3, weekday=0), (get_freq_code("W-MON")[0], 3)),
        (offsets.Week(-2, weekday=4), (get_freq_code("W-FRI")[0], -2)),
    ],
)
예제 #5
0

@pytest.mark.parametrize("freqstr,expected", [("+1d", 1), ("+2h30min", 150)])
def test_to_offset_leading_plus(freqstr, expected):
    result = to_offset(freqstr)
    assert result.n == expected


@pytest.mark.parametrize(
    "kwargs,expected",
    [
        (dict(days=1, seconds=1), offsets.Second(86401)),
        (dict(days=-1, seconds=1), offsets.Second(-86399)),
        (dict(hours=1, minutes=10), offsets.Minute(70)),
        (dict(hours=1, minutes=-10), offsets.Minute(50)),
        (dict(weeks=1), offsets.Day(7)),
        (dict(hours=1), offsets.Hour(1)),
        (dict(hours=1), to_offset("60min")),
        (dict(microseconds=1), offsets.Micro(1)),
        (dict(microseconds=0), offsets.Nano(0)),
    ],
)
def test_to_offset_pd_timedelta(kwargs, expected):
    # see gh-9064
    td = Timedelta(**kwargs)
    result = to_offset(td)
    assert result == expected


@pytest.mark.parametrize(
    "shortcut,expected",