Пример #1
0
    def test_vectorized_offset_addition(self, klass):
        s = klass(
            [
                Timestamp("2000-01-15 00:15:00", tz="US/Central"),
                Timestamp("2000-02-15", tz="US/Central"),
            ],
            name="a",
        )

        with tm.assert_produces_warning(None):
            # GH#22535 check that we don't get a FutureWarning from adding
            # an integer array to PeriodIndex
            result = s + SemiMonthEnd()
            result2 = SemiMonthEnd() + s

        exp = klass(
            [
                Timestamp("2000-01-31 00:15:00", tz="US/Central"),
                Timestamp("2000-02-29", tz="US/Central"),
            ],
            name="a",
        )
        tm.assert_equal(result, exp)
        tm.assert_equal(result2, exp)

        s = klass(
            [
                Timestamp("2000-01-01 00:15:00", tz="US/Central"),
                Timestamp("2000-02-01", tz="US/Central"),
            ],
            name="a",
        )

        with tm.assert_produces_warning(None):
            # GH#22535 check that we don't get a FutureWarning from adding
            # an integer array to PeriodIndex
            result = s + SemiMonthEnd()
            result2 = SemiMonthEnd() + s

        exp = klass(
            [
                Timestamp("2000-01-15 00:15:00", tz="US/Central"),
                Timestamp("2000-02-15", tz="US/Central"),
            ],
            name="a",
        )
        tm.assert_equal(result, exp)
        tm.assert_equal(result2, exp)
Пример #2
0
    def test_offset_whole_year(self):
        dates = (
            datetime(2007, 12, 31),
            datetime(2008, 1, 15),
            datetime(2008, 1, 31),
            datetime(2008, 2, 15),
            datetime(2008, 2, 29),
            datetime(2008, 3, 15),
            datetime(2008, 3, 31),
            datetime(2008, 4, 15),
            datetime(2008, 4, 30),
            datetime(2008, 5, 15),
            datetime(2008, 5, 31),
            datetime(2008, 6, 15),
            datetime(2008, 6, 30),
            datetime(2008, 7, 15),
            datetime(2008, 7, 31),
            datetime(2008, 8, 15),
            datetime(2008, 8, 31),
            datetime(2008, 9, 15),
            datetime(2008, 9, 30),
            datetime(2008, 10, 15),
            datetime(2008, 10, 31),
            datetime(2008, 11, 15),
            datetime(2008, 11, 30),
            datetime(2008, 12, 15),
            datetime(2008, 12, 31),
        )

        for base, exp_date in zip(dates[:-1], dates[1:]):
            assert_offset_equal(SemiMonthEnd(), base, exp_date)

        # ensure .apply_index works as expected
        s = DatetimeIndex(dates[:-1])
        with tm.assert_produces_warning(None):
            # GH#22535 check that we don't get a FutureWarning from adding
            # an integer array to PeriodIndex
            result = SemiMonthEnd() + s

        exp = DatetimeIndex(dates[1:])
        tm.assert_index_equal(result, exp)

        # ensure generating a range with DatetimeIndex gives same result
        result = date_range(start=dates[0], end=dates[-1], freq="SM")
        exp = DatetimeIndex(dates, freq="SM")
        tm.assert_index_equal(result, exp)
Пример #3
0
 def test_is_on_offset(self, case):
     dt, expected = case
     assert_is_on_offset(SemiMonthEnd(), dt, expected)
Пример #4
0
class TestSemiMonthEnd(Base):
    _offset = SemiMonthEnd
    offset1 = _offset()
    offset2 = _offset(2)

    def test_offset_whole_year(self):
        dates = (
            datetime(2007, 12, 31),
            datetime(2008, 1, 15),
            datetime(2008, 1, 31),
            datetime(2008, 2, 15),
            datetime(2008, 2, 29),
            datetime(2008, 3, 15),
            datetime(2008, 3, 31),
            datetime(2008, 4, 15),
            datetime(2008, 4, 30),
            datetime(2008, 5, 15),
            datetime(2008, 5, 31),
            datetime(2008, 6, 15),
            datetime(2008, 6, 30),
            datetime(2008, 7, 15),
            datetime(2008, 7, 31),
            datetime(2008, 8, 15),
            datetime(2008, 8, 31),
            datetime(2008, 9, 15),
            datetime(2008, 9, 30),
            datetime(2008, 10, 15),
            datetime(2008, 10, 31),
            datetime(2008, 11, 15),
            datetime(2008, 11, 30),
            datetime(2008, 12, 15),
            datetime(2008, 12, 31),
        )

        for base, exp_date in zip(dates[:-1], dates[1:]):
            assert_offset_equal(SemiMonthEnd(), base, exp_date)

        # ensure .apply_index works as expected
        s = DatetimeIndex(dates[:-1])
        with tm.assert_produces_warning(None):
            # GH#22535 check that we don't get a FutureWarning from adding
            # an integer array to PeriodIndex
            result = SemiMonthEnd() + s

        exp = DatetimeIndex(dates[1:])
        tm.assert_index_equal(result, exp)

        # ensure generating a range with DatetimeIndex gives same result
        result = date_range(start=dates[0], end=dates[-1], freq="SM")
        exp = DatetimeIndex(dates, freq="SM")
        tm.assert_index_equal(result, exp)

    offset_cases = []
    offset_cases.append((
        SemiMonthEnd(),
        {
            datetime(2008, 1, 1): datetime(2008, 1, 15),
            datetime(2008, 1, 15): datetime(2008, 1, 31),
            datetime(2008, 1, 31): datetime(2008, 2, 15),
            datetime(2006, 12, 14): datetime(2006, 12, 15),
            datetime(2006, 12, 29): datetime(2006, 12, 31),
            datetime(2006, 12, 31): datetime(2007, 1, 15),
            datetime(2007, 1, 1): datetime(2007, 1, 15),
            datetime(2006, 12, 1): datetime(2006, 12, 15),
            datetime(2006, 12, 15): datetime(2006, 12, 31),
        },
    ))

    offset_cases.append((
        SemiMonthEnd(day_of_month=20),
        {
            datetime(2008, 1, 1): datetime(2008, 1, 20),
            datetime(2008, 1, 15): datetime(2008, 1, 20),
            datetime(2008, 1, 21): datetime(2008, 1, 31),
            datetime(2008, 1, 31): datetime(2008, 2, 20),
            datetime(2006, 12, 14): datetime(2006, 12, 20),
            datetime(2006, 12, 29): datetime(2006, 12, 31),
            datetime(2006, 12, 31): datetime(2007, 1, 20),
            datetime(2007, 1, 1): datetime(2007, 1, 20),
            datetime(2006, 12, 1): datetime(2006, 12, 20),
            datetime(2006, 12, 15): datetime(2006, 12, 20),
        },
    ))

    offset_cases.append((
        SemiMonthEnd(0),
        {
            datetime(2008, 1, 1): datetime(2008, 1, 15),
            datetime(2008, 1, 16): datetime(2008, 1, 31),
            datetime(2008, 1, 15): datetime(2008, 1, 15),
            datetime(2008, 1, 31): datetime(2008, 1, 31),
            datetime(2006, 12, 29): datetime(2006, 12, 31),
            datetime(2006, 12, 31): datetime(2006, 12, 31),
            datetime(2007, 1, 1): datetime(2007, 1, 15),
        },
    ))

    offset_cases.append((
        SemiMonthEnd(0, day_of_month=16),
        {
            datetime(2008, 1, 1): datetime(2008, 1, 16),
            datetime(2008, 1, 16): datetime(2008, 1, 16),
            datetime(2008, 1, 15): datetime(2008, 1, 16),
            datetime(2008, 1, 31): datetime(2008, 1, 31),
            datetime(2006, 12, 29): datetime(2006, 12, 31),
            datetime(2006, 12, 31): datetime(2006, 12, 31),
            datetime(2007, 1, 1): datetime(2007, 1, 16),
        },
    ))

    offset_cases.append((
        SemiMonthEnd(2),
        {
            datetime(2008, 1, 1): datetime(2008, 1, 31),
            datetime(2008, 1, 31): datetime(2008, 2, 29),
            datetime(2006, 12, 29): datetime(2007, 1, 15),
            datetime(2006, 12, 31): datetime(2007, 1, 31),
            datetime(2007, 1, 1): datetime(2007, 1, 31),
            datetime(2007, 1, 16): datetime(2007, 2, 15),
            datetime(2006, 11, 1): datetime(2006, 11, 30),
        },
    ))

    offset_cases.append((
        SemiMonthEnd(-1),
        {
            datetime(2007, 1, 1): datetime(2006, 12, 31),
            datetime(2008, 6, 30): datetime(2008, 6, 15),
            datetime(2008, 12, 31): datetime(2008, 12, 15),
            datetime(2006, 12, 29): datetime(2006, 12, 15),
            datetime(2006, 12, 30): datetime(2006, 12, 15),
            datetime(2007, 1, 1): datetime(2006, 12, 31),
        },
    ))

    offset_cases.append((
        SemiMonthEnd(-1, day_of_month=4),
        {
            datetime(2007, 1, 1): datetime(2006, 12, 31),
            datetime(2007, 1, 4): datetime(2006, 12, 31),
            datetime(2008, 6, 30): datetime(2008, 6, 4),
            datetime(2008, 12, 31): datetime(2008, 12, 4),
            datetime(2006, 12, 5): datetime(2006, 12, 4),
            datetime(2006, 12, 30): datetime(2006, 12, 4),
            datetime(2007, 1, 1): datetime(2006, 12, 31),
        },
    ))

    offset_cases.append((
        SemiMonthEnd(-2),
        {
            datetime(2007, 1, 1): datetime(2006, 12, 15),
            datetime(2008, 6, 30): datetime(2008, 5, 31),
            datetime(2008, 3, 15): datetime(2008, 2, 15),
            datetime(2008, 12, 31): datetime(2008, 11, 30),
            datetime(2006, 12, 29): datetime(2006, 11, 30),
            datetime(2006, 12, 14): datetime(2006, 11, 15),
            datetime(2007, 1, 1): datetime(2006, 12, 15),
        },
    ))

    @pytest.mark.parametrize("case", offset_cases)
    def test_offset(self, case):
        offset, cases = case
        for base, expected in cases.items():
            assert_offset_equal(offset, base, expected)

    @pytest.mark.parametrize("case", offset_cases)
    def test_apply_index(self, case):
        # https://github.com/pandas-dev/pandas/issues/34580
        offset, cases = case
        s = DatetimeIndex(cases.keys())
        exp = DatetimeIndex(cases.values())

        with tm.assert_produces_warning(None):
            # GH#22535 check that we don't get a FutureWarning from adding
            # an integer array to PeriodIndex
            result = offset + s
        tm.assert_index_equal(result, exp)

        with tm.assert_produces_warning(FutureWarning):
            result = offset.apply_index(s)
        tm.assert_index_equal(result, exp)

    on_offset_cases = [
        (datetime(2007, 12, 31), True),
        (datetime(2007, 12, 15), True),
        (datetime(2007, 12, 14), False),
        (datetime(2007, 12, 1), False),
        (datetime(2008, 2, 29), True),
    ]

    @pytest.mark.parametrize("case", on_offset_cases)
    def test_is_on_offset(self, case):
        dt, expected = case
        assert_is_on_offset(SemiMonthEnd(), dt, expected)

    @pytest.mark.parametrize("klass", [Series, DatetimeIndex])
    def test_vectorized_offset_addition(self, klass):
        s = klass(
            [
                Timestamp("2000-01-15 00:15:00", tz="US/Central"),
                Timestamp("2000-02-15", tz="US/Central"),
            ],
            name="a",
        )

        with tm.assert_produces_warning(None):
            # GH#22535 check that we don't get a FutureWarning from adding
            # an integer array to PeriodIndex
            result = s + SemiMonthEnd()
            result2 = SemiMonthEnd() + s

        exp = klass(
            [
                Timestamp("2000-01-31 00:15:00", tz="US/Central"),
                Timestamp("2000-02-29", tz="US/Central"),
            ],
            name="a",
        )
        tm.assert_equal(result, exp)
        tm.assert_equal(result2, exp)

        s = klass(
            [
                Timestamp("2000-01-01 00:15:00", tz="US/Central"),
                Timestamp("2000-02-01", tz="US/Central"),
            ],
            name="a",
        )

        with tm.assert_produces_warning(None):
            # GH#22535 check that we don't get a FutureWarning from adding
            # an integer array to PeriodIndex
            result = s + SemiMonthEnd()
            result2 = SemiMonthEnd() + s

        exp = klass(
            [
                Timestamp("2000-01-15 00:15:00", tz="US/Central"),
                Timestamp("2000-02-15", tz="US/Central"),
            ],
            name="a",
        )
        tm.assert_equal(result, exp)
        tm.assert_equal(result2, exp)
Пример #5
0
     (
         Timestamp("1900-01-01"),
         Timestamp("1905-07-01"),
         MonthBegin(66),
         "Africa/Kinshasa",
     ),  # GH41906
     (
         Timestamp("2021-10-01 01:15"),
         Timestamp("2021-10-31 01:15"),
         MonthEnd(1),
         "Europe/London",
     ),
     (
         Timestamp("2010-12-05 02:59"),
         Timestamp("2010-10-31 02:59"),
         SemiMonthEnd(-3),
         "Europe/Paris",
     ),
     (
         Timestamp("2021-10-31 01:20"),
         Timestamp("2021-11-07 01:20"),
         CustomBusinessDay(2, weekmask="Sun Mon"),
         "US/Eastern",
     ),
     (
         Timestamp("2020-04-03 01:30"),
         Timestamp("2020-11-01 01:30"),
         YearBegin(1, month=11),
         "America/Chicago",
     ),
 ],