예제 #1
0
def test_maybe_convert_timedelta():
    pi = PeriodIndex(['2000', '2001'], freq='D')
    offset = offsets.Day(2)
    assert pi._maybe_convert_timedelta(offset) == 2
    assert pi._maybe_convert_timedelta(2) == 2

    offset = offsets.BusinessDay()
    with pytest.raises(ValueError, match='freq'):
        pi._maybe_convert_timedelta(offset)
예제 #2
0
def test_maybe_convert_timedelta():
    pi = PeriodIndex(["2000", "2001"], freq="D")
    offset = offsets.Day(2)
    assert pi._maybe_convert_timedelta(offset) == 2
    assert pi._maybe_convert_timedelta(2) == 2

    offset = offsets.BusinessDay()
    msg = r"Input has different freq=B from PeriodIndex\(freq=D\)"
    with pytest.raises(ValueError, match=msg):
        pi._maybe_convert_timedelta(offset)
예제 #3
0
    def test_period_constructor_offsets(self):
        assert (Period('1/1/2005', freq=offsets.MonthEnd()) ==
                Period('1/1/2005', freq='M'))
        assert (Period('2005', freq=offsets.YearEnd()) ==
                Period('2005', freq='A'))
        assert (Period('2005', freq=offsets.MonthEnd()) ==
                Period('2005', freq='M'))
        assert (Period('3/10/12', freq=offsets.BusinessDay()) ==
                Period('3/10/12', freq='B'))
        assert (Period('3/10/12', freq=offsets.Day()) ==
                Period('3/10/12', freq='D'))

        assert (Period(year=2005, quarter=1,
                       freq=offsets.QuarterEnd(startingMonth=12)) ==
                Period(year=2005, quarter=1, freq='Q'))
        assert (Period(year=2005, quarter=2,
                       freq=offsets.QuarterEnd(startingMonth=12)) ==
                Period(year=2005, quarter=2, freq='Q'))

        assert (Period(year=2005, month=3, day=1, freq=offsets.Day()) ==
                Period(year=2005, month=3, day=1, freq='D'))
        assert (Period(year=2012, month=3, day=10, freq=offsets.BDay()) ==
                Period(year=2012, month=3, day=10, freq='B'))

        expected = Period('2005-03-01', freq='3D')
        assert (Period(year=2005, month=3, day=1,
                       freq=offsets.Day(3)) == expected)
        assert Period(year=2005, month=3, day=1, freq='3D') == expected

        assert (Period(year=2012, month=3, day=10,
                       freq=offsets.BDay(3)) ==
                Period(year=2012, month=3, day=10, freq='3B'))

        assert (Period(200701, freq=offsets.MonthEnd()) ==
                Period(200701, freq='M'))

        i1 = Period(ordinal=200701, freq=offsets.MonthEnd())
        i2 = Period(ordinal=200701, freq='M')
        assert i1 == i2
        assert i1.year == 18695
        assert i2.year == 18695

        i1 = Period(datetime(2007, 1, 1), freq='M')
        i2 = Period('200701', freq='M')
        assert i1 == i2

        i1 = Period(date(2007, 1, 1), freq='M')
        i2 = Period(datetime(2007, 1, 1), freq='M')
        i3 = Period(np.datetime64('2007-01-01'), freq='M')
        i4 = Period(np_datetime64_compat('2007-01-01 00:00:00Z'), freq='M')
        i5 = Period(np_datetime64_compat('2007-01-01 00:00:00.000Z'), freq='M')
        assert i1 == i2
        assert i1 == i3
        assert i1 == i4
        assert i1 == i5

        i1 = Period('2007-01-01 09:00:00.001')
        expected = Period(datetime(2007, 1, 1, 9, 0, 0, 1000), freq='L')
        assert i1 == expected

        expected = Period(np_datetime64_compat(
            '2007-01-01 09:00:00.001Z'), freq='L')
        assert i1 == expected

        i1 = Period('2007-01-01 09:00:00.00101')
        expected = Period(datetime(2007, 1, 1, 9, 0, 0, 1010), freq='U')
        assert i1 == expected

        expected = Period(np_datetime64_compat('2007-01-01 09:00:00.00101Z'),
                          freq='U')
        assert i1 == expected

        pytest.raises(ValueError, Period, ordinal=200701)

        pytest.raises(ValueError, Period, '2007-1-1', freq='X')
예제 #4
0
파일: offsets.py 프로젝트: zuku1985/pandas
    offsets.BMonthBegin(),
    offsets.CustomBusinessDay(),
    offsets.CustomBusinessDay(calendar=hcal),
    offsets.CustomBusinessMonthBegin(calendar=hcal),
    offsets.CustomBusinessMonthEnd(calendar=hcal),
    offsets.CustomBusinessMonthEnd(calendar=hcal),
]
other_offsets = [
    offsets.YearEnd(),
    offsets.YearBegin(),
    offsets.QuarterEnd(),
    offsets.QuarterBegin(),
    offsets.MonthEnd(),
    offsets.MonthBegin(),
    offsets.DateOffset(months=2, days=2),
    offsets.BusinessDay(),
    offsets.SemiMonthEnd(),
    offsets.SemiMonthBegin(),
]
offset_objs = non_apply + other_offsets


class OnOffset:

    params = offset_objs
    param_names = ["offset"]

    def setup(self, offset):
        self.dates = [
            datetime(2016, m, d) for m in [10, 11, 12]
            for d in [1, 2, 3, 28, 29, 30, 31] if not (m == 11 and d == 31)
예제 #5
0
    def test_period_constructor_offsets(self):
        assert Period("1/1/2005", freq=offsets.MonthEnd()) == Period(
            "1/1/2005", freq="M"
        )
        assert Period("2005", freq=offsets.YearEnd()) == Period("2005", freq="A")
        assert Period("2005", freq=offsets.MonthEnd()) == Period("2005", freq="M")
        assert Period("3/10/12", freq=offsets.BusinessDay()) == Period(
            "3/10/12", freq="B"
        )
        assert Period("3/10/12", freq=offsets.Day()) == Period("3/10/12", freq="D")

        assert Period(
            year=2005, quarter=1, freq=offsets.QuarterEnd(startingMonth=12)
        ) == Period(year=2005, quarter=1, freq="Q")
        assert Period(
            year=2005, quarter=2, freq=offsets.QuarterEnd(startingMonth=12)
        ) == Period(year=2005, quarter=2, freq="Q")

        assert Period(year=2005, month=3, day=1, freq=offsets.Day()) == Period(
            year=2005, month=3, day=1, freq="D"
        )
        assert Period(year=2012, month=3, day=10, freq=offsets.BDay()) == Period(
            year=2012, month=3, day=10, freq="B"
        )

        expected = Period("2005-03-01", freq="3D")
        assert Period(year=2005, month=3, day=1, freq=offsets.Day(3)) == expected
        assert Period(year=2005, month=3, day=1, freq="3D") == expected

        assert Period(year=2012, month=3, day=10, freq=offsets.BDay(3)) == Period(
            year=2012, month=3, day=10, freq="3B"
        )

        assert Period(200701, freq=offsets.MonthEnd()) == Period(200701, freq="M")

        i1 = Period(ordinal=200701, freq=offsets.MonthEnd())
        i2 = Period(ordinal=200701, freq="M")
        assert i1 == i2
        assert i1.year == 18695
        assert i2.year == 18695

        i1 = Period(datetime(2007, 1, 1), freq="M")
        i2 = Period("200701", freq="M")
        assert i1 == i2

        i1 = Period(date(2007, 1, 1), freq="M")
        i2 = Period(datetime(2007, 1, 1), freq="M")
        i3 = Period(np.datetime64("2007-01-01"), freq="M")
        i4 = Period(np_datetime64_compat("2007-01-01 00:00:00Z"), freq="M")
        i5 = Period(np_datetime64_compat("2007-01-01 00:00:00.000Z"), freq="M")
        assert i1 == i2
        assert i1 == i3
        assert i1 == i4
        assert i1 == i5

        i1 = Period("2007-01-01 09:00:00.001")
        expected = Period(datetime(2007, 1, 1, 9, 0, 0, 1000), freq="L")
        assert i1 == expected

        expected = Period(np_datetime64_compat("2007-01-01 09:00:00.001Z"), freq="L")
        assert i1 == expected

        i1 = Period("2007-01-01 09:00:00.00101")
        expected = Period(datetime(2007, 1, 1, 9, 0, 0, 1010), freq="U")
        assert i1 == expected

        expected = Period(np_datetime64_compat("2007-01-01 09:00:00.00101Z"), freq="U")
        assert i1 == expected