Python pandas.offsets.Day() Examples

The following are 21 code examples of pandas.offsets.Day(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module pandas.offsets , or try the search function .
Example #1
Source File: test_period.py    From twitter-stock-recommendation with MIT License 6 votes vote down vote up
def test_period_ops_offset(self):
        p = Period('2011-04-01', freq='D')
        result = p + offsets.Day()
        exp = pd.Period('2011-04-02', freq='D')
        assert result == exp

        result = p - offsets.Day(2)
        exp = pd.Period('2011-03-30', freq='D')
        assert result == exp

        msg = r"Input cannot be converted to Period\(freq=D\)"
        with tm.assert_raises_regex(period.IncompatibleFrequency, msg):
            p + offsets.Hour(2)

        with tm.assert_raises_regex(period.IncompatibleFrequency, msg):
            p - offsets.Hour(2) 
Example #2
Source File: test_period.py    From recruit with Apache License 2.0 6 votes vote down vote up
def test_period_ops_offset(self):
        p = Period('2011-04-01', freq='D')
        result = p + offsets.Day()
        exp = Period('2011-04-02', freq='D')
        assert result == exp

        result = p - offsets.Day(2)
        exp = Period('2011-03-30', freq='D')
        assert result == exp

        msg = r"Input cannot be converted to Period\(freq=D\)"
        with pytest.raises(period.IncompatibleFrequency, match=msg):
            p + offsets.Hour(2)

        with pytest.raises(period.IncompatibleFrequency, match=msg):
            p - offsets.Hour(2) 
Example #3
Source File: test_period.py    From vnpy_crypto with MIT License 6 votes vote down vote up
def test_period_deprecated_freq(self):
        cases = {"M": ["MTH", "MONTH", "MONTHLY", "Mth", "month", "monthly"],
                 "B": ["BUS", "BUSINESS", "BUSINESSLY", "WEEKDAY", "bus"],
                 "D": ["DAY", "DLY", "DAILY", "Day", "Dly", "Daily"],
                 "H": ["HR", "HOUR", "HRLY", "HOURLY", "hr", "Hour", "HRly"],
                 "T": ["minute", "MINUTE", "MINUTELY", "minutely"],
                 "S": ["sec", "SEC", "SECOND", "SECONDLY", "second"],
                 "L": ["MILLISECOND", "MILLISECONDLY", "millisecond"],
                 "U": ["MICROSECOND", "MICROSECONDLY", "microsecond"],
                 "N": ["NANOSECOND", "NANOSECONDLY", "nanosecond"]}

        msg = pd._libs.tslibs.frequencies._INVALID_FREQ_ERROR
        for exp, freqs in iteritems(cases):
            for freq in freqs:
                with tm.assert_raises_regex(ValueError, msg):
                    Period('2016-03-01 09:00', freq=freq)
                with tm.assert_raises_regex(ValueError, msg):
                    Period(ordinal=1, freq=freq)

            # check supported freq-aliases still works
            p1 = Period('2016-03-01 09:00', freq=exp)
            p2 = Period(ordinal=1, freq=exp)
            assert isinstance(p1, Period)
            assert isinstance(p2, Period) 
Example #4
Source File: test_period.py    From twitter-stock-recommendation with MIT License 6 votes vote down vote up
def test_period_deprecated_freq(self):
        cases = {"M": ["MTH", "MONTH", "MONTHLY", "Mth", "month", "monthly"],
                 "B": ["BUS", "BUSINESS", "BUSINESSLY", "WEEKDAY", "bus"],
                 "D": ["DAY", "DLY", "DAILY", "Day", "Dly", "Daily"],
                 "H": ["HR", "HOUR", "HRLY", "HOURLY", "hr", "Hour", "HRly"],
                 "T": ["minute", "MINUTE", "MINUTELY", "minutely"],
                 "S": ["sec", "SEC", "SECOND", "SECONDLY", "second"],
                 "L": ["MILLISECOND", "MILLISECONDLY", "millisecond"],
                 "U": ["MICROSECOND", "MICROSECONDLY", "microsecond"],
                 "N": ["NANOSECOND", "NANOSECONDLY", "nanosecond"]}

        msg = pd._libs.tslibs.frequencies._INVALID_FREQ_ERROR
        for exp, freqs in iteritems(cases):
            for freq in freqs:
                with tm.assert_raises_regex(ValueError, msg):
                    Period('2016-03-01 09:00', freq=freq)
                with tm.assert_raises_regex(ValueError, msg):
                    Period(ordinal=1, freq=freq)

            # check supported freq-aliases still works
            p1 = Period('2016-03-01 09:00', freq=exp)
            p2 = Period(ordinal=1, freq=exp)
            assert isinstance(p1, Period)
            assert isinstance(p2, Period) 
Example #5
Source File: test_period.py    From vnpy_crypto with MIT License 6 votes vote down vote up
def test_period_ops_offset(self):
        p = Period('2011-04-01', freq='D')
        result = p + offsets.Day()
        exp = pd.Period('2011-04-02', freq='D')
        assert result == exp

        result = p - offsets.Day(2)
        exp = pd.Period('2011-03-30', freq='D')
        assert result == exp

        msg = r"Input cannot be converted to Period\(freq=D\)"
        with tm.assert_raises_regex(period.IncompatibleFrequency, msg):
            p + offsets.Hour(2)

        with tm.assert_raises_regex(period.IncompatibleFrequency, msg):
            p - offsets.Hour(2) 
Example #6
Source File: test_period.py    From predictive-maintenance-using-machine-learning with Apache License 2.0 6 votes vote down vote up
def test_period_deprecated_freq(self):
        cases = {"M": ["MTH", "MONTH", "MONTHLY", "Mth", "month", "monthly"],
                 "B": ["BUS", "BUSINESS", "BUSINESSLY", "WEEKDAY", "bus"],
                 "D": ["DAY", "DLY", "DAILY", "Day", "Dly", "Daily"],
                 "H": ["HR", "HOUR", "HRLY", "HOURLY", "hr", "Hour", "HRly"],
                 "T": ["minute", "MINUTE", "MINUTELY", "minutely"],
                 "S": ["sec", "SEC", "SECOND", "SECONDLY", "second"],
                 "L": ["MILLISECOND", "MILLISECONDLY", "millisecond"],
                 "U": ["MICROSECOND", "MICROSECONDLY", "microsecond"],
                 "N": ["NANOSECOND", "NANOSECONDLY", "nanosecond"]}

        msg = INVALID_FREQ_ERR_MSG
        for exp, freqs in iteritems(cases):
            for freq in freqs:
                with pytest.raises(ValueError, match=msg):
                    Period('2016-03-01 09:00', freq=freq)
                with pytest.raises(ValueError, match=msg):
                    Period(ordinal=1, freq=freq)

            # check supported freq-aliases still works
            p1 = Period('2016-03-01 09:00', freq=exp)
            p2 = Period(ordinal=1, freq=exp)
            assert isinstance(p1, Period)
            assert isinstance(p2, Period) 
Example #7
Source File: test_period.py    From elasticintel with GNU General Public License v3.0 6 votes vote down vote up
def test_period_ops_offset(self):
        p = Period('2011-04-01', freq='D')
        result = p + offsets.Day()
        exp = pd.Period('2011-04-02', freq='D')
        assert result == exp

        result = p - offsets.Day(2)
        exp = pd.Period('2011-03-30', freq='D')
        assert result == exp

        msg = r"Input cannot be converted to Period\(freq=D\)"
        with tm.assert_raises_regex(period.IncompatibleFrequency, msg):
            p + offsets.Hour(2)

        with tm.assert_raises_regex(period.IncompatibleFrequency, msg):
            p - offsets.Hour(2) 
Example #8
Source File: test_period.py    From recruit with Apache License 2.0 6 votes vote down vote up
def test_period_deprecated_freq(self):
        cases = {"M": ["MTH", "MONTH", "MONTHLY", "Mth", "month", "monthly"],
                 "B": ["BUS", "BUSINESS", "BUSINESSLY", "WEEKDAY", "bus"],
                 "D": ["DAY", "DLY", "DAILY", "Day", "Dly", "Daily"],
                 "H": ["HR", "HOUR", "HRLY", "HOURLY", "hr", "Hour", "HRly"],
                 "T": ["minute", "MINUTE", "MINUTELY", "minutely"],
                 "S": ["sec", "SEC", "SECOND", "SECONDLY", "second"],
                 "L": ["MILLISECOND", "MILLISECONDLY", "millisecond"],
                 "U": ["MICROSECOND", "MICROSECONDLY", "microsecond"],
                 "N": ["NANOSECOND", "NANOSECONDLY", "nanosecond"]}

        msg = INVALID_FREQ_ERR_MSG
        for exp, freqs in iteritems(cases):
            for freq in freqs:
                with pytest.raises(ValueError, match=msg):
                    Period('2016-03-01 09:00', freq=freq)
                with pytest.raises(ValueError, match=msg):
                    Period(ordinal=1, freq=freq)

            # check supported freq-aliases still works
            p1 = Period('2016-03-01 09:00', freq=exp)
            p2 = Period(ordinal=1, freq=exp)
            assert isinstance(p1, Period)
            assert isinstance(p2, Period) 
Example #9
Source File: test_period.py    From predictive-maintenance-using-machine-learning with Apache License 2.0 6 votes vote down vote up
def test_period_ops_offset(self):
        p = Period('2011-04-01', freq='D')
        result = p + offsets.Day()
        exp = Period('2011-04-02', freq='D')
        assert result == exp

        result = p - offsets.Day(2)
        exp = Period('2011-03-30', freq='D')
        assert result == exp

        msg = r"Input cannot be converted to Period\(freq=D\)"
        with pytest.raises(period.IncompatibleFrequency, match=msg):
            p + offsets.Hour(2)

        with pytest.raises(period.IncompatibleFrequency, match=msg):
            p - offsets.Hour(2) 
Example #10
Source File: test_period.py    From elasticintel with GNU General Public License v3.0 6 votes vote down vote up
def test_period_deprecated_freq(self):
        cases = {"M": ["MTH", "MONTH", "MONTHLY", "Mth", "month", "monthly"],
                 "B": ["BUS", "BUSINESS", "BUSINESSLY", "WEEKDAY", "bus"],
                 "D": ["DAY", "DLY", "DAILY", "Day", "Dly", "Daily"],
                 "H": ["HR", "HOUR", "HRLY", "HOURLY", "hr", "Hour", "HRly"],
                 "T": ["minute", "MINUTE", "MINUTELY", "minutely"],
                 "S": ["sec", "SEC", "SECOND", "SECONDLY", "second"],
                 "L": ["MILLISECOND", "MILLISECONDLY", "millisecond"],
                 "U": ["MICROSECOND", "MICROSECONDLY", "microsecond"],
                 "N": ["NANOSECOND", "NANOSECONDLY", "nanosecond"]}

        msg = pd.tseries.frequencies._INVALID_FREQ_ERROR
        for exp, freqs in iteritems(cases):
            for freq in freqs:
                with tm.assert_raises_regex(ValueError, msg):
                    Period('2016-03-01 09:00', freq=freq)
                with tm.assert_raises_regex(ValueError, msg):
                    Period(ordinal=1, freq=freq)

            # check supported freq-aliases still works
            p1 = Period('2016-03-01 09:00', freq=exp)
            p2 = Period(ordinal=1, freq=exp)
            assert isinstance(p1, Period)
            assert isinstance(p2, Period) 
Example #11
Source File: test_period.py    From predictive-maintenance-using-machine-learning with Apache License 2.0 4 votes vote down vote up
def test_sub_offset_nat(self):
        # freq is DateOffset
        for freq in ['A', '2A', '3A']:
            p = Period('NaT', freq=freq)
            for o in [offsets.YearEnd(2)]:
                assert p - o is NaT

            for o in [offsets.YearBegin(2), offsets.MonthBegin(1),
                      offsets.Minute(), np.timedelta64(365, 'D'),
                      timedelta(365)]:
                assert p - o is NaT

        for freq in ['M', '2M', '3M']:
            p = Period('NaT', freq=freq)
            for o in [offsets.MonthEnd(2), offsets.MonthEnd(12)]:
                assert p - o is NaT

            for o in [offsets.YearBegin(2), offsets.MonthBegin(1),
                      offsets.Minute(), np.timedelta64(365, 'D'),
                      timedelta(365)]:
                assert p - o is NaT

        # freq is Tick
        for freq in ['D', '2D', '3D']:
            p = Period('NaT', freq=freq)
            for o in [offsets.Day(5), offsets.Hour(24), np.timedelta64(2, 'D'),
                      np.timedelta64(3600 * 24, 's'), timedelta(-2),
                      timedelta(hours=48)]:
                assert p - o is NaT

            for o in [offsets.YearBegin(2), offsets.MonthBegin(1),
                      offsets.Minute(), np.timedelta64(4, 'h'),
                      timedelta(hours=23)]:
                assert p - o is NaT

        for freq in ['H', '2H', '3H']:
            p = Period('NaT', freq=freq)
            for o in [offsets.Day(2), offsets.Hour(3), np.timedelta64(3, 'h'),
                      np.timedelta64(3600, 's'), timedelta(minutes=120),
                      timedelta(days=4, minutes=180)]:
                assert p - o is NaT

            for o in [offsets.YearBegin(2), offsets.MonthBegin(1),
                      offsets.Minute(), np.timedelta64(3200, 's'),
                      timedelta(hours=23, minutes=30)]:
                assert p - o is NaT 
Example #12
Source File: test_period.py    From twitter-stock-recommendation with MIT License 4 votes vote down vote up
def test_sub_offset_nat(self):
        # freq is DateOffset
        for freq in ['A', '2A', '3A']:
            p = Period('NaT', freq=freq)
            for o in [offsets.YearEnd(2)]:
                assert p - o is tslib.NaT

            for o in [offsets.YearBegin(2), offsets.MonthBegin(1),
                      offsets.Minute(), np.timedelta64(365, 'D'),
                      timedelta(365)]:
                assert p - o is tslib.NaT

        for freq in ['M', '2M', '3M']:
            p = Period('NaT', freq=freq)
            for o in [offsets.MonthEnd(2), offsets.MonthEnd(12)]:
                assert p - o is tslib.NaT

            for o in [offsets.YearBegin(2), offsets.MonthBegin(1),
                      offsets.Minute(), np.timedelta64(365, 'D'),
                      timedelta(365)]:
                assert p - o is tslib.NaT

        # freq is Tick
        for freq in ['D', '2D', '3D']:
            p = Period('NaT', freq=freq)
            for o in [offsets.Day(5), offsets.Hour(24), np.timedelta64(2, 'D'),
                      np.timedelta64(3600 * 24, 's'), timedelta(-2),
                      timedelta(hours=48)]:
                assert p - o is tslib.NaT

            for o in [offsets.YearBegin(2), offsets.MonthBegin(1),
                      offsets.Minute(), np.timedelta64(4, 'h'),
                      timedelta(hours=23)]:
                assert p - o is tslib.NaT

        for freq in ['H', '2H', '3H']:
            p = Period('NaT', freq=freq)
            for o in [offsets.Day(2), offsets.Hour(3), np.timedelta64(3, 'h'),
                      np.timedelta64(3600, 's'), timedelta(minutes=120),
                      timedelta(days=4, minutes=180)]:
                assert p - o is tslib.NaT

            for o in [offsets.YearBegin(2), offsets.MonthBegin(1),
                      offsets.Minute(), np.timedelta64(3200, 's'),
                      timedelta(hours=23, minutes=30)]:
                assert p - o is tslib.NaT 
Example #13
Source File: test_period.py    From twitter-stock-recommendation with MIT License 4 votes vote down vote up
def test_sub_offset(self):
        # freq is DateOffset
        for freq in ['A', '2A', '3A']:
            p = Period('2011', freq=freq)
            assert p - offsets.YearEnd(2) == Period('2009', freq=freq)

            for o in [offsets.YearBegin(2), offsets.MonthBegin(1),
                      offsets.Minute(), np.timedelta64(365, 'D'),
                      timedelta(365)]:
                with pytest.raises(period.IncompatibleFrequency):
                    p - o

        for freq in ['M', '2M', '3M']:
            p = Period('2011-03', freq=freq)
            assert p - offsets.MonthEnd(2) == Period('2011-01', freq=freq)
            assert p - offsets.MonthEnd(12) == Period('2010-03', freq=freq)

            for o in [offsets.YearBegin(2), offsets.MonthBegin(1),
                      offsets.Minute(), np.timedelta64(365, 'D'),
                      timedelta(365)]:
                with pytest.raises(period.IncompatibleFrequency):
                    p - o

        # freq is Tick
        for freq in ['D', '2D', '3D']:
            p = Period('2011-04-01', freq=freq)
            assert p - offsets.Day(5) == Period('2011-03-27', freq=freq)
            assert p - offsets.Hour(24) == Period('2011-03-31', freq=freq)
            assert p - np.timedelta64(2, 'D') == Period(
                '2011-03-30', freq=freq)
            assert p - np.timedelta64(3600 * 24, 's') == Period(
                '2011-03-31', freq=freq)
            assert p - timedelta(-2) == Period('2011-04-03', freq=freq)
            assert p - timedelta(hours=48) == Period('2011-03-30', freq=freq)

            for o in [offsets.YearBegin(2), offsets.MonthBegin(1),
                      offsets.Minute(), np.timedelta64(4, 'h'),
                      timedelta(hours=23)]:
                with pytest.raises(period.IncompatibleFrequency):
                    p - o

        for freq in ['H', '2H', '3H']:
            p = Period('2011-04-01 09:00', freq=freq)
            assert p - offsets.Day(2) == Period('2011-03-30 09:00', freq=freq)
            assert p - offsets.Hour(3) == Period('2011-04-01 06:00', freq=freq)
            assert p - np.timedelta64(3, 'h') == Period(
                '2011-04-01 06:00', freq=freq)
            assert p - np.timedelta64(3600, 's') == Period(
                '2011-04-01 08:00', freq=freq)
            assert p - timedelta(minutes=120) == Period(
                '2011-04-01 07:00', freq=freq)
            assert p - timedelta(days=4, minutes=180) == Period(
                '2011-03-28 06:00', freq=freq)

            for o in [offsets.YearBegin(2), offsets.MonthBegin(1),
                      offsets.Minute(), np.timedelta64(3200, 's'),
                      timedelta(hours=23, minutes=30)]:
                with pytest.raises(period.IncompatibleFrequency):
                    p - o 
Example #14
Source File: test_period.py    From elasticintel with GNU General Public License v3.0 4 votes vote down vote up
def test_sub_offset_nat(self):
        # freq is DateOffset
        for freq in ['A', '2A', '3A']:
            p = Period('NaT', freq=freq)
            for o in [offsets.YearEnd(2)]:
                assert p - o is tslib.NaT

            for o in [offsets.YearBegin(2), offsets.MonthBegin(1),
                      offsets.Minute(), np.timedelta64(365, 'D'),
                      timedelta(365)]:
                assert p - o is tslib.NaT

        for freq in ['M', '2M', '3M']:
            p = Period('NaT', freq=freq)
            for o in [offsets.MonthEnd(2), offsets.MonthEnd(12)]:
                assert p - o is tslib.NaT

            for o in [offsets.YearBegin(2), offsets.MonthBegin(1),
                      offsets.Minute(), np.timedelta64(365, 'D'),
                      timedelta(365)]:
                assert p - o is tslib.NaT

        # freq is Tick
        for freq in ['D', '2D', '3D']:
            p = Period('NaT', freq=freq)
            for o in [offsets.Day(5), offsets.Hour(24), np.timedelta64(2, 'D'),
                      np.timedelta64(3600 * 24, 's'), timedelta(-2),
                      timedelta(hours=48)]:
                assert p - o is tslib.NaT

            for o in [offsets.YearBegin(2), offsets.MonthBegin(1),
                      offsets.Minute(), np.timedelta64(4, 'h'),
                      timedelta(hours=23)]:
                assert p - o is tslib.NaT

        for freq in ['H', '2H', '3H']:
            p = Period('NaT', freq=freq)
            for o in [offsets.Day(2), offsets.Hour(3), np.timedelta64(3, 'h'),
                      np.timedelta64(3600, 's'), timedelta(minutes=120),
                      timedelta(days=4, minutes=180)]:
                assert p - o is tslib.NaT

            for o in [offsets.YearBegin(2), offsets.MonthBegin(1),
                      offsets.Minute(), np.timedelta64(3200, 's'),
                      timedelta(hours=23, minutes=30)]:
                assert p - o is tslib.NaT 
Example #15
Source File: test_period.py    From elasticintel with GNU General Public License v3.0 4 votes vote down vote up
def test_sub_offset(self):
        # freq is DateOffset
        for freq in ['A', '2A', '3A']:
            p = Period('2011', freq=freq)
            assert p - offsets.YearEnd(2) == Period('2009', freq=freq)

            for o in [offsets.YearBegin(2), offsets.MonthBegin(1),
                      offsets.Minute(), np.timedelta64(365, 'D'),
                      timedelta(365)]:
                with pytest.raises(period.IncompatibleFrequency):
                    p - o

        for freq in ['M', '2M', '3M']:
            p = Period('2011-03', freq=freq)
            assert p - offsets.MonthEnd(2) == Period('2011-01', freq=freq)
            assert p - offsets.MonthEnd(12) == Period('2010-03', freq=freq)

            for o in [offsets.YearBegin(2), offsets.MonthBegin(1),
                      offsets.Minute(), np.timedelta64(365, 'D'),
                      timedelta(365)]:
                with pytest.raises(period.IncompatibleFrequency):
                    p - o

        # freq is Tick
        for freq in ['D', '2D', '3D']:
            p = Period('2011-04-01', freq=freq)
            assert p - offsets.Day(5) == Period('2011-03-27', freq=freq)
            assert p - offsets.Hour(24) == Period('2011-03-31', freq=freq)
            assert p - np.timedelta64(2, 'D') == Period(
                '2011-03-30', freq=freq)
            assert p - np.timedelta64(3600 * 24, 's') == Period(
                '2011-03-31', freq=freq)
            assert p - timedelta(-2) == Period('2011-04-03', freq=freq)
            assert p - timedelta(hours=48) == Period('2011-03-30', freq=freq)

            for o in [offsets.YearBegin(2), offsets.MonthBegin(1),
                      offsets.Minute(), np.timedelta64(4, 'h'),
                      timedelta(hours=23)]:
                with pytest.raises(period.IncompatibleFrequency):
                    p - o

        for freq in ['H', '2H', '3H']:
            p = Period('2011-04-01 09:00', freq=freq)
            assert p - offsets.Day(2) == Period('2011-03-30 09:00', freq=freq)
            assert p - offsets.Hour(3) == Period('2011-04-01 06:00', freq=freq)
            assert p - np.timedelta64(3, 'h') == Period(
                '2011-04-01 06:00', freq=freq)
            assert p - np.timedelta64(3600, 's') == Period(
                '2011-04-01 08:00', freq=freq)
            assert p - timedelta(minutes=120) == Period(
                '2011-04-01 07:00', freq=freq)
            assert p - timedelta(days=4, minutes=180) == Period(
                '2011-03-28 06:00', freq=freq)

            for o in [offsets.YearBegin(2), offsets.MonthBegin(1),
                      offsets.Minute(), np.timedelta64(3200, 's'),
                      timedelta(hours=23, minutes=30)]:
                with pytest.raises(period.IncompatibleFrequency):
                    p - o 
Example #16
Source File: menyanthes.py    From pastas with MIT License 4 votes vote down vote up
def read_in(self, mat):
        """Read the input part.

        """

        # Check if more then one time series model is present
        if not isinstance(mat['IN'], np.ndarray):
            mat['IN'] = [mat['IN']]

        # Read all the time series models
        for i, IN in enumerate(mat['IN']):
            data = {}

            for name in IN._fieldnames:
                if name != 'values':
                    data[name] = getattr(IN, name)
                else:
                    tindex = map(datenum_to_datetime, IN.values[:, 0])
                    series = Series(IN.values[:, 1], index=tindex)

                    # round on seconds, to get rid of conversion milliseconds
                    series.index = series.index.round('s')

                    if hasattr(IN, 'type'):
                        IN.Type = IN.type

                    if IN.Type in ['EVAP', 'PREC', 'WELL']:
                        # in menyanthes, the flux is summed over the
                        # time-step, so divide by the timestep now
                        step = series.index.to_series().diff() / offsets.Day(
                            1)
                        step = step.values.astype(np.float)
                        series = series / step
                        if series.values[0] != 0:
                            series = series[1:]

                    data['values'] = series

            # add to self.IN
            if not hasattr(IN, 'Name') and not hasattr(IN, 'name'):
                IN.Name = 'IN' + str(i)
            if hasattr(IN, 'name'):
                IN.Name = IN.name

            self.IN[IN.Name] = data 
Example #17
Source File: test_period.py    From predictive-maintenance-using-machine-learning with Apache License 2.0 4 votes vote down vote up
def test_sub_offset(self):
        # freq is DateOffset
        for freq in ['A', '2A', '3A']:
            p = Period('2011', freq=freq)
            assert p - offsets.YearEnd(2) == Period('2009', freq=freq)

            for o in [offsets.YearBegin(2), offsets.MonthBegin(1),
                      offsets.Minute(), np.timedelta64(365, 'D'),
                      timedelta(365)]:
                with pytest.raises(period.IncompatibleFrequency):
                    p - o

        for freq in ['M', '2M', '3M']:
            p = Period('2011-03', freq=freq)
            assert p - offsets.MonthEnd(2) == Period('2011-01', freq=freq)
            assert p - offsets.MonthEnd(12) == Period('2010-03', freq=freq)

            for o in [offsets.YearBegin(2), offsets.MonthBegin(1),
                      offsets.Minute(), np.timedelta64(365, 'D'),
                      timedelta(365)]:
                with pytest.raises(period.IncompatibleFrequency):
                    p - o

        # freq is Tick
        for freq in ['D', '2D', '3D']:
            p = Period('2011-04-01', freq=freq)
            assert p - offsets.Day(5) == Period('2011-03-27', freq=freq)
            assert p - offsets.Hour(24) == Period('2011-03-31', freq=freq)
            assert p - np.timedelta64(2, 'D') == Period(
                '2011-03-30', freq=freq)
            assert p - np.timedelta64(3600 * 24, 's') == Period(
                '2011-03-31', freq=freq)
            assert p - timedelta(-2) == Period('2011-04-03', freq=freq)
            assert p - timedelta(hours=48) == Period('2011-03-30', freq=freq)

            for o in [offsets.YearBegin(2), offsets.MonthBegin(1),
                      offsets.Minute(), np.timedelta64(4, 'h'),
                      timedelta(hours=23)]:
                with pytest.raises(period.IncompatibleFrequency):
                    p - o

        for freq in ['H', '2H', '3H']:
            p = Period('2011-04-01 09:00', freq=freq)
            assert p - offsets.Day(2) == Period('2011-03-30 09:00', freq=freq)
            assert p - offsets.Hour(3) == Period('2011-04-01 06:00', freq=freq)
            assert p - np.timedelta64(3, 'h') == Period(
                '2011-04-01 06:00', freq=freq)
            assert p - np.timedelta64(3600, 's') == Period(
                '2011-04-01 08:00', freq=freq)
            assert p - timedelta(minutes=120) == Period(
                '2011-04-01 07:00', freq=freq)
            assert p - timedelta(days=4, minutes=180) == Period(
                '2011-03-28 06:00', freq=freq)

            for o in [offsets.YearBegin(2), offsets.MonthBegin(1),
                      offsets.Minute(), np.timedelta64(3200, 's'),
                      timedelta(hours=23, minutes=30)]:
                with pytest.raises(period.IncompatibleFrequency):
                    p - o 
Example #18
Source File: test_period.py    From vnpy_crypto with MIT License 4 votes vote down vote up
def test_sub_offset_nat(self):
        # freq is DateOffset
        for freq in ['A', '2A', '3A']:
            p = Period('NaT', freq=freq)
            for o in [offsets.YearEnd(2)]:
                assert p - o is tslib.NaT

            for o in [offsets.YearBegin(2), offsets.MonthBegin(1),
                      offsets.Minute(), np.timedelta64(365, 'D'),
                      timedelta(365)]:
                assert p - o is tslib.NaT

        for freq in ['M', '2M', '3M']:
            p = Period('NaT', freq=freq)
            for o in [offsets.MonthEnd(2), offsets.MonthEnd(12)]:
                assert p - o is tslib.NaT

            for o in [offsets.YearBegin(2), offsets.MonthBegin(1),
                      offsets.Minute(), np.timedelta64(365, 'D'),
                      timedelta(365)]:
                assert p - o is tslib.NaT

        # freq is Tick
        for freq in ['D', '2D', '3D']:
            p = Period('NaT', freq=freq)
            for o in [offsets.Day(5), offsets.Hour(24), np.timedelta64(2, 'D'),
                      np.timedelta64(3600 * 24, 's'), timedelta(-2),
                      timedelta(hours=48)]:
                assert p - o is tslib.NaT

            for o in [offsets.YearBegin(2), offsets.MonthBegin(1),
                      offsets.Minute(), np.timedelta64(4, 'h'),
                      timedelta(hours=23)]:
                assert p - o is tslib.NaT

        for freq in ['H', '2H', '3H']:
            p = Period('NaT', freq=freq)
            for o in [offsets.Day(2), offsets.Hour(3), np.timedelta64(3, 'h'),
                      np.timedelta64(3600, 's'), timedelta(minutes=120),
                      timedelta(days=4, minutes=180)]:
                assert p - o is tslib.NaT

            for o in [offsets.YearBegin(2), offsets.MonthBegin(1),
                      offsets.Minute(), np.timedelta64(3200, 's'),
                      timedelta(hours=23, minutes=30)]:
                assert p - o is tslib.NaT 
Example #19
Source File: test_period.py    From vnpy_crypto with MIT License 4 votes vote down vote up
def test_sub_offset(self):
        # freq is DateOffset
        for freq in ['A', '2A', '3A']:
            p = Period('2011', freq=freq)
            assert p - offsets.YearEnd(2) == Period('2009', freq=freq)

            for o in [offsets.YearBegin(2), offsets.MonthBegin(1),
                      offsets.Minute(), np.timedelta64(365, 'D'),
                      timedelta(365)]:
                with pytest.raises(period.IncompatibleFrequency):
                    p - o

        for freq in ['M', '2M', '3M']:
            p = Period('2011-03', freq=freq)
            assert p - offsets.MonthEnd(2) == Period('2011-01', freq=freq)
            assert p - offsets.MonthEnd(12) == Period('2010-03', freq=freq)

            for o in [offsets.YearBegin(2), offsets.MonthBegin(1),
                      offsets.Minute(), np.timedelta64(365, 'D'),
                      timedelta(365)]:
                with pytest.raises(period.IncompatibleFrequency):
                    p - o

        # freq is Tick
        for freq in ['D', '2D', '3D']:
            p = Period('2011-04-01', freq=freq)
            assert p - offsets.Day(5) == Period('2011-03-27', freq=freq)
            assert p - offsets.Hour(24) == Period('2011-03-31', freq=freq)
            assert p - np.timedelta64(2, 'D') == Period(
                '2011-03-30', freq=freq)
            assert p - np.timedelta64(3600 * 24, 's') == Period(
                '2011-03-31', freq=freq)
            assert p - timedelta(-2) == Period('2011-04-03', freq=freq)
            assert p - timedelta(hours=48) == Period('2011-03-30', freq=freq)

            for o in [offsets.YearBegin(2), offsets.MonthBegin(1),
                      offsets.Minute(), np.timedelta64(4, 'h'),
                      timedelta(hours=23)]:
                with pytest.raises(period.IncompatibleFrequency):
                    p - o

        for freq in ['H', '2H', '3H']:
            p = Period('2011-04-01 09:00', freq=freq)
            assert p - offsets.Day(2) == Period('2011-03-30 09:00', freq=freq)
            assert p - offsets.Hour(3) == Period('2011-04-01 06:00', freq=freq)
            assert p - np.timedelta64(3, 'h') == Period(
                '2011-04-01 06:00', freq=freq)
            assert p - np.timedelta64(3600, 's') == Period(
                '2011-04-01 08:00', freq=freq)
            assert p - timedelta(minutes=120) == Period(
                '2011-04-01 07:00', freq=freq)
            assert p - timedelta(days=4, minutes=180) == Period(
                '2011-03-28 06:00', freq=freq)

            for o in [offsets.YearBegin(2), offsets.MonthBegin(1),
                      offsets.Minute(), np.timedelta64(3200, 's'),
                      timedelta(hours=23, minutes=30)]:
                with pytest.raises(period.IncompatibleFrequency):
                    p - o 
Example #20
Source File: test_period.py    From recruit with Apache License 2.0 4 votes vote down vote up
def test_sub_offset_nat(self):
        # freq is DateOffset
        for freq in ['A', '2A', '3A']:
            p = Period('NaT', freq=freq)
            for o in [offsets.YearEnd(2)]:
                assert p - o is NaT

            for o in [offsets.YearBegin(2), offsets.MonthBegin(1),
                      offsets.Minute(), np.timedelta64(365, 'D'),
                      timedelta(365)]:
                assert p - o is NaT

        for freq in ['M', '2M', '3M']:
            p = Period('NaT', freq=freq)
            for o in [offsets.MonthEnd(2), offsets.MonthEnd(12)]:
                assert p - o is NaT

            for o in [offsets.YearBegin(2), offsets.MonthBegin(1),
                      offsets.Minute(), np.timedelta64(365, 'D'),
                      timedelta(365)]:
                assert p - o is NaT

        # freq is Tick
        for freq in ['D', '2D', '3D']:
            p = Period('NaT', freq=freq)
            for o in [offsets.Day(5), offsets.Hour(24), np.timedelta64(2, 'D'),
                      np.timedelta64(3600 * 24, 's'), timedelta(-2),
                      timedelta(hours=48)]:
                assert p - o is NaT

            for o in [offsets.YearBegin(2), offsets.MonthBegin(1),
                      offsets.Minute(), np.timedelta64(4, 'h'),
                      timedelta(hours=23)]:
                assert p - o is NaT

        for freq in ['H', '2H', '3H']:
            p = Period('NaT', freq=freq)
            for o in [offsets.Day(2), offsets.Hour(3), np.timedelta64(3, 'h'),
                      np.timedelta64(3600, 's'), timedelta(minutes=120),
                      timedelta(days=4, minutes=180)]:
                assert p - o is NaT

            for o in [offsets.YearBegin(2), offsets.MonthBegin(1),
                      offsets.Minute(), np.timedelta64(3200, 's'),
                      timedelta(hours=23, minutes=30)]:
                assert p - o is NaT 
Example #21
Source File: test_period.py    From recruit with Apache License 2.0 4 votes vote down vote up
def test_sub_offset(self):
        # freq is DateOffset
        for freq in ['A', '2A', '3A']:
            p = Period('2011', freq=freq)
            assert p - offsets.YearEnd(2) == Period('2009', freq=freq)

            for o in [offsets.YearBegin(2), offsets.MonthBegin(1),
                      offsets.Minute(), np.timedelta64(365, 'D'),
                      timedelta(365)]:
                with pytest.raises(period.IncompatibleFrequency):
                    p - o

        for freq in ['M', '2M', '3M']:
            p = Period('2011-03', freq=freq)
            assert p - offsets.MonthEnd(2) == Period('2011-01', freq=freq)
            assert p - offsets.MonthEnd(12) == Period('2010-03', freq=freq)

            for o in [offsets.YearBegin(2), offsets.MonthBegin(1),
                      offsets.Minute(), np.timedelta64(365, 'D'),
                      timedelta(365)]:
                with pytest.raises(period.IncompatibleFrequency):
                    p - o

        # freq is Tick
        for freq in ['D', '2D', '3D']:
            p = Period('2011-04-01', freq=freq)
            assert p - offsets.Day(5) == Period('2011-03-27', freq=freq)
            assert p - offsets.Hour(24) == Period('2011-03-31', freq=freq)
            assert p - np.timedelta64(2, 'D') == Period(
                '2011-03-30', freq=freq)
            assert p - np.timedelta64(3600 * 24, 's') == Period(
                '2011-03-31', freq=freq)
            assert p - timedelta(-2) == Period('2011-04-03', freq=freq)
            assert p - timedelta(hours=48) == Period('2011-03-30', freq=freq)

            for o in [offsets.YearBegin(2), offsets.MonthBegin(1),
                      offsets.Minute(), np.timedelta64(4, 'h'),
                      timedelta(hours=23)]:
                with pytest.raises(period.IncompatibleFrequency):
                    p - o

        for freq in ['H', '2H', '3H']:
            p = Period('2011-04-01 09:00', freq=freq)
            assert p - offsets.Day(2) == Period('2011-03-30 09:00', freq=freq)
            assert p - offsets.Hour(3) == Period('2011-04-01 06:00', freq=freq)
            assert p - np.timedelta64(3, 'h') == Period(
                '2011-04-01 06:00', freq=freq)
            assert p - np.timedelta64(3600, 's') == Period(
                '2011-04-01 08:00', freq=freq)
            assert p - timedelta(minutes=120) == Period(
                '2011-04-01 07:00', freq=freq)
            assert p - timedelta(days=4, minutes=180) == Period(
                '2011-03-28 06:00', freq=freq)

            for o in [offsets.YearBegin(2), offsets.MonthBegin(1),
                      offsets.Minute(), np.timedelta64(3200, 's'),
                      timedelta(hours=23, minutes=30)]:
                with pytest.raises(period.IncompatibleFrequency):
                    p - o