Python pandas.tseries.offsets.Second() Examples

The following are 27 code examples of pandas.tseries.offsets.Second(). 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.tseries.offsets , or try the search function .
Example #1
Source File: test_libfrequencies.py    From vnpy_crypto with MIT License 7 votes vote down vote up
def test_is_superperiod_subperiod():

    # input validation
    assert not (is_superperiod(offsets.YearEnd(), None))
    assert not (is_subperiod(offsets.MonthEnd(), None))
    assert not (is_superperiod(None, offsets.YearEnd()))
    assert not (is_subperiod(None, offsets.MonthEnd()))
    assert not (is_superperiod(None, None))
    assert not (is_subperiod(None, None))

    assert (is_superperiod(offsets.YearEnd(), offsets.MonthEnd()))
    assert (is_subperiod(offsets.MonthEnd(), offsets.YearEnd()))

    assert (is_superperiod(offsets.Hour(), offsets.Minute()))
    assert (is_subperiod(offsets.Minute(), offsets.Hour()))

    assert (is_superperiod(offsets.Second(), offsets.Milli()))
    assert (is_subperiod(offsets.Milli(), offsets.Second()))

    assert (is_superperiod(offsets.Milli(), offsets.Micro()))
    assert (is_subperiod(offsets.Micro(), offsets.Milli()))

    assert (is_superperiod(offsets.Micro(), offsets.Nano()))
    assert (is_subperiod(offsets.Nano(), offsets.Micro())) 
Example #2
Source File: test_libfrequencies.py    From twitter-stock-recommendation with MIT License 6 votes vote down vote up
def test_is_superperiod_subperiod():

    # input validation
    assert not (is_superperiod(offsets.YearEnd(), None))
    assert not (is_subperiod(offsets.MonthEnd(), None))
    assert not (is_superperiod(None, offsets.YearEnd()))
    assert not (is_subperiod(None, offsets.MonthEnd()))
    assert not (is_superperiod(None, None))
    assert not (is_subperiod(None, None))

    assert (is_superperiod(offsets.YearEnd(), offsets.MonthEnd()))
    assert (is_subperiod(offsets.MonthEnd(), offsets.YearEnd()))

    assert (is_superperiod(offsets.Hour(), offsets.Minute()))
    assert (is_subperiod(offsets.Minute(), offsets.Hour()))

    assert (is_superperiod(offsets.Second(), offsets.Milli()))
    assert (is_subperiod(offsets.Milli(), offsets.Second()))

    assert (is_superperiod(offsets.Milli(), offsets.Micro()))
    assert (is_subperiod(offsets.Micro(), offsets.Milli()))

    assert (is_superperiod(offsets.Micro(), offsets.Nano()))
    assert (is_subperiod(offsets.Nano(), offsets.Micro())) 
Example #3
Source File: test_ticks.py    From recruit with Apache License 2.0 6 votes vote down vote up
def test_Second():
    assert_offset_equal(Second(),
                        datetime(2010, 1, 1),
                        datetime(2010, 1, 1, 0, 0, 1))
    assert_offset_equal(Second(-1),
                        datetime(2010, 1, 1, 0, 0, 1),
                        datetime(2010, 1, 1))
    assert_offset_equal(2 * Second(),
                        datetime(2010, 1, 1),
                        datetime(2010, 1, 1, 0, 0, 2))
    assert_offset_equal(-1 * Second(),
                        datetime(2010, 1, 1, 0, 0, 1),
                        datetime(2010, 1, 1))

    assert Second(3) + Second(2) == Second(5)
    assert Second(3) - Second(2) == Second() 
Example #4
Source File: test_ticks.py    From twitter-stock-recommendation with MIT License 6 votes vote down vote up
def test_Second():
    assert_offset_equal(Second(),
                        datetime(2010, 1, 1),
                        datetime(2010, 1, 1, 0, 0, 1))
    assert_offset_equal(Second(-1),
                        datetime(2010, 1, 1, 0, 0, 1),
                        datetime(2010, 1, 1))
    assert_offset_equal(2 * Second(),
                        datetime(2010, 1, 1),
                        datetime(2010, 1, 1, 0, 0, 2))
    assert_offset_equal(-1 * Second(),
                        datetime(2010, 1, 1, 0, 0, 1),
                        datetime(2010, 1, 1))

    assert Second(3) + Second(2) == Second(5)
    assert Second(3) - Second(2) == Second() 
Example #5
Source File: test_ticks.py    From vnpy_crypto with MIT License 6 votes vote down vote up
def test_Second():
    assert_offset_equal(Second(),
                        datetime(2010, 1, 1),
                        datetime(2010, 1, 1, 0, 0, 1))
    assert_offset_equal(Second(-1),
                        datetime(2010, 1, 1, 0, 0, 1),
                        datetime(2010, 1, 1))
    assert_offset_equal(2 * Second(),
                        datetime(2010, 1, 1),
                        datetime(2010, 1, 1, 0, 0, 2))
    assert_offset_equal(-1 * Second(),
                        datetime(2010, 1, 1, 0, 0, 1),
                        datetime(2010, 1, 1))

    assert Second(3) + Second(2) == Second(5)
    assert Second(3) - Second(2) == Second() 
Example #6
Source File: test_ticks.py    From coffeegrindsize with MIT License 6 votes vote down vote up
def test_Second():
    assert_offset_equal(Second(),
                        datetime(2010, 1, 1),
                        datetime(2010, 1, 1, 0, 0, 1))
    assert_offset_equal(Second(-1),
                        datetime(2010, 1, 1, 0, 0, 1),
                        datetime(2010, 1, 1))
    assert_offset_equal(2 * Second(),
                        datetime(2010, 1, 1),
                        datetime(2010, 1, 1, 0, 0, 2))
    assert_offset_equal(-1 * Second(),
                        datetime(2010, 1, 1, 0, 0, 1),
                        datetime(2010, 1, 1))

    assert Second(3) + Second(2) == Second(5)
    assert Second(3) - Second(2) == Second() 
Example #7
Source File: test_ticks.py    From predictive-maintenance-using-machine-learning with Apache License 2.0 6 votes vote down vote up
def test_Second():
    assert_offset_equal(Second(),
                        datetime(2010, 1, 1),
                        datetime(2010, 1, 1, 0, 0, 1))
    assert_offset_equal(Second(-1),
                        datetime(2010, 1, 1, 0, 0, 1),
                        datetime(2010, 1, 1))
    assert_offset_equal(2 * Second(),
                        datetime(2010, 1, 1),
                        datetime(2010, 1, 1, 0, 0, 2))
    assert_offset_equal(-1 * Second(),
                        datetime(2010, 1, 1, 0, 0, 1),
                        datetime(2010, 1, 1))

    assert Second(3) + Second(2) == Second(5)
    assert Second(3) - Second(2) == Second() 
Example #8
Source File: test_frequencies.py    From elasticintel with GNU General Public License v3.0 6 votes vote down vote up
def test_is_superperiod_subperiod():

    # input validation
    assert not (frequencies.is_superperiod(offsets.YearEnd(), None))
    assert not (frequencies.is_subperiod(offsets.MonthEnd(), None))
    assert not (frequencies.is_superperiod(None, offsets.YearEnd()))
    assert not (frequencies.is_subperiod(None, offsets.MonthEnd()))
    assert not (frequencies.is_superperiod(None, None))
    assert not (frequencies.is_subperiod(None, None))

    assert (frequencies.is_superperiod(offsets.YearEnd(), offsets.MonthEnd()))
    assert (frequencies.is_subperiod(offsets.MonthEnd(), offsets.YearEnd()))

    assert (frequencies.is_superperiod(offsets.Hour(), offsets.Minute()))
    assert (frequencies.is_subperiod(offsets.Minute(), offsets.Hour()))

    assert (frequencies.is_superperiod(offsets.Second(), offsets.Milli()))
    assert (frequencies.is_subperiod(offsets.Milli(), offsets.Second()))

    assert (frequencies.is_superperiod(offsets.Milli(), offsets.Micro()))
    assert (frequencies.is_subperiod(offsets.Micro(), offsets.Milli()))

    assert (frequencies.is_superperiod(offsets.Micro(), offsets.Nano()))
    assert (frequencies.is_subperiod(offsets.Nano(), offsets.Micro())) 
Example #9
Source File: test_frequencies.py    From predictive-maintenance-using-machine-learning with Apache License 2.0 5 votes vote down vote up
def test_to_offset_pd_timedelta(self):
        # Tests for #9064
        td = Timedelta(days=1, seconds=1)
        result = frequencies.to_offset(td)
        expected = offsets.Second(86401)
        assert (expected == result)

        td = Timedelta(days=-1, seconds=1)
        result = frequencies.to_offset(td)
        expected = offsets.Second(-86399)
        assert (expected == result)

        td = Timedelta(hours=1, minutes=10)
        result = frequencies.to_offset(td)
        expected = offsets.Minute(70)
        assert (expected == result)

        td = Timedelta(hours=1, minutes=-10)
        result = frequencies.to_offset(td)
        expected = offsets.Minute(50)
        assert (expected == result)

        td = Timedelta(weeks=1)
        result = frequencies.to_offset(td)
        expected = offsets.Day(7)
        assert (expected == result)

        td1 = Timedelta(hours=1)
        result1 = frequencies.to_offset(td1)
        result2 = frequencies.to_offset('60min')
        assert (result1 == result2)

        td = Timedelta(microseconds=1)
        result = frequencies.to_offset(td)
        expected = offsets.Micro(1)
        assert (expected == result)

        td = Timedelta(microseconds=0)
        pytest.raises(ValueError, lambda: frequencies.to_offset(td)) 
Example #10
Source File: test_frequencies.py    From twitter-stock-recommendation with MIT License 5 votes vote down vote up
def test_to_offset_pd_timedelta(self):
        # Tests for #9064
        td = Timedelta(days=1, seconds=1)
        result = frequencies.to_offset(td)
        expected = offsets.Second(86401)
        assert (expected == result)

        td = Timedelta(days=-1, seconds=1)
        result = frequencies.to_offset(td)
        expected = offsets.Second(-86399)
        assert (expected == result)

        td = Timedelta(hours=1, minutes=10)
        result = frequencies.to_offset(td)
        expected = offsets.Minute(70)
        assert (expected == result)

        td = Timedelta(hours=1, minutes=-10)
        result = frequencies.to_offset(td)
        expected = offsets.Minute(50)
        assert (expected == result)

        td = Timedelta(weeks=1)
        result = frequencies.to_offset(td)
        expected = offsets.Day(7)
        assert (expected == result)

        td1 = Timedelta(hours=1)
        result1 = frequencies.to_offset(td1)
        result2 = frequencies.to_offset('60min')
        assert (result1 == result2)

        td = Timedelta(microseconds=1)
        result = frequencies.to_offset(td)
        expected = offsets.Micro(1)
        assert (expected == result)

        td = Timedelta(microseconds=0)
        pytest.raises(ValueError, lambda: frequencies.to_offset(td)) 
Example #11
Source File: test_converter.py    From twitter-stock-recommendation with MIT License 5 votes vote down vote up
def test_resolution(self):
        def _assert_less(ts1, ts2):
            val1 = self.dtc.convert(ts1, None, None)
            val2 = self.dtc.convert(ts2, None, None)
            if not val1 < val2:
                raise AssertionError('{0} is not less than {1}.'.format(val1,
                                                                        val2))

        # Matplotlib's time representation using floats cannot distinguish
        # intervals smaller than ~10 microsecond in the common range of years.
        ts = Timestamp('2012-1-1')
        _assert_less(ts, ts + Second())
        _assert_less(ts, ts + Milli())
        _assert_less(ts, ts + Micro(50)) 
Example #12
Source File: test_timedelta_range.py    From twitter-stock-recommendation with MIT License 5 votes vote down vote up
def test_timedelta_range(self):

        expected = to_timedelta(np.arange(5), unit='D')
        result = timedelta_range('0 days', periods=5, freq='D')
        tm.assert_index_equal(result, expected)

        expected = to_timedelta(np.arange(11), unit='D')
        result = timedelta_range('0 days', '10 days', freq='D')
        tm.assert_index_equal(result, expected)

        expected = to_timedelta(np.arange(5), unit='D') + Second(2) + Day()
        result = timedelta_range('1 days, 00:00:02', '5 days, 00:00:02',
                                 freq='D')
        tm.assert_index_equal(result, expected)

        expected = to_timedelta([1, 3, 5, 7, 9], unit='D') + Second(2)
        result = timedelta_range('1 days, 00:00:02', periods=5, freq='2D')
        tm.assert_index_equal(result, expected)

        expected = to_timedelta(np.arange(50), unit='T') * 30
        result = timedelta_range('0 days', freq='30T', periods=50)
        tm.assert_index_equal(result, expected)

        # GH 11776
        arr = np.arange(10).reshape(2, 5)
        df = pd.DataFrame(np.arange(10).reshape(2, 5))
        for arg in (arr, df):
            with tm.assert_raises_regex(TypeError, "1-d array"):
                to_timedelta(arg)
            for errors in ['ignore', 'raise', 'coerce']:
                with tm.assert_raises_regex(TypeError, "1-d array"):
                    to_timedelta(arg, errors=errors)

        # issue10583
        df = pd.DataFrame(np.random.normal(size=(10, 4)))
        df.index = pd.timedelta_range(start='0s', periods=10, freq='s')
        expected = df.loc[pd.Timedelta('0s'):, :]
        result = df.loc['0s':, :]
        tm.assert_frame_equal(expected, result) 
Example #13
Source File: test_frequencies.py    From coffeegrindsize with MIT License 5 votes vote down vote up
def test_to_offset_pd_timedelta(self):
        # Tests for #9064
        td = Timedelta(days=1, seconds=1)
        result = frequencies.to_offset(td)
        expected = offsets.Second(86401)
        assert (expected == result)

        td = Timedelta(days=-1, seconds=1)
        result = frequencies.to_offset(td)
        expected = offsets.Second(-86399)
        assert (expected == result)

        td = Timedelta(hours=1, minutes=10)
        result = frequencies.to_offset(td)
        expected = offsets.Minute(70)
        assert (expected == result)

        td = Timedelta(hours=1, minutes=-10)
        result = frequencies.to_offset(td)
        expected = offsets.Minute(50)
        assert (expected == result)

        td = Timedelta(weeks=1)
        result = frequencies.to_offset(td)
        expected = offsets.Day(7)
        assert (expected == result)

        td1 = Timedelta(hours=1)
        result1 = frequencies.to_offset(td1)
        result2 = frequencies.to_offset('60min')
        assert (result1 == result2)

        td = Timedelta(microseconds=1)
        result = frequencies.to_offset(td)
        expected = offsets.Micro(1)
        assert (expected == result)

        td = Timedelta(microseconds=0)
        pytest.raises(ValueError, lambda: frequencies.to_offset(td)) 
Example #14
Source File: test_converter.py    From coffeegrindsize with MIT License 5 votes vote down vote up
def test_resolution(self):
        def _assert_less(ts1, ts2):
            val1 = self.dtc.convert(ts1, None, None)
            val2 = self.dtc.convert(ts2, None, None)
            if not val1 < val2:
                raise AssertionError('{0} is not less than {1}.'.format(val1,
                                                                        val2))

        # Matplotlib's time representation using floats cannot distinguish
        # intervals smaller than ~10 microsecond in the common range of years.
        ts = Timestamp('2012-1-1')
        _assert_less(ts, ts + Second())
        _assert_less(ts, ts + Milli())
        _assert_less(ts, ts + Micro(50)) 
Example #15
Source File: test_timedelta_range.py    From coffeegrindsize with MIT License 5 votes vote down vote up
def test_timedelta_range(self):

        expected = to_timedelta(np.arange(5), unit='D')
        result = timedelta_range('0 days', periods=5, freq='D')
        tm.assert_index_equal(result, expected)

        expected = to_timedelta(np.arange(11), unit='D')
        result = timedelta_range('0 days', '10 days', freq='D')
        tm.assert_index_equal(result, expected)

        expected = to_timedelta(np.arange(5), unit='D') + Second(2) + Day()
        result = timedelta_range('1 days, 00:00:02', '5 days, 00:00:02',
                                 freq='D')
        tm.assert_index_equal(result, expected)

        expected = to_timedelta([1, 3, 5, 7, 9], unit='D') + Second(2)
        result = timedelta_range('1 days, 00:00:02', periods=5, freq='2D')
        tm.assert_index_equal(result, expected)

        expected = to_timedelta(np.arange(50), unit='T') * 30
        result = timedelta_range('0 days', freq='30T', periods=50)
        tm.assert_index_equal(result, expected)

        # GH 11776
        arr = np.arange(10).reshape(2, 5)
        df = pd.DataFrame(np.arange(10).reshape(2, 5))
        for arg in (arr, df):
            with pytest.raises(TypeError, match="1-d array"):
                to_timedelta(arg)
            for errors in ['ignore', 'raise', 'coerce']:
                with pytest.raises(TypeError, match="1-d array"):
                    to_timedelta(arg, errors=errors)

        # issue10583
        df = pd.DataFrame(np.random.normal(size=(10, 4)))
        df.index = pd.timedelta_range(start='0s', periods=10, freq='s')
        expected = df.loc[pd.Timedelta('0s'):, :]
        result = df.loc['0s':, :]
        tm.assert_frame_equal(expected, result) 
Example #16
Source File: test_frequencies.py    From elasticintel with GNU General Public License v3.0 5 votes vote down vote up
def test_to_offset_pd_timedelta(self):
        # Tests for #9064
        td = Timedelta(days=1, seconds=1)
        result = frequencies.to_offset(td)
        expected = offsets.Second(86401)
        assert (expected == result)

        td = Timedelta(days=-1, seconds=1)
        result = frequencies.to_offset(td)
        expected = offsets.Second(-86399)
        assert (expected == result)

        td = Timedelta(hours=1, minutes=10)
        result = frequencies.to_offset(td)
        expected = offsets.Minute(70)
        assert (expected == result)

        td = Timedelta(hours=1, minutes=-10)
        result = frequencies.to_offset(td)
        expected = offsets.Minute(50)
        assert (expected == result)

        td = Timedelta(weeks=1)
        result = frequencies.to_offset(td)
        expected = offsets.Day(7)
        assert (expected == result)

        td1 = Timedelta(hours=1)
        result1 = frequencies.to_offset(td1)
        result2 = frequencies.to_offset('60min')
        assert (result1 == result2)

        td = Timedelta(microseconds=1)
        result = frequencies.to_offset(td)
        expected = offsets.Micro(1)
        assert (expected == result)

        td = Timedelta(microseconds=0)
        pytest.raises(ValueError, lambda: frequencies.to_offset(td)) 
Example #17
Source File: test_converter.py    From elasticintel with GNU General Public License v3.0 5 votes vote down vote up
def test_resolution(self):
        def _assert_less(ts1, ts2):
            val1 = self.dtc.convert(ts1, None, None)
            val2 = self.dtc.convert(ts2, None, None)
            if not val1 < val2:
                raise AssertionError('{0} is not less than {1}.'.format(val1,
                                                                        val2))

        # Matplotlib's time representation using floats cannot distinguish
        # intervals smaller than ~10 microsecond in the common range of years.
        ts = Timestamp('2012-1-1')
        _assert_less(ts, ts + Second())
        _assert_less(ts, ts + Milli())
        _assert_less(ts, ts + Micro(50)) 
Example #18
Source File: test_timedelta_range.py    From elasticintel with GNU General Public License v3.0 5 votes vote down vote up
def test_timedelta_range(self):

        expected = to_timedelta(np.arange(5), unit='D')
        result = timedelta_range('0 days', periods=5, freq='D')
        tm.assert_index_equal(result, expected)

        expected = to_timedelta(np.arange(11), unit='D')
        result = timedelta_range('0 days', '10 days', freq='D')
        tm.assert_index_equal(result, expected)

        expected = to_timedelta(np.arange(5), unit='D') + Second(2) + Day()
        result = timedelta_range('1 days, 00:00:02', '5 days, 00:00:02',
                                 freq='D')
        tm.assert_index_equal(result, expected)

        expected = to_timedelta([1, 3, 5, 7, 9], unit='D') + Second(2)
        result = timedelta_range('1 days, 00:00:02', periods=5, freq='2D')
        tm.assert_index_equal(result, expected)

        expected = to_timedelta(np.arange(50), unit='T') * 30
        result = timedelta_range('0 days', freq='30T', periods=50)
        tm.assert_index_equal(result, expected)

        # GH 11776
        arr = np.arange(10).reshape(2, 5)
        df = pd.DataFrame(np.arange(10).reshape(2, 5))
        for arg in (arr, df):
            with tm.assert_raises_regex(TypeError, "1-d array"):
                to_timedelta(arg)
            for errors in ['ignore', 'raise', 'coerce']:
                with tm.assert_raises_regex(TypeError, "1-d array"):
                    to_timedelta(arg, errors=errors)

        # issue10583
        df = pd.DataFrame(np.random.normal(size=(10, 4)))
        df.index = pd.timedelta_range(start='0s', periods=10, freq='s')
        expected = df.loc[pd.Timedelta('0s'):, :]
        result = df.loc['0s':, :]
        assert_frame_equal(expected, result) 
Example #19
Source File: test_timedelta_range.py    From recruit with Apache License 2.0 5 votes vote down vote up
def test_timedelta_range(self):

        expected = to_timedelta(np.arange(5), unit='D')
        result = timedelta_range('0 days', periods=5, freq='D')
        tm.assert_index_equal(result, expected)

        expected = to_timedelta(np.arange(11), unit='D')
        result = timedelta_range('0 days', '10 days', freq='D')
        tm.assert_index_equal(result, expected)

        expected = to_timedelta(np.arange(5), unit='D') + Second(2) + Day()
        result = timedelta_range('1 days, 00:00:02', '5 days, 00:00:02',
                                 freq='D')
        tm.assert_index_equal(result, expected)

        expected = to_timedelta([1, 3, 5, 7, 9], unit='D') + Second(2)
        result = timedelta_range('1 days, 00:00:02', periods=5, freq='2D')
        tm.assert_index_equal(result, expected)

        expected = to_timedelta(np.arange(50), unit='T') * 30
        result = timedelta_range('0 days', freq='30T', periods=50)
        tm.assert_index_equal(result, expected)

        # GH 11776
        arr = np.arange(10).reshape(2, 5)
        df = pd.DataFrame(np.arange(10).reshape(2, 5))
        for arg in (arr, df):
            with pytest.raises(TypeError, match="1-d array"):
                to_timedelta(arg)
            for errors in ['ignore', 'raise', 'coerce']:
                with pytest.raises(TypeError, match="1-d array"):
                    to_timedelta(arg, errors=errors)

        # issue10583
        df = pd.DataFrame(np.random.normal(size=(10, 4)))
        df.index = pd.timedelta_range(start='0s', periods=10, freq='s')
        expected = df.loc[pd.Timedelta('0s'):, :]
        result = df.loc['0s':, :]
        tm.assert_frame_equal(expected, result) 
Example #20
Source File: test_converter.py    From predictive-maintenance-using-machine-learning with Apache License 2.0 5 votes vote down vote up
def test_resolution(self):
        def _assert_less(ts1, ts2):
            val1 = self.dtc.convert(ts1, None, None)
            val2 = self.dtc.convert(ts2, None, None)
            if not val1 < val2:
                raise AssertionError('{0} is not less than {1}.'.format(val1,
                                                                        val2))

        # Matplotlib's time representation using floats cannot distinguish
        # intervals smaller than ~10 microsecond in the common range of years.
        ts = Timestamp('2012-1-1')
        _assert_less(ts, ts + Second())
        _assert_less(ts, ts + Milli())
        _assert_less(ts, ts + Micro(50)) 
Example #21
Source File: test_timedelta_range.py    From predictive-maintenance-using-machine-learning with Apache License 2.0 5 votes vote down vote up
def test_timedelta_range(self):

        expected = to_timedelta(np.arange(5), unit='D')
        result = timedelta_range('0 days', periods=5, freq='D')
        tm.assert_index_equal(result, expected)

        expected = to_timedelta(np.arange(11), unit='D')
        result = timedelta_range('0 days', '10 days', freq='D')
        tm.assert_index_equal(result, expected)

        expected = to_timedelta(np.arange(5), unit='D') + Second(2) + Day()
        result = timedelta_range('1 days, 00:00:02', '5 days, 00:00:02',
                                 freq='D')
        tm.assert_index_equal(result, expected)

        expected = to_timedelta([1, 3, 5, 7, 9], unit='D') + Second(2)
        result = timedelta_range('1 days, 00:00:02', periods=5, freq='2D')
        tm.assert_index_equal(result, expected)

        expected = to_timedelta(np.arange(50), unit='T') * 30
        result = timedelta_range('0 days', freq='30T', periods=50)
        tm.assert_index_equal(result, expected)

        # GH 11776
        arr = np.arange(10).reshape(2, 5)
        df = pd.DataFrame(np.arange(10).reshape(2, 5))
        for arg in (arr, df):
            with pytest.raises(TypeError, match="1-d array"):
                to_timedelta(arg)
            for errors in ['ignore', 'raise', 'coerce']:
                with pytest.raises(TypeError, match="1-d array"):
                    to_timedelta(arg, errors=errors)

        # issue10583
        df = pd.DataFrame(np.random.normal(size=(10, 4)))
        df.index = pd.timedelta_range(start='0s', periods=10, freq='s')
        expected = df.loc[pd.Timedelta('0s'):, :]
        result = df.loc['0s':, :]
        tm.assert_frame_equal(expected, result) 
Example #22
Source File: test_frequencies.py    From vnpy_crypto with MIT License 5 votes vote down vote up
def test_to_offset_pd_timedelta(self):
        # Tests for #9064
        td = Timedelta(days=1, seconds=1)
        result = frequencies.to_offset(td)
        expected = offsets.Second(86401)
        assert (expected == result)

        td = Timedelta(days=-1, seconds=1)
        result = frequencies.to_offset(td)
        expected = offsets.Second(-86399)
        assert (expected == result)

        td = Timedelta(hours=1, minutes=10)
        result = frequencies.to_offset(td)
        expected = offsets.Minute(70)
        assert (expected == result)

        td = Timedelta(hours=1, minutes=-10)
        result = frequencies.to_offset(td)
        expected = offsets.Minute(50)
        assert (expected == result)

        td = Timedelta(weeks=1)
        result = frequencies.to_offset(td)
        expected = offsets.Day(7)
        assert (expected == result)

        td1 = Timedelta(hours=1)
        result1 = frequencies.to_offset(td1)
        result2 = frequencies.to_offset('60min')
        assert (result1 == result2)

        td = Timedelta(microseconds=1)
        result = frequencies.to_offset(td)
        expected = offsets.Micro(1)
        assert (expected == result)

        td = Timedelta(microseconds=0)
        pytest.raises(ValueError, lambda: frequencies.to_offset(td)) 
Example #23
Source File: test_converter.py    From vnpy_crypto with MIT License 5 votes vote down vote up
def test_resolution(self):
        def _assert_less(ts1, ts2):
            val1 = self.dtc.convert(ts1, None, None)
            val2 = self.dtc.convert(ts2, None, None)
            if not val1 < val2:
                raise AssertionError('{0} is not less than {1}.'.format(val1,
                                                                        val2))

        # Matplotlib's time representation using floats cannot distinguish
        # intervals smaller than ~10 microsecond in the common range of years.
        ts = Timestamp('2012-1-1')
        _assert_less(ts, ts + Second())
        _assert_less(ts, ts + Milli())
        _assert_less(ts, ts + Micro(50)) 
Example #24
Source File: test_timedelta_range.py    From vnpy_crypto with MIT License 5 votes vote down vote up
def test_timedelta_range(self):

        expected = to_timedelta(np.arange(5), unit='D')
        result = timedelta_range('0 days', periods=5, freq='D')
        tm.assert_index_equal(result, expected)

        expected = to_timedelta(np.arange(11), unit='D')
        result = timedelta_range('0 days', '10 days', freq='D')
        tm.assert_index_equal(result, expected)

        expected = to_timedelta(np.arange(5), unit='D') + Second(2) + Day()
        result = timedelta_range('1 days, 00:00:02', '5 days, 00:00:02',
                                 freq='D')
        tm.assert_index_equal(result, expected)

        expected = to_timedelta([1, 3, 5, 7, 9], unit='D') + Second(2)
        result = timedelta_range('1 days, 00:00:02', periods=5, freq='2D')
        tm.assert_index_equal(result, expected)

        expected = to_timedelta(np.arange(50), unit='T') * 30
        result = timedelta_range('0 days', freq='30T', periods=50)
        tm.assert_index_equal(result, expected)

        # GH 11776
        arr = np.arange(10).reshape(2, 5)
        df = pd.DataFrame(np.arange(10).reshape(2, 5))
        for arg in (arr, df):
            with tm.assert_raises_regex(TypeError, "1-d array"):
                to_timedelta(arg)
            for errors in ['ignore', 'raise', 'coerce']:
                with tm.assert_raises_regex(TypeError, "1-d array"):
                    to_timedelta(arg, errors=errors)

        # issue10583
        df = pd.DataFrame(np.random.normal(size=(10, 4)))
        df.index = pd.timedelta_range(start='0s', periods=10, freq='s')
        expected = df.loc[pd.Timedelta('0s'):, :]
        result = df.loc['0s':, :]
        tm.assert_frame_equal(expected, result) 
Example #25
Source File: test_frequencies.py    From recruit with Apache License 2.0 5 votes vote down vote up
def test_to_offset_pd_timedelta(self):
        # Tests for #9064
        td = Timedelta(days=1, seconds=1)
        result = frequencies.to_offset(td)
        expected = offsets.Second(86401)
        assert (expected == result)

        td = Timedelta(days=-1, seconds=1)
        result = frequencies.to_offset(td)
        expected = offsets.Second(-86399)
        assert (expected == result)

        td = Timedelta(hours=1, minutes=10)
        result = frequencies.to_offset(td)
        expected = offsets.Minute(70)
        assert (expected == result)

        td = Timedelta(hours=1, minutes=-10)
        result = frequencies.to_offset(td)
        expected = offsets.Minute(50)
        assert (expected == result)

        td = Timedelta(weeks=1)
        result = frequencies.to_offset(td)
        expected = offsets.Day(7)
        assert (expected == result)

        td1 = Timedelta(hours=1)
        result1 = frequencies.to_offset(td1)
        result2 = frequencies.to_offset('60min')
        assert (result1 == result2)

        td = Timedelta(microseconds=1)
        result = frequencies.to_offset(td)
        expected = offsets.Micro(1)
        assert (expected == result)

        td = Timedelta(microseconds=0)
        pytest.raises(ValueError, lambda: frequencies.to_offset(td)) 
Example #26
Source File: test_converter.py    From recruit with Apache License 2.0 5 votes vote down vote up
def test_resolution(self):
        def _assert_less(ts1, ts2):
            val1 = self.dtc.convert(ts1, None, None)
            val2 = self.dtc.convert(ts2, None, None)
            if not val1 < val2:
                raise AssertionError('{0} is not less than {1}.'.format(val1,
                                                                        val2))

        # Matplotlib's time representation using floats cannot distinguish
        # intervals smaller than ~10 microsecond in the common range of years.
        ts = Timestamp('2012-1-1')
        _assert_less(ts, ts + Second())
        _assert_less(ts, ts + Milli())
        _assert_less(ts, ts + Micro(50)) 
Example #27
Source File: test_frequencies.py    From Computable with MIT License 4 votes vote down vote up
def test_to_offset_multiple():
    freqstr = '2h30min'
    freqstr2 = '2h 30min'

    result = to_offset(freqstr)
    assert(result == to_offset(freqstr2))
    expected = offsets.Minute(150)
    assert(result == expected)

    freqstr = '2h30min15s'
    result = to_offset(freqstr)
    expected = offsets.Second(150 * 60 + 15)
    assert(result == expected)

    freqstr = '2h 60min'
    result = to_offset(freqstr)
    expected = offsets.Hour(3)
    assert(result == expected)

    freqstr = '15l500u'
    result = to_offset(freqstr)
    expected = offsets.Micro(15500)
    assert(result == expected)

    freqstr = '10s75L'
    result = to_offset(freqstr)
    expected = offsets.Milli(10075)
    assert(result == expected)

    if not _np_version_under1p7:
        freqstr = '2800N'
        result = to_offset(freqstr)
        expected = offsets.Nano(2800)
        assert(result == expected)

    # malformed
    try:
        to_offset('2h20m')
    except ValueError:
        pass
    else:
        assert(False)