示例#1
0
 def test_repr(self):
     expected = "<BusinessQuarterEnd: startingMonth=3>"
     assert repr(BQuarterEnd()) == expected
     expected = "<BusinessQuarterEnd: startingMonth=3>"
     assert repr(BQuarterEnd(startingMonth=3)) == expected
     expected = "<BusinessQuarterEnd: startingMonth=1>"
     assert repr(BQuarterEnd(startingMonth=1)) == expected
    def manipulate_yield_data(df_treasury):
        # Take out 2 month yield and only use 24 months of data
        df_treasury = df_treasury.drop(['2 MO'], axis=1).last('24M')
        df_treasury.reset_index(inplace=True)

        # Get the data for the last day of each business quarter for the
        # last 24 months.
        df_us_treasury = df_treasury.loc[df_treasury.Date.isin(
            df_treasury.Date + BQuarterEnd())]

        # Once every 5 or 6 years the end of the first business
        # quarter is Good Friday. The financial markets in the US
        # are closed on Good Friday. When this occurs we have to get
        # data from the day before Good Friday.

        # Create a list of the years that are in the df_treasury dataframe
        df_dates = pd.to_datetime(df_treasury['Date']).dt.year.unique()

        for date in df_dates:
            # Calculate Good Friday. It's two days before Easter Sunday
            goodfriday = easter(date) + timedelta(days=-2)
            # Calculate the end of the business quarter for the quarter that
            # Good Friday is in.
            Bqtr_end_date = (pd.to_datetime(goodfriday) +
                             BQuarterEnd(0)).date()

            # check to see if Good Friday is the last day of the business quarter
            if goodfriday == Bqtr_end_date:

                # Subtract one day from Good Friday to get financial end of qtr
                end_of_qtr = pd.to_datetime(goodfriday + timedelta(days=-1))
                # Get the row in df_treasury with the information that we need
                df_temp = df_treasury[df_treasury.Date == end_of_qtr]
                # Add the dataframe with the one record that we need to the
                # dataframe with the business quarter end data
                df_us_treasury = pd.concat([df_us_treasury, df_temp])

        # Add data from most recent date in df_treasury to df_us_treasury
        df_us_treasury = df_us_treasury.append(df_treasury.iloc[-1])
        # Update the index so that the data prints out correctly
        df_us_treasury = df_us_treasury.sort_values(by=['Date'],
                                                    ascending=False)
        df_treasury = df_treasury.sort_values(by=['Date'], ascending=False)
        df_us_treasury['Date'] = pd.to_datetime(df_us_treasury['Date'],
                                                format='%Y-%m-%d').dt.date
        return df_us_treasury, df_treasury
示例#3
0
    def manipulate_yield_data(df_treasury):
        # Take out 2 month yield and only use last two years data
        df_treasury = df_treasury.drop(['2 MO'], axis=1).last('2Y')
        df_treasury.reset_index(inplace=True)

        # Get data from last day of each business quarter
        # Add data from the most recent day we have
        # Transpose the dataframe
        df_us_treasury = df_treasury.loc[df_treasury.Date.isin(
            df_treasury.Date + BQuarterEnd())]

        # Once every 5 or 6 years the end of the first business
        # quarter is Good Friday. The financial markets in the US
        # are closed on Good Friday. When this occurs we have to get
        # data from the day before Good Friday.

        # Create a list of the years that are in the df_treasury dataframe
        df_dates = pd.to_datetime(df_treasury['Date']).dt.year.unique()

        for date in df_dates:
            # Calculate Good Friday. It's two days before Easter Sunday
            goodfriday = easter(date) + timedelta(days=-2)
            # Calculate the end of the business quarter for the quarter that
            # Good Friday is in.
            Bqtr_end_date = (pd.to_datetime(goodfriday) +
                             BQuarterEnd(0)).date()

            # check to see if Good Friday is the last day of the business quarter
            if goodfriday == Bqtr_end_date:

                # Subtract one day from Good Friday to get financial end of qtr
                end_of_qtr = pd.to_datetime(goodfriday + timedelta(days=-1))
                # Get the row in df_treasury with the information that we need
                df_temp = df_treasury[df_treasury.Date == end_of_qtr]
                # Add the dataframe with the one record that we need to the
                # dataframe with the business quarter end data
                df_us_treasury = pd.concat([df_us_treasury, df_temp])

        df_us_treasury = (df_us_treasury.append(df_treasury.iloc[-1],
                                                ignore_index=True)).T
        return df_us_treasury, len(
            df_us_treasury.index), len(df_us_treasury.columns) + 1
示例#4
0
    # 'QS'     : QuarterBegin(startingMonth=1),
    'QS-JAN' : QuarterBegin(startingMonth=1),
    'QS-FEB' : QuarterBegin(startingMonth=2),
    'QS-MAR' : QuarterBegin(startingMonth=3),
    'QS-APR' : QuarterBegin(startingMonth=4),
    'QS-MAY' : QuarterBegin(startingMonth=5),
    'QS-JUN' : QuarterBegin(startingMonth=6),
    'QS-JUL' : QuarterBegin(startingMonth=7),
    'QS-AUG' : QuarterBegin(startingMonth=8),
    'QS-SEP' : QuarterBegin(startingMonth=9),
    'QS-OCT' : QuarterBegin(startingMonth=10),
    'QS-NOV' : QuarterBegin(startingMonth=11),
    'QS-DEC' : QuarterBegin(startingMonth=12),

    # Quarterly - Business
    'BQ-JAN' : BQuarterEnd(startingMonth=1),
    'BQ-FEB' : BQuarterEnd(startingMonth=2),
    'BQ-MAR' : BQuarterEnd(startingMonth=3),

    # 'BQ'     : BQuarterEnd(startingMonth=3),
    'BQ-APR' : BQuarterEnd(startingMonth=4),
    'BQ-MAY' : BQuarterEnd(startingMonth=5),
    'BQ-JUN' : BQuarterEnd(startingMonth=6),
    'BQ-JUL' : BQuarterEnd(startingMonth=7),
    'BQ-AUG' : BQuarterEnd(startingMonth=8),
    'BQ-SEP' : BQuarterEnd(startingMonth=9),
    'BQ-OCT' : BQuarterEnd(startingMonth=10),
    'BQ-NOV' : BQuarterEnd(startingMonth=11),
    'BQ-DEC' : BQuarterEnd(startingMonth=12),

    # Quarterly - Business (Start)
示例#5
0
 def test_offset_corner_case(self):
     # corner
     offset = BQuarterEnd(n=-1, startingMonth=1)
     assert datetime(2010, 1, 31) + offset == datetime(2010, 1, 29)
示例#6
0
 def test_isAnchored(self):
     assert BQuarterEnd(startingMonth=1).isAnchored()
     assert BQuarterEnd().isAnchored()
     assert not BQuarterEnd(2, startingMonth=1).isAnchored()
示例#7
0
class TestBQuarterEnd(Base):
    _offset = BQuarterEnd

    def test_repr(self):
        expected = "<BusinessQuarterEnd: startingMonth=3>"
        assert repr(BQuarterEnd()) == expected
        expected = "<BusinessQuarterEnd: startingMonth=3>"
        assert repr(BQuarterEnd(startingMonth=3)) == expected
        expected = "<BusinessQuarterEnd: startingMonth=1>"
        assert repr(BQuarterEnd(startingMonth=1)) == expected

    def test_isAnchored(self):
        assert BQuarterEnd(startingMonth=1).isAnchored()
        assert BQuarterEnd().isAnchored()
        assert not BQuarterEnd(2, startingMonth=1).isAnchored()

    def test_offset_corner_case(self):
        # corner
        offset = BQuarterEnd(n=-1, startingMonth=1)
        assert datetime(2010, 1, 31) + offset == datetime(2010, 1, 29)

    offset_cases = []
    offset_cases.append((BQuarterEnd(startingMonth=1), {
        datetime(2008, 1, 1): datetime(2008, 1, 31),
        datetime(2008, 1, 31): datetime(2008, 4, 30),
        datetime(2008, 2, 15): datetime(2008, 4, 30),
        datetime(2008, 2, 29): datetime(2008, 4, 30),
        datetime(2008, 3, 15): datetime(2008, 4, 30),
        datetime(2008, 3, 31): datetime(2008, 4, 30),
        datetime(2008, 4, 15): datetime(2008, 4, 30),
        datetime(2008, 4, 30): datetime(2008, 7, 31)}))

    offset_cases.append((BQuarterEnd(startingMonth=2), {
        datetime(2008, 1, 1): datetime(2008, 2, 29),
        datetime(2008, 1, 31): datetime(2008, 2, 29),
        datetime(2008, 2, 15): datetime(2008, 2, 29),
        datetime(2008, 2, 29): datetime(2008, 5, 30),
        datetime(2008, 3, 15): datetime(2008, 5, 30),
        datetime(2008, 3, 31): datetime(2008, 5, 30),
        datetime(2008, 4, 15): datetime(2008, 5, 30),
        datetime(2008, 4, 30): datetime(2008, 5, 30)}))

    offset_cases.append((BQuarterEnd(startingMonth=1, n=0), {
        datetime(2008, 1, 1): datetime(2008, 1, 31),
        datetime(2008, 1, 31): datetime(2008, 1, 31),
        datetime(2008, 2, 15): datetime(2008, 4, 30),
        datetime(2008, 2, 29): datetime(2008, 4, 30),
        datetime(2008, 3, 15): datetime(2008, 4, 30),
        datetime(2008, 3, 31): datetime(2008, 4, 30),
        datetime(2008, 4, 15): datetime(2008, 4, 30),
        datetime(2008, 4, 30): datetime(2008, 4, 30)}))

    offset_cases.append((BQuarterEnd(startingMonth=1, n=-1), {
        datetime(2008, 1, 1): datetime(2007, 10, 31),
        datetime(2008, 1, 31): datetime(2007, 10, 31),
        datetime(2008, 2, 15): datetime(2008, 1, 31),
        datetime(2008, 2, 29): datetime(2008, 1, 31),
        datetime(2008, 3, 15): datetime(2008, 1, 31),
        datetime(2008, 3, 31): datetime(2008, 1, 31),
        datetime(2008, 4, 15): datetime(2008, 1, 31),
        datetime(2008, 4, 30): datetime(2008, 1, 31)}))

    offset_cases.append((BQuarterEnd(startingMonth=1, n=2), {
        datetime(2008, 1, 31): datetime(2008, 7, 31),
        datetime(2008, 2, 15): datetime(2008, 7, 31),
        datetime(2008, 2, 29): datetime(2008, 7, 31),
        datetime(2008, 3, 15): datetime(2008, 7, 31),
        datetime(2008, 3, 31): datetime(2008, 7, 31),
        datetime(2008, 4, 15): datetime(2008, 7, 31),
        datetime(2008, 4, 30): datetime(2008, 10, 31)}))

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

    on_offset_cases = [
        (BQuarterEnd(1, startingMonth=1), datetime(2008, 1, 31), True),
        (BQuarterEnd(1, startingMonth=1), datetime(2007, 12, 31), False),
        (BQuarterEnd(1, startingMonth=1), datetime(2008, 2, 29), False),
        (BQuarterEnd(1, startingMonth=1), datetime(2007, 3, 30), False),
        (BQuarterEnd(1, startingMonth=1), datetime(2007, 3, 31), False),
        (BQuarterEnd(1, startingMonth=1), datetime(2008, 4, 30), True),
        (BQuarterEnd(1, startingMonth=1), datetime(2008, 5, 30), False),
        (BQuarterEnd(1, startingMonth=1), datetime(2007, 6, 29), False),
        (BQuarterEnd(1, startingMonth=1), datetime(2007, 6, 30), False),
        (BQuarterEnd(1, startingMonth=2), datetime(2008, 1, 31), False),
        (BQuarterEnd(1, startingMonth=2), datetime(2007, 12, 31), False),
        (BQuarterEnd(1, startingMonth=2), datetime(2008, 2, 29), True),
        (BQuarterEnd(1, startingMonth=2), datetime(2007, 3, 30), False),
        (BQuarterEnd(1, startingMonth=2), datetime(2007, 3, 31), False),
        (BQuarterEnd(1, startingMonth=2), datetime(2008, 4, 30), False),
        (BQuarterEnd(1, startingMonth=2), datetime(2008, 5, 30), True),
        (BQuarterEnd(1, startingMonth=2), datetime(2007, 6, 29), False),
        (BQuarterEnd(1, startingMonth=2), datetime(2007, 6, 30), False),
        (BQuarterEnd(1, startingMonth=3), datetime(2008, 1, 31), False),
        (BQuarterEnd(1, startingMonth=3), datetime(2007, 12, 31), True),
        (BQuarterEnd(1, startingMonth=3), datetime(2008, 2, 29), False),
        (BQuarterEnd(1, startingMonth=3), datetime(2007, 3, 30), True),
        (BQuarterEnd(1, startingMonth=3), datetime(2007, 3, 31), False),
        (BQuarterEnd(1, startingMonth=3), datetime(2008, 4, 30), False),
        (BQuarterEnd(1, startingMonth=3), datetime(2008, 5, 30), False),
        (BQuarterEnd(1, startingMonth=3), datetime(2007, 6, 29), True),
        (BQuarterEnd(1, startingMonth=3), datetime(2007, 6, 30), False)]

    @pytest.mark.parametrize('case', on_offset_cases)
    def test_onOffset(self, case):
        offset, dt, expected = case
        assert_onOffset(offset, dt, expected)
示例#8
0
    rng = pd.date_range(start='1/1/2000', periods=100000, freq='T')
    ser = pd.Series(rng)

    res = rng + offset
    res_v2 = offset.apply_index(rng)
    assert (res == res_v2).all()
    assert res[0] == rng[0] + offset
    assert res[-1] == rng[-1] + offset
    res2 = ser + offset
    # apply_index is only for indexes, not series, so no res2_v2
    assert res2.iloc[0] == ser.iloc[0] + offset
    assert res2.iloc[-1] == ser.iloc[-1] + offset


@pytest.mark.parametrize('offset', [QuarterBegin(), QuarterEnd(),
                                    BQuarterBegin(), BQuarterEnd()])
def test_on_offset(offset):
    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)]
    for date in dates:
        res = offset.onOffset(date)
        slow_version = date == (date + offset) - offset
        assert res == slow_version


# --------------------------------------------------------------------
# Months

class TestMonthBegin(Base):
    _offset = MonthBegin
示例#9
0
    ser = pd.Series(rng)

    res = rng + offset
    assert res.freq is None  # not retained
    res_v2 = offset.apply_index(rng)
    assert (res == res_v2).all()
    assert res[0] == rng[0] + offset
    assert res[-1] == rng[-1] + offset
    res2 = ser + offset
    # apply_index is only for indexes, not series, so no res2_v2
    assert res2.iloc[0] == ser.iloc[0] + offset
    assert res2.iloc[-1] == ser.iloc[-1] + offset


@pytest.mark.parametrize(
    "offset", [QuarterBegin(), QuarterEnd(), BQuarterBegin(), BQuarterEnd()]
)
def test_on_offset(offset):
    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)
    ]
    for date in dates:
        res = offset.is_on_offset(date)
        slow_version = date == (date + offset) - offset
        assert res == slow_version


# --------------------------------------------------------------------
示例#10
0
    res = rng + offset
    res_v2 = offset.apply_index(rng)
    assert (res == res_v2).all()
    assert res[0] == rng[0] + offset
    assert res[-1] == rng[-1] + offset
    res2 = ser + offset
    # apply_index is only for indexes, not series, so no res2_v2
    assert res2.iloc[0] == ser.iloc[0] + offset
    assert res2.iloc[-1] == ser.iloc[-1] + offset


@pytest.mark.parametrize(
    'offset', [QuarterBegin(),
               QuarterEnd(),
               BQuarterBegin(),
               BQuarterEnd()])
def test_on_offset(offset):
    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)
    ]
    for date in dates:
        res = offset.onOffset(date)
        slow_version = date == (date + offset) - offset
        assert res == slow_version


# --------------------------------------------------------------------
# Months

示例#11
0
    BQuarterBegin,
    BQuarterEnd,
)


def test_quarterly_dont_normalize():
    date = datetime(2012, 3, 31, 5, 30)

    offsets = (BQuarterEnd, BQuarterBegin)

    for klass in offsets:
        result = date + klass()
        assert result.time() == date.time()


@pytest.mark.parametrize("offset", [BQuarterBegin(), BQuarterEnd()])
def test_on_offset(offset):
    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)
    ]
    for date in dates:
        res = offset.is_on_offset(date)
        slow_version = date == (date + offset) - offset
        assert res == slow_version


class TestBQuarterBegin(Base):
    _offset: type[QuarterOffset] = BQuarterBegin

    def test_repr(self):