Пример #1
0
 def test_datetimeindex_accessors3(self):
     # Ensure is_start/end accessors throw ValueError for CustomBusinessDay,
     bday_egypt = offsets.CustomBusinessDay(weekmask="Sun Mon Tue Wed Thu")
     dti = date_range(datetime(2013, 4, 30), periods=5, freq=bday_egypt)
     msg = "Custom business days is not supported by is_month_start"
     with pytest.raises(ValueError, match=msg):
         dti.is_month_start
Пример #2
0
def test_calendar_2031():
    # See gh-27790
    #
    # Labor Day 2031 is on September 1. Saturday before is August 30.
    # Next working day after August 30 ought to be Tuesday, September 2.

    class testCalendar(AbstractHolidayCalendar):
        rules = [USLaborDay]

    cal = testCalendar()
    workDay = offsets.CustomBusinessDay(calendar=cal)
    Sat_before_Labor_Day_2031 = to_datetime("2031-08-30")
    next_working_day = Sat_before_Labor_Day_2031 + 0 * workDay
    assert next_working_day == to_datetime("2031-09-02")
Пример #3
0
    def test_datetimeindex_accessors(self):
        dti_naive = DatetimeIndex(freq='D', start=datetime(1998, 1, 1),
                                  periods=365)
        # GH 13303
        dti_tz = DatetimeIndex(freq='D', start=datetime(1998, 1, 1),
                               periods=365, tz='US/Eastern')
        for dti in [dti_naive, dti_tz]:

            assert dti.year[0] == 1998
            assert dti.month[0] == 1
            assert dti.day[0] == 1
            assert dti.hour[0] == 0
            assert dti.minute[0] == 0
            assert dti.second[0] == 0
            assert dti.microsecond[0] == 0
            assert dti.dayofweek[0] == 3

            assert dti.dayofyear[0] == 1
            assert dti.dayofyear[120] == 121

            assert dti.weekofyear[0] == 1
            assert dti.weekofyear[120] == 18

            assert dti.quarter[0] == 1
            assert dti.quarter[120] == 2

            assert dti.days_in_month[0] == 31
            assert dti.days_in_month[90] == 30

            assert dti.is_month_start[0]
            assert not dti.is_month_start[1]
            assert dti.is_month_start[31]
            assert dti.is_quarter_start[0]
            assert dti.is_quarter_start[90]
            assert dti.is_year_start[0]
            assert not dti.is_year_start[364]
            assert not dti.is_month_end[0]
            assert dti.is_month_end[30]
            assert not dti.is_month_end[31]
            assert dti.is_month_end[364]
            assert not dti.is_quarter_end[0]
            assert not dti.is_quarter_end[30]
            assert dti.is_quarter_end[89]
            assert dti.is_quarter_end[364]
            assert not dti.is_year_end[0]
            assert dti.is_year_end[364]

            assert len(dti.year) == 365
            assert len(dti.month) == 365
            assert len(dti.day) == 365
            assert len(dti.hour) == 365
            assert len(dti.minute) == 365
            assert len(dti.second) == 365
            assert len(dti.microsecond) == 365
            assert len(dti.dayofweek) == 365
            assert len(dti.dayofyear) == 365
            assert len(dti.weekofyear) == 365
            assert len(dti.quarter) == 365
            assert len(dti.is_month_start) == 365
            assert len(dti.is_month_end) == 365
            assert len(dti.is_quarter_start) == 365
            assert len(dti.is_quarter_end) == 365
            assert len(dti.is_year_start) == 365
            assert len(dti.is_year_end) == 365
            assert len(dti.weekday_name) == 365

            dti.name = 'name'

            # non boolean accessors -> return Index
            for accessor in DatetimeIndex._field_ops:
                res = getattr(dti, accessor)
                assert len(res) == 365
                assert isinstance(res, Index)
                assert res.name == 'name'

            # boolean accessors -> return array
            for accessor in DatetimeIndex._bool_ops:
                res = getattr(dti, accessor)
                assert len(res) == 365
                assert isinstance(res, np.ndarray)

            # test boolean indexing
            res = dti[dti.is_quarter_start]
            exp = dti[[0, 90, 181, 273]]
            tm.assert_index_equal(res, exp)
            res = dti[dti.is_leap_year]
            exp = DatetimeIndex([], freq='D', tz=dti.tz, name='name')
            tm.assert_index_equal(res, exp)

        dti = DatetimeIndex(freq='BQ-FEB', start=datetime(1998, 1, 1),
                            periods=4)

        assert sum(dti.is_quarter_start) == 0
        assert sum(dti.is_quarter_end) == 4
        assert sum(dti.is_year_start) == 0
        assert sum(dti.is_year_end) == 1

        # Ensure is_start/end accessors throw ValueError for CustomBusinessDay,
        # CBD requires np >= 1.7
        bday_egypt = offsets.CustomBusinessDay(weekmask='Sun Mon Tue Wed Thu')
        dti = date_range(datetime(2013, 4, 30), periods=5, freq=bday_egypt)
        pytest.raises(ValueError, lambda: dti.is_month_start)

        dti = DatetimeIndex(['2000-01-01', '2000-01-02', '2000-01-03'])

        assert dti.is_month_start[0] == 1

        tests = [
            (Timestamp('2013-06-01', freq='M').is_month_start, 1),
            (Timestamp('2013-06-01', freq='BM').is_month_start, 0),
            (Timestamp('2013-06-03', freq='M').is_month_start, 0),
            (Timestamp('2013-06-03', freq='BM').is_month_start, 1),
            (Timestamp('2013-02-28', freq='Q-FEB').is_month_end, 1),
            (Timestamp('2013-02-28', freq='Q-FEB').is_quarter_end, 1),
            (Timestamp('2013-02-28', freq='Q-FEB').is_year_end, 1),
            (Timestamp('2013-03-01', freq='Q-FEB').is_month_start, 1),
            (Timestamp('2013-03-01', freq='Q-FEB').is_quarter_start, 1),
            (Timestamp('2013-03-01', freq='Q-FEB').is_year_start, 1),
            (Timestamp('2013-03-31', freq='QS-FEB').is_month_end, 1),
            (Timestamp('2013-03-31', freq='QS-FEB').is_quarter_end, 0),
            (Timestamp('2013-03-31', freq='QS-FEB').is_year_end, 0),
            (Timestamp('2013-02-01', freq='QS-FEB').is_month_start, 1),
            (Timestamp('2013-02-01', freq='QS-FEB').is_quarter_start, 1),
            (Timestamp('2013-02-01', freq='QS-FEB').is_year_start, 1),
            (Timestamp('2013-06-30', freq='BQ').is_month_end, 0),
            (Timestamp('2013-06-30', freq='BQ').is_quarter_end, 0),
            (Timestamp('2013-06-30', freq='BQ').is_year_end, 0),
            (Timestamp('2013-06-28', freq='BQ').is_month_end, 1),
            (Timestamp('2013-06-28', freq='BQ').is_quarter_end, 1),
            (Timestamp('2013-06-28', freq='BQ').is_year_end, 0),
            (Timestamp('2013-06-30', freq='BQS-APR').is_month_end, 0),
            (Timestamp('2013-06-30', freq='BQS-APR').is_quarter_end, 0),
            (Timestamp('2013-06-30', freq='BQS-APR').is_year_end, 0),
            (Timestamp('2013-06-28', freq='BQS-APR').is_month_end, 1),
            (Timestamp('2013-06-28', freq='BQS-APR').is_quarter_end, 1),
            (Timestamp('2013-03-29', freq='BQS-APR').is_year_end, 1),
            (Timestamp('2013-11-01', freq='AS-NOV').is_year_start, 1),
            (Timestamp('2013-10-31', freq='AS-NOV').is_year_end, 1),
            (Timestamp('2012-02-01').days_in_month, 29),
            (Timestamp('2013-02-01').days_in_month, 28)]

        for ts, value in tests:
            assert ts == value

        # GH 6538: Check that DatetimeIndex and its TimeStamp elements
        # return the same weekofyear accessor close to new year w/ tz
        dates = ["2013/12/29", "2013/12/30", "2013/12/31"]
        dates = DatetimeIndex(dates, tz="Europe/Brussels")
        expected = [52, 1, 1]
        assert dates.weekofyear.tolist() == expected
        assert [d.weekofyear for d in dates] == expected
Пример #4
0
try:
    import pandas.tseries.holiday  # noqa
except ImportError:
    pass

hcal = pandas.tseries.holiday.USFederalHolidayCalendar()
# These offsets currently raise a NotImplimentedError with .apply_index()
non_apply = [
    offsets.Day(),
    offsets.BYearEnd(),
    offsets.BYearBegin(),
    offsets.BQuarterEnd(),
    offsets.BQuarterBegin(),
    offsets.BMonthEnd(),
    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(),
Пример #5
0
    def test_datetimeindex_accessors(self):
        dti = DatetimeIndex(freq='D', start=datetime(1998, 1, 1), periods=365)

        self.assertEqual(dti.year[0], 1998)
        self.assertEqual(dti.month[0], 1)
        self.assertEqual(dti.day[0], 1)
        self.assertEqual(dti.hour[0], 0)
        self.assertEqual(dti.minute[0], 0)
        self.assertEqual(dti.second[0], 0)
        self.assertEqual(dti.microsecond[0], 0)
        self.assertEqual(dti.dayofweek[0], 3)

        self.assertEqual(dti.dayofyear[0], 1)
        self.assertEqual(dti.dayofyear[120], 121)

        self.assertEqual(dti.weekofyear[0], 1)
        self.assertEqual(dti.weekofyear[120], 18)

        self.assertEqual(dti.quarter[0], 1)
        self.assertEqual(dti.quarter[120], 2)

        self.assertEqual(dti.days_in_month[0], 31)
        self.assertEqual(dti.days_in_month[90], 30)

        self.assertEqual(dti.is_month_start[0], True)
        self.assertEqual(dti.is_month_start[1], False)
        self.assertEqual(dti.is_month_start[31], True)
        self.assertEqual(dti.is_quarter_start[0], True)
        self.assertEqual(dti.is_quarter_start[90], True)
        self.assertEqual(dti.is_year_start[0], True)
        self.assertEqual(dti.is_year_start[364], False)
        self.assertEqual(dti.is_month_end[0], False)
        self.assertEqual(dti.is_month_end[30], True)
        self.assertEqual(dti.is_month_end[31], False)
        self.assertEqual(dti.is_month_end[364], True)
        self.assertEqual(dti.is_quarter_end[0], False)
        self.assertEqual(dti.is_quarter_end[30], False)
        self.assertEqual(dti.is_quarter_end[89], True)
        self.assertEqual(dti.is_quarter_end[364], True)
        self.assertEqual(dti.is_year_end[0], False)
        self.assertEqual(dti.is_year_end[364], True)

        # GH 11128
        self.assertEqual(dti.weekday_name[4], u'Monday')
        self.assertEqual(dti.weekday_name[5], u'Tuesday')
        self.assertEqual(dti.weekday_name[6], u'Wednesday')
        self.assertEqual(dti.weekday_name[7], u'Thursday')
        self.assertEqual(dti.weekday_name[8], u'Friday')
        self.assertEqual(dti.weekday_name[9], u'Saturday')
        self.assertEqual(dti.weekday_name[10], u'Sunday')

        self.assertEqual(Timestamp('2016-04-04').weekday_name, u'Monday')
        self.assertEqual(Timestamp('2016-04-05').weekday_name, u'Tuesday')
        self.assertEqual(Timestamp('2016-04-06').weekday_name, u'Wednesday')
        self.assertEqual(Timestamp('2016-04-07').weekday_name, u'Thursday')
        self.assertEqual(Timestamp('2016-04-08').weekday_name, u'Friday')
        self.assertEqual(Timestamp('2016-04-09').weekday_name, u'Saturday')
        self.assertEqual(Timestamp('2016-04-10').weekday_name, u'Sunday')

        self.assertEqual(len(dti.year), 365)
        self.assertEqual(len(dti.month), 365)
        self.assertEqual(len(dti.day), 365)
        self.assertEqual(len(dti.hour), 365)
        self.assertEqual(len(dti.minute), 365)
        self.assertEqual(len(dti.second), 365)
        self.assertEqual(len(dti.microsecond), 365)
        self.assertEqual(len(dti.dayofweek), 365)
        self.assertEqual(len(dti.dayofyear), 365)
        self.assertEqual(len(dti.weekofyear), 365)
        self.assertEqual(len(dti.quarter), 365)
        self.assertEqual(len(dti.is_month_start), 365)
        self.assertEqual(len(dti.is_month_end), 365)
        self.assertEqual(len(dti.is_quarter_start), 365)
        self.assertEqual(len(dti.is_quarter_end), 365)
        self.assertEqual(len(dti.is_year_start), 365)
        self.assertEqual(len(dti.is_year_end), 365)
        self.assertEqual(len(dti.weekday_name), 365)

        dti = DatetimeIndex(freq='BQ-FEB',
                            start=datetime(1998, 1, 1),
                            periods=4)

        self.assertEqual(sum(dti.is_quarter_start), 0)
        self.assertEqual(sum(dti.is_quarter_end), 4)
        self.assertEqual(sum(dti.is_year_start), 0)
        self.assertEqual(sum(dti.is_year_end), 1)

        # Ensure is_start/end accessors throw ValueError for CustomBusinessDay,
        # CBD requires np >= 1.7
        bday_egypt = offsets.CustomBusinessDay(weekmask='Sun Mon Tue Wed Thu')
        dti = date_range(datetime(2013, 4, 30), periods=5, freq=bday_egypt)
        self.assertRaises(ValueError, lambda: dti.is_month_start)

        dti = DatetimeIndex(['2000-01-01', '2000-01-02', '2000-01-03'])

        self.assertEqual(dti.is_month_start[0], 1)

        tests = [(Timestamp('2013-06-01', freq='M').is_month_start, 1),
                 (Timestamp('2013-06-01', freq='BM').is_month_start, 0),
                 (Timestamp('2013-06-03', freq='M').is_month_start, 0),
                 (Timestamp('2013-06-03', freq='BM').is_month_start, 1),
                 (Timestamp('2013-02-28', freq='Q-FEB').is_month_end, 1),
                 (Timestamp('2013-02-28', freq='Q-FEB').is_quarter_end, 1),
                 (Timestamp('2013-02-28', freq='Q-FEB').is_year_end, 1),
                 (Timestamp('2013-03-01', freq='Q-FEB').is_month_start, 1),
                 (Timestamp('2013-03-01', freq='Q-FEB').is_quarter_start, 1),
                 (Timestamp('2013-03-01', freq='Q-FEB').is_year_start, 1),
                 (Timestamp('2013-03-31', freq='QS-FEB').is_month_end, 1),
                 (Timestamp('2013-03-31', freq='QS-FEB').is_quarter_end, 0),
                 (Timestamp('2013-03-31', freq='QS-FEB').is_year_end, 0),
                 (Timestamp('2013-02-01', freq='QS-FEB').is_month_start, 1),
                 (Timestamp('2013-02-01', freq='QS-FEB').is_quarter_start, 1),
                 (Timestamp('2013-02-01', freq='QS-FEB').is_year_start, 1),
                 (Timestamp('2013-06-30', freq='BQ').is_month_end, 0),
                 (Timestamp('2013-06-30', freq='BQ').is_quarter_end, 0),
                 (Timestamp('2013-06-30', freq='BQ').is_year_end, 0),
                 (Timestamp('2013-06-28', freq='BQ').is_month_end, 1),
                 (Timestamp('2013-06-28', freq='BQ').is_quarter_end, 1),
                 (Timestamp('2013-06-28', freq='BQ').is_year_end, 0),
                 (Timestamp('2013-06-30', freq='BQS-APR').is_month_end, 0),
                 (Timestamp('2013-06-30', freq='BQS-APR').is_quarter_end, 0),
                 (Timestamp('2013-06-30', freq='BQS-APR').is_year_end, 0),
                 (Timestamp('2013-06-28', freq='BQS-APR').is_month_end, 1),
                 (Timestamp('2013-06-28', freq='BQS-APR').is_quarter_end, 1),
                 (Timestamp('2013-03-29', freq='BQS-APR').is_year_end, 1),
                 (Timestamp('2013-11-01', freq='AS-NOV').is_year_start, 1),
                 (Timestamp('2013-10-31', freq='AS-NOV').is_year_end, 1),
                 (Timestamp('2012-02-01').days_in_month, 29),
                 (Timestamp('2013-02-01').days_in_month, 28)]

        for ts, value in tests:
            self.assertEqual(ts, value)
Пример #6
0
    def test_datetimeindex_accessors(self):

        dti_naive = DatetimeIndex(freq='D',
                                  start=datetime(1998, 1, 1),
                                  periods=365)
        # GH 13303
        dti_tz = DatetimeIndex(freq='D',
                               start=datetime(1998, 1, 1),
                               periods=365,
                               tz='US/Eastern')
        for dti in [dti_naive, dti_tz]:

            self.assertEqual(dti.year[0], 1998)
            self.assertEqual(dti.month[0], 1)
            self.assertEqual(dti.day[0], 1)
            self.assertEqual(dti.hour[0], 0)
            self.assertEqual(dti.minute[0], 0)
            self.assertEqual(dti.second[0], 0)
            self.assertEqual(dti.microsecond[0], 0)
            self.assertEqual(dti.dayofweek[0], 3)

            self.assertEqual(dti.dayofyear[0], 1)
            self.assertEqual(dti.dayofyear[120], 121)

            self.assertEqual(dti.weekofyear[0], 1)
            self.assertEqual(dti.weekofyear[120], 18)

            self.assertEqual(dti.quarter[0], 1)
            self.assertEqual(dti.quarter[120], 2)

            self.assertEqual(dti.days_in_month[0], 31)
            self.assertEqual(dti.days_in_month[90], 30)

            self.assertEqual(dti.is_month_start[0], True)
            self.assertEqual(dti.is_month_start[1], False)
            self.assertEqual(dti.is_month_start[31], True)
            self.assertEqual(dti.is_quarter_start[0], True)
            self.assertEqual(dti.is_quarter_start[90], True)
            self.assertEqual(dti.is_year_start[0], True)
            self.assertEqual(dti.is_year_start[364], False)
            self.assertEqual(dti.is_month_end[0], False)
            self.assertEqual(dti.is_month_end[30], True)
            self.assertEqual(dti.is_month_end[31], False)
            self.assertEqual(dti.is_month_end[364], True)
            self.assertEqual(dti.is_quarter_end[0], False)
            self.assertEqual(dti.is_quarter_end[30], False)
            self.assertEqual(dti.is_quarter_end[89], True)
            self.assertEqual(dti.is_quarter_end[364], True)
            self.assertEqual(dti.is_year_end[0], False)
            self.assertEqual(dti.is_year_end[364], True)

            # GH 11128
            self.assertEqual(dti.weekday_name[4], u'Monday')
            self.assertEqual(dti.weekday_name[5], u'Tuesday')
            self.assertEqual(dti.weekday_name[6], u'Wednesday')
            self.assertEqual(dti.weekday_name[7], u'Thursday')
            self.assertEqual(dti.weekday_name[8], u'Friday')
            self.assertEqual(dti.weekday_name[9], u'Saturday')
            self.assertEqual(dti.weekday_name[10], u'Sunday')

            self.assertEqual(Timestamp('2016-04-04').weekday_name, u'Monday')
            self.assertEqual(Timestamp('2016-04-05').weekday_name, u'Tuesday')
            self.assertEqual(
                Timestamp('2016-04-06').weekday_name, u'Wednesday')
            self.assertEqual(Timestamp('2016-04-07').weekday_name, u'Thursday')
            self.assertEqual(Timestamp('2016-04-08').weekday_name, u'Friday')
            self.assertEqual(Timestamp('2016-04-09').weekday_name, u'Saturday')
            self.assertEqual(Timestamp('2016-04-10').weekday_name, u'Sunday')

            self.assertEqual(len(dti.year), 365)
            self.assertEqual(len(dti.month), 365)
            self.assertEqual(len(dti.day), 365)
            self.assertEqual(len(dti.hour), 365)
            self.assertEqual(len(dti.minute), 365)
            self.assertEqual(len(dti.second), 365)
            self.assertEqual(len(dti.microsecond), 365)
            self.assertEqual(len(dti.dayofweek), 365)
            self.assertEqual(len(dti.dayofyear), 365)
            self.assertEqual(len(dti.weekofyear), 365)
            self.assertEqual(len(dti.quarter), 365)
            self.assertEqual(len(dti.is_month_start), 365)
            self.assertEqual(len(dti.is_month_end), 365)
            self.assertEqual(len(dti.is_quarter_start), 365)
            self.assertEqual(len(dti.is_quarter_end), 365)
            self.assertEqual(len(dti.is_year_start), 365)
            self.assertEqual(len(dti.is_year_end), 365)
            self.assertEqual(len(dti.weekday_name), 365)

            dti.name = 'name'

            # non boolean accessors -> return Index
            for accessor in DatetimeIndex._field_ops:
                res = getattr(dti, accessor)
                assert len(res) == 365
                assert isinstance(res, Index)
                assert res.name == 'name'

            # boolean accessors -> return array
            for accessor in DatetimeIndex._bool_ops:
                res = getattr(dti, accessor)
                assert len(res) == 365
                assert isinstance(res, np.ndarray)

            # test boolean indexing
            res = dti[dti.is_quarter_start]
            exp = dti[[0, 90, 181, 273]]
            tm.assert_index_equal(res, exp)
            res = dti[dti.is_leap_year]
            exp = DatetimeIndex([], freq='D', tz=dti.tz, name='name')
            tm.assert_index_equal(res, exp)

        dti = DatetimeIndex(freq='BQ-FEB',
                            start=datetime(1998, 1, 1),
                            periods=4)

        self.assertEqual(sum(dti.is_quarter_start), 0)
        self.assertEqual(sum(dti.is_quarter_end), 4)
        self.assertEqual(sum(dti.is_year_start), 0)
        self.assertEqual(sum(dti.is_year_end), 1)

        # Ensure is_start/end accessors throw ValueError for CustomBusinessDay,
        # CBD requires np >= 1.7
        bday_egypt = offsets.CustomBusinessDay(weekmask='Sun Mon Tue Wed Thu')
        dti = date_range(datetime(2013, 4, 30), periods=5, freq=bday_egypt)
        pytest.raises(ValueError, lambda: dti.is_month_start)

        dti = DatetimeIndex(['2000-01-01', '2000-01-02', '2000-01-03'])

        self.assertEqual(dti.is_month_start[0], 1)

        tests = [(Timestamp('2013-06-01', freq='M').is_month_start, 1),
                 (Timestamp('2013-06-01', freq='BM').is_month_start, 0),
                 (Timestamp('2013-06-03', freq='M').is_month_start, 0),
                 (Timestamp('2013-06-03', freq='BM').is_month_start, 1),
                 (Timestamp('2013-02-28', freq='Q-FEB').is_month_end, 1),
                 (Timestamp('2013-02-28', freq='Q-FEB').is_quarter_end, 1),
                 (Timestamp('2013-02-28', freq='Q-FEB').is_year_end, 1),
                 (Timestamp('2013-03-01', freq='Q-FEB').is_month_start, 1),
                 (Timestamp('2013-03-01', freq='Q-FEB').is_quarter_start, 1),
                 (Timestamp('2013-03-01', freq='Q-FEB').is_year_start, 1),
                 (Timestamp('2013-03-31', freq='QS-FEB').is_month_end, 1),
                 (Timestamp('2013-03-31', freq='QS-FEB').is_quarter_end, 0),
                 (Timestamp('2013-03-31', freq='QS-FEB').is_year_end, 0),
                 (Timestamp('2013-02-01', freq='QS-FEB').is_month_start, 1),
                 (Timestamp('2013-02-01', freq='QS-FEB').is_quarter_start, 1),
                 (Timestamp('2013-02-01', freq='QS-FEB').is_year_start, 1),
                 (Timestamp('2013-06-30', freq='BQ').is_month_end, 0),
                 (Timestamp('2013-06-30', freq='BQ').is_quarter_end, 0),
                 (Timestamp('2013-06-30', freq='BQ').is_year_end, 0),
                 (Timestamp('2013-06-28', freq='BQ').is_month_end, 1),
                 (Timestamp('2013-06-28', freq='BQ').is_quarter_end, 1),
                 (Timestamp('2013-06-28', freq='BQ').is_year_end, 0),
                 (Timestamp('2013-06-30', freq='BQS-APR').is_month_end, 0),
                 (Timestamp('2013-06-30', freq='BQS-APR').is_quarter_end, 0),
                 (Timestamp('2013-06-30', freq='BQS-APR').is_year_end, 0),
                 (Timestamp('2013-06-28', freq='BQS-APR').is_month_end, 1),
                 (Timestamp('2013-06-28', freq='BQS-APR').is_quarter_end, 1),
                 (Timestamp('2013-03-29', freq='BQS-APR').is_year_end, 1),
                 (Timestamp('2013-11-01', freq='AS-NOV').is_year_start, 1),
                 (Timestamp('2013-10-31', freq='AS-NOV').is_year_end, 1),
                 (Timestamp('2012-02-01').days_in_month, 29),
                 (Timestamp('2013-02-01').days_in_month, 28)]

        for ts, value in tests:
            self.assertEqual(ts, value)
    def test_datetimeindex_accessors(self):
        dti_naive = pd.date_range(freq="D",
                                  start=datetime(1998, 1, 1),
                                  periods=365)
        # GH#13303
        dti_tz = pd.date_range(freq="D",
                               start=datetime(1998, 1, 1),
                               periods=365,
                               tz="US/Eastern")
        for dti in [dti_naive, dti_tz]:

            assert dti.year[0] == 1998
            assert dti.month[0] == 1
            assert dti.day[0] == 1
            assert dti.hour[0] == 0
            assert dti.minute[0] == 0
            assert dti.second[0] == 0
            assert dti.microsecond[0] == 0
            assert dti.dayofweek[0] == 3

            assert dti.dayofyear[0] == 1
            assert dti.dayofyear[120] == 121

            assert dti.isocalendar().week[0] == 1
            assert dti.isocalendar().week[120] == 18

            assert dti.quarter[0] == 1
            assert dti.quarter[120] == 2

            assert dti.days_in_month[0] == 31
            assert dti.days_in_month[90] == 30

            assert dti.is_month_start[0]
            assert not dti.is_month_start[1]
            assert dti.is_month_start[31]
            assert dti.is_quarter_start[0]
            assert dti.is_quarter_start[90]
            assert dti.is_year_start[0]
            assert not dti.is_year_start[364]
            assert not dti.is_month_end[0]
            assert dti.is_month_end[30]
            assert not dti.is_month_end[31]
            assert dti.is_month_end[364]
            assert not dti.is_quarter_end[0]
            assert not dti.is_quarter_end[30]
            assert dti.is_quarter_end[89]
            assert dti.is_quarter_end[364]
            assert not dti.is_year_end[0]
            assert dti.is_year_end[364]

            assert len(dti.year) == 365
            assert len(dti.month) == 365
            assert len(dti.day) == 365
            assert len(dti.hour) == 365
            assert len(dti.minute) == 365
            assert len(dti.second) == 365
            assert len(dti.microsecond) == 365
            assert len(dti.dayofweek) == 365
            assert len(dti.dayofyear) == 365
            assert len(dti.isocalendar()) == 365
            assert len(dti.quarter) == 365
            assert len(dti.is_month_start) == 365
            assert len(dti.is_month_end) == 365
            assert len(dti.is_quarter_start) == 365
            assert len(dti.is_quarter_end) == 365
            assert len(dti.is_year_start) == 365
            assert len(dti.is_year_end) == 365

            dti.name = "name"

            # non boolean accessors -> return Index
            for accessor in DatetimeIndex._field_ops:
                if accessor in ["week", "weekofyear"]:
                    # GH#33595 Deprecate week and weekofyear
                    continue
                res = getattr(dti, accessor)
                assert len(res) == 365
                assert isinstance(res, Index)
                assert res.name == "name"

            # boolean accessors -> return array
            for accessor in DatetimeIndex._bool_ops:
                res = getattr(dti, accessor)
                assert len(res) == 365
                assert isinstance(res, np.ndarray)

            # test boolean indexing
            res = dti[dti.is_quarter_start]
            exp = dti[[0, 90, 181, 273]]
            tm.assert_index_equal(res, exp)
            res = dti[dti.is_leap_year]
            exp = DatetimeIndex([], freq="D", tz=dti.tz, name="name")
            tm.assert_index_equal(res, exp)

        dti = pd.date_range(freq="BQ-FEB",
                            start=datetime(1998, 1, 1),
                            periods=4)

        assert sum(dti.is_quarter_start) == 0
        assert sum(dti.is_quarter_end) == 4
        assert sum(dti.is_year_start) == 0
        assert sum(dti.is_year_end) == 1

        # Ensure is_start/end accessors throw ValueError for CustomBusinessDay,
        bday_egypt = offsets.CustomBusinessDay(weekmask="Sun Mon Tue Wed Thu")
        dti = date_range(datetime(2013, 4, 30), periods=5, freq=bday_egypt)
        msg = "Custom business days is not supported by is_month_start"
        with pytest.raises(ValueError, match=msg):
            dti.is_month_start

        dti = DatetimeIndex(["2000-01-01", "2000-01-02", "2000-01-03"])

        assert dti.is_month_start[0] == 1

        tests = [
            (Timestamp("2013-06-01", freq="M").is_month_start, 1),
            (Timestamp("2013-06-01", freq="BM").is_month_start, 0),
            (Timestamp("2013-06-03", freq="M").is_month_start, 0),
            (Timestamp("2013-06-03", freq="BM").is_month_start, 1),
            (Timestamp("2013-02-28", freq="Q-FEB").is_month_end, 1),
            (Timestamp("2013-02-28", freq="Q-FEB").is_quarter_end, 1),
            (Timestamp("2013-02-28", freq="Q-FEB").is_year_end, 1),
            (Timestamp("2013-03-01", freq="Q-FEB").is_month_start, 1),
            (Timestamp("2013-03-01", freq="Q-FEB").is_quarter_start, 1),
            (Timestamp("2013-03-01", freq="Q-FEB").is_year_start, 1),
            (Timestamp("2013-03-31", freq="QS-FEB").is_month_end, 1),
            (Timestamp("2013-03-31", freq="QS-FEB").is_quarter_end, 0),
            (Timestamp("2013-03-31", freq="QS-FEB").is_year_end, 0),
            (Timestamp("2013-02-01", freq="QS-FEB").is_month_start, 1),
            (Timestamp("2013-02-01", freq="QS-FEB").is_quarter_start, 1),
            (Timestamp("2013-02-01", freq="QS-FEB").is_year_start, 1),
            (Timestamp("2013-06-30", freq="BQ").is_month_end, 0),
            (Timestamp("2013-06-30", freq="BQ").is_quarter_end, 0),
            (Timestamp("2013-06-30", freq="BQ").is_year_end, 0),
            (Timestamp("2013-06-28", freq="BQ").is_month_end, 1),
            (Timestamp("2013-06-28", freq="BQ").is_quarter_end, 1),
            (Timestamp("2013-06-28", freq="BQ").is_year_end, 0),
            (Timestamp("2013-06-30", freq="BQS-APR").is_month_end, 0),
            (Timestamp("2013-06-30", freq="BQS-APR").is_quarter_end, 0),
            (Timestamp("2013-06-30", freq="BQS-APR").is_year_end, 0),
            (Timestamp("2013-06-28", freq="BQS-APR").is_month_end, 1),
            (Timestamp("2013-06-28", freq="BQS-APR").is_quarter_end, 1),
            (Timestamp("2013-03-29", freq="BQS-APR").is_year_end, 1),
            (Timestamp("2013-11-01", freq="AS-NOV").is_year_start, 1),
            (Timestamp("2013-10-31", freq="AS-NOV").is_year_end, 1),
            (Timestamp("2012-02-01").days_in_month, 29),
            (Timestamp("2013-02-01").days_in_month, 28),
        ]

        for ts, value in tests:
            assert ts == value

        # GH 6538: Check that DatetimeIndex and its TimeStamp elements
        # return the same weekofyear accessor close to new year w/ tz
        dates = ["2013/12/29", "2013/12/30", "2013/12/31"]
        dates = DatetimeIndex(dates, tz="Europe/Brussels")
        expected = [52, 1, 1]
        assert dates.isocalendar().week.tolist() == expected
        assert [d.weekofyear for d in dates] == expected
Пример #8
0
def Calculate(stdt, enddt, LD, dfRI, dfRO, hol_list, rollDates, rlamt, typ):
    #    x = time.time()

    print(len(dfRO), len(dfRI), len(hol_list), len(rollDates))

    intlvl = 100
    run_date = stdt
    dts, idx, idxprd, uro, uri = [], [], [-2], [], []
    ro, ri, pro, pri, ltd, rlflg = [], [], [], [], [], []
    rlcnt, cnt, flg = 0, -2, 0
    reduce1 = lambda x: -2 if x < -1 else x - 1
    reduce2 = lambda x: 0 if x < 1 else x - 1
    srl = 0
    print
    while run_date <= enddt:
        try:

            if run_date > stdt and rol_date == (
                    run_date +
                    pof.CustomBusinessDay(LD, holidays=hol_list)).date():
                cnt = LD
                flg = LD
                rlcnt += 1

            if (run_date not in hol_list) and (run_date >= stdt):
                try:
                    dts.append(run_date)
                    ro.append(dfRO.loc[run_date].RO)
                    ri.append(dfRI.loc[run_date].RI)
                    pro.append(dfRO.loc[run_date].PRO)
                    pri.append(dfRI.loc[run_date].PRI)
                    rlflg.append(flg)
                except:
                    print("Data issue on", str(run_date))
                    print(dts, ro, ri, pro, pri)
                    run_date += pof.CustomBusinessDay(1, holidays=hol_list)
                    run_date = run_date.date()
                    continue
                if typ == 0:
                    rol_date = rollDates.loc[ro[-1]]
                else:
                    while run_date > rollDates[srl].date():
                        srl += 1
                    rol_date = rollDates[srl].date()
                    ltd_prvDay = rol_date

                if run_date == stdt:
                    idx.append(intlvl)
                    ltd_prvDay = rol_date
                    ltd.append(ltd_prvDay)
                    if (cnt > 0):
                        uro.append(0)
                        uri.append(idx[-1] / dfRI.loc[run_date].PRI)
                    else:
                        uro.append(idx[-1] / dfRO.loc[run_date].PRO)
                        uri.append(0)
                    rlcnt = 0

                else:
                    if dts[-2] == ltd_prvDay:
                        idx.append(
                            idx[-1] + uro[-1] *
                            (dfRO.loc[run_date].PRO - dfRI.loc[dts[-2]].PRI) +
                            uri[-1] *
                            (dfRI.loc[run_date].PRI - dfRI.loc[dts[-2]].PRI))
                        ltd_prvDay = rol_date
                        ltd.append(ltd_prvDay)

                    else:
                        idx.append(
                            idx[-1] + uro[-1] *
                            (dfRO.loc[run_date].PRO - dfRO.loc[dts[-2]].PRO) +
                            uri[-1] *
                            (dfRI.loc[run_date].PRI - dfRI.loc[dts[-2]].PRI))
                        ltd.append(ltd[-1])
                    u1, u2 = units(rlamt, uri[-1], uro[-1], cnt, idx[-1],
                                   dfRI.loc[run_date].PRI,
                                   dfRO.loc[run_date].PRO, LD)
                    uri.append(u1)
                    uro.append(u2)
            idxprd.append(cnt)
            cnt = reduce1(cnt)
            flg = reduce2(flg)
            run_date += pof.CustomBusinessDay(1, holidays=hol_list)
            run_date = run_date.date()
#    y = time.time()
#        except:
#            print("Error on ", str(run_date))
#            run_date += pof.CustomBusinessDay(1,holidays = hol_list)
#            run_date = run_date.date(
        except:
            print(len(dts), "\t", len(ro), "\t", len(idx), "\t", len(uri),
                  "\t", len(uro))
            break

    fdf = pd.DataFrame()
    fdf = pd.DataFrame(index=dts)
    fdf["Contract_RO"] = ro
    fdf["Contract_RI"] = ri
    fdf["Price_RO"] = pro
    fdf["Price_RI"] = pri
    fdf["Roll_Flag"] = rlflg
    fdf["Units_RO"] = uro
    fdf["Units_RI"] = uri
    fdf["ICL"] = idx

    return fdf