Python pandas.Timedelta() Examples

The following are 30 code examples for showing how to use pandas.Timedelta(). These examples are extracted from open source projects. 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 check out the related API usage on the sidebar.

You may also want to check out all available functions/classes of the module pandas , or try the search function .

Example 1
Project: recruit   Author: Frank-qlu   File: test_timedeltas.py    License: Apache License 2.0 6 votes vote down vote up
def test_min_max(self):
        arr = TimedeltaArray._from_sequence([
            '3H', '3H', 'NaT', '2H', '5H', '4H',
        ])

        result = arr.min()
        expected = pd.Timedelta('2H')
        assert result == expected

        result = arr.max()
        expected = pd.Timedelta('5H')
        assert result == expected

        result = arr.min(skipna=False)
        assert result is pd.NaT

        result = arr.max(skipna=False)
        assert result is pd.NaT 
Example 2
Project: recruit   Author: Frank-qlu   File: test_datetimelike.py    License: Apache License 2.0 6 votes vote down vote up
def test_take_fill_valid(self, timedelta_index):
        tdi = timedelta_index
        arr = TimedeltaArray(tdi)

        td1 = pd.Timedelta(days=1)
        result = arr.take([-1, 1], allow_fill=True, fill_value=td1)
        assert result[0] == td1

        now = pd.Timestamp.now()
        with pytest.raises(ValueError):
            # fill_value Timestamp invalid
            arr.take([0, 1], allow_fill=True, fill_value=now)

        with pytest.raises(ValueError):
            # fill_value Period invalid
            arr.take([0, 1], allow_fill=True, fill_value=now.to_period('D')) 
Example 3
Project: recruit   Author: Frank-qlu   File: test_scalar_compat.py    License: Apache License 2.0 6 votes vote down vote up
def test_tdi_round(self):
        td = pd.timedelta_range(start='16801 days', periods=5, freq='30Min')
        elt = td[1]

        expected_rng = TimedeltaIndex([Timedelta('16801 days 00:00:00'),
                                       Timedelta('16801 days 00:00:00'),
                                       Timedelta('16801 days 01:00:00'),
                                       Timedelta('16801 days 02:00:00'),
                                       Timedelta('16801 days 02:00:00')])
        expected_elt = expected_rng[1]

        tm.assert_index_equal(td.round(freq='H'), expected_rng)
        assert elt.round(freq='H') == expected_elt

        msg = pd._libs.tslibs.frequencies.INVALID_FREQ_ERR_MSG
        with pytest.raises(ValueError, match=msg):
            td.round(freq='foo')
        with pytest.raises(ValueError, match=msg):
            elt.round(freq='foo')

        msg = "<MonthEnd> is a non-fixed frequency"
        with pytest.raises(ValueError, match=msg):
            td.round(freq='M')
        with pytest.raises(ValueError, match=msg):
            elt.round(freq='M') 
Example 4
Project: recruit   Author: Frank-qlu   File: test_indexing.py    License: Apache License 2.0 6 votes vote down vote up
def test_get_indexer(self):
        idx = pd.to_timedelta(['0 days', '1 days', '2 days'])
        tm.assert_numpy_array_equal(idx.get_indexer(idx),
                                    np.array([0, 1, 2], dtype=np.intp))

        target = pd.to_timedelta(['-1 hour', '12 hours', '1 day 1 hour'])
        tm.assert_numpy_array_equal(idx.get_indexer(target, 'pad'),
                                    np.array([-1, 0, 1], dtype=np.intp))
        tm.assert_numpy_array_equal(idx.get_indexer(target, 'backfill'),
                                    np.array([0, 1, 2], dtype=np.intp))
        tm.assert_numpy_array_equal(idx.get_indexer(target, 'nearest'),
                                    np.array([0, 1, 1], dtype=np.intp))

        res = idx.get_indexer(target, 'nearest',
                              tolerance=Timedelta('1 hour'))
        tm.assert_numpy_array_equal(res, np.array([0, -1, 1], dtype=np.intp)) 
Example 5
Project: recruit   Author: Frank-qlu   File: test_astype.py    License: Apache License 2.0 6 votes vote down vote up
def test_astype(self):
        # GH 13149, GH 13209
        idx = TimedeltaIndex([1e14, 'NaT', NaT, np.NaN])

        result = idx.astype(object)
        expected = Index([Timedelta('1 days 03:46:40')] + [NaT] * 3,
                         dtype=object)
        tm.assert_index_equal(result, expected)

        result = idx.astype(int)
        expected = Int64Index([100000000000000] + [-9223372036854775808] * 3,
                              dtype=np.int64)
        tm.assert_index_equal(result, expected)

        result = idx.astype(str)
        expected = Index(str(x) for x in idx)
        tm.assert_index_equal(result, expected)

        rng = timedelta_range('1 days', periods=10)
        result = rng.astype('i8')
        tm.assert_index_equal(result, Index(rng.asi8))
        tm.assert_numpy_array_equal(rng.asi8, result.values) 
Example 6
Project: recruit   Author: Frank-qlu   File: test_partial_slicing.py    License: Apache License 2.0 6 votes vote down vote up
def test_slice_with_negative_step(self):
        ts = Series(np.arange(20), timedelta_range('0', periods=20, freq='H'))
        SLC = pd.IndexSlice

        def assert_slices_equivalent(l_slc, i_slc):
            assert_series_equal(ts[l_slc], ts.iloc[i_slc])
            assert_series_equal(ts.loc[l_slc], ts.iloc[i_slc])
            assert_series_equal(ts.loc[l_slc], ts.iloc[i_slc])

        assert_slices_equivalent(SLC[Timedelta(hours=7)::-1], SLC[7::-1])
        assert_slices_equivalent(SLC['7 hours'::-1], SLC[7::-1])

        assert_slices_equivalent(SLC[:Timedelta(hours=7):-1], SLC[:6:-1])
        assert_slices_equivalent(SLC[:'7 hours':-1], SLC[:6:-1])

        assert_slices_equivalent(SLC['15 hours':'7 hours':-1], SLC[15:6:-1])
        assert_slices_equivalent(SLC[Timedelta(hours=15):Timedelta(hours=7):-
                                     1], SLC[15:6:-1])
        assert_slices_equivalent(SLC['15 hours':Timedelta(hours=7):-1],
                                 SLC[15:6:-1])
        assert_slices_equivalent(SLC[Timedelta(hours=15):'7 hours':-1],
                                 SLC[15:6:-1])

        assert_slices_equivalent(SLC['7 hours':'15 hours':-1], SLC[:0]) 
Example 7
Project: recruit   Author: Frank-qlu   File: test_base.py    License: Apache License 2.0 6 votes vote down vote up
def test_constructor_from_index_dtlike(self, cast_as_obj, index):
        if cast_as_obj:
            result = pd.Index(index.astype(object))
        else:
            result = pd.Index(index)

        tm.assert_index_equal(result, index)

        if isinstance(index, pd.DatetimeIndex):
            assert result.tz == index.tz
            if cast_as_obj:
                # GH#23524 check that Index(dti, dtype=object) does not
                #  incorrectly raise ValueError, and that nanoseconds are not
                #  dropped
                index += pd.Timedelta(nanoseconds=50)
                result = pd.Index(index, dtype=object)
                assert result.dtype == np.object_
                assert list(result) == list(index) 
Example 8
Project: recruit   Author: Frank-qlu   File: test_interval.py    License: Apache License 2.0 6 votes vote down vote up
def test_get_loc_datetimelike_overlapping(self, arrays):
        # GH 20636
        # overlapping = IntervalTree method with i8 conversion
        index = IntervalIndex.from_arrays(*arrays)

        value = index[0].mid + Timedelta('12 hours')
        result = np.sort(index.get_loc(value))
        expected = np.array([0, 1], dtype='intp')
        assert tm.assert_numpy_array_equal(result, expected)

        interval = Interval(index[0].left, index[1].right)
        result = np.sort(index.get_loc(interval))
        expected = np.array([0, 1, 2], dtype='intp')
        assert tm.assert_numpy_array_equal(result, expected)

    # To be removed, replaced by test_interval_new.py (see #16316, #16386) 
Example 9
Project: recruit   Author: Frank-qlu   File: test_merge_asof.py    License: Apache License 2.0 6 votes vote down vote up
def test_tolerance_tz(self):
        # GH 14844
        left = pd.DataFrame(
            {'date': pd.date_range(start=pd.to_datetime('2016-01-02'),
                                   freq='D', periods=5,
                                   tz=pytz.timezone('UTC')),
             'value1': np.arange(5)})
        right = pd.DataFrame(
            {'date': pd.date_range(start=pd.to_datetime('2016-01-01'),
                                   freq='D', periods=5,
                                   tz=pytz.timezone('UTC')),
             'value2': list("ABCDE")})
        result = pd.merge_asof(left, right, on='date',
                               tolerance=pd.Timedelta('1 day'))

        expected = pd.DataFrame(
            {'date': pd.date_range(start=pd.to_datetime('2016-01-02'),
                                   freq='D', periods=5,
                                   tz=pytz.timezone('UTC')),
             'value1': np.arange(5),
             'value2': list("BCDEE")})
        assert_frame_equal(result, expected) 
Example 10
Project: recruit   Author: Frank-qlu   File: test_ticks.py    License: Apache License 2.0 6 votes vote down vote up
def test_tick_division(cls):
    off = cls(10)

    assert off / cls(5) == 2
    assert off / 2 == cls(5)
    assert off / 2.0 == cls(5)

    assert off / off.delta == 1
    assert off / off.delta.to_timedelta64() == 1

    assert off / Nano(1) == off.delta / Nano(1).delta

    if cls is not Nano:
        # A case where we end up with a smaller class
        result = off / 1000
        assert isinstance(result, offsets.Tick)
        assert not isinstance(result, cls)
        assert result.delta == off.delta / 1000

    if cls._inc < Timedelta(seconds=1):
        # Case where we end up with a bigger class
        result = off / .001
        assert isinstance(result, offsets.Tick)
        assert not isinstance(result, cls)
        assert result.delta == off.delta / .001 
Example 11
Project: recruit   Author: Frank-qlu   File: test_ticks.py    License: Apache License 2.0 6 votes vote down vote up
def test_compare_ticks_to_strs(cls):
    # GH#23524
    off = cls(19)

    # These tests should work with any strings, but we particularly are
    #  interested in "infer" as that comparison is convenient to make in
    #  Datetime/Timedelta Array/Index constructors
    assert not off == "infer"
    assert not "foo" == off

    for left, right in [("infer", off), (off, "infer")]:
        with pytest.raises(TypeError):
            left < right
        with pytest.raises(TypeError):
            left <= right
        with pytest.raises(TypeError):
            left > right
        with pytest.raises(TypeError):
            left >= right 
Example 12
Project: recruit   Author: Frank-qlu   File: test_coercion.py    License: Apache License 2.0 6 votes vote down vote up
def test_insert_index_timedelta64(self):
        obj = pd.TimedeltaIndex(['1 day', '2 day', '3 day', '4 day'])
        assert obj.dtype == 'timedelta64[ns]'

        # timedelta64 + timedelta64 => timedelta64
        exp = pd.TimedeltaIndex(['1 day', '10 day', '2 day', '3 day', '4 day'])
        self._assert_insert_conversion(obj, pd.Timedelta('10 day'),
                                       exp, 'timedelta64[ns]')

        # ToDo: must coerce to object
        msg = "cannot insert TimedeltaIndex with incompatible label"
        with pytest.raises(TypeError, match=msg):
            obj.insert(1, pd.Timestamp('2012-01-01'))

        # ToDo: must coerce to object
        msg = "cannot insert TimedeltaIndex with incompatible label"
        with pytest.raises(TypeError, match=msg):
            obj.insert(1, 1) 
Example 13
Project: recruit   Author: Frank-qlu   File: test_indexing.py    License: Apache License 2.0 6 votes vote down vote up
def test_timedelta_assignment():
    # GH 8209
    s = Series([])
    s.loc['B'] = timedelta(1)
    tm.assert_series_equal(s, Series(Timedelta('1 days'), index=['B']))

    s = s.reindex(s.index.insert(0, 'A'))
    tm.assert_series_equal(s, Series(
        [np.nan, Timedelta('1 days')], index=['A', 'B']))

    result = s.fillna(timedelta(1))
    expected = Series(Timedelta('1 days'), index=['A', 'B'])
    tm.assert_series_equal(result, expected)

    s.loc['A'] = timedelta(1)
    tm.assert_series_equal(s, expected)

    # GH 14155
    s = Series(10 * [np.timedelta64(10, 'm')])
    s.loc[[1, 2, 3]] = np.timedelta64(20, 'm')
    expected = pd.Series(10 * [np.timedelta64(10, 'm')])
    expected.loc[[1, 2, 3]] = pd.Timedelta(np.timedelta64(20, 'm'))
    tm.assert_series_equal(s, expected) 
Example 14
Project: recruit   Author: Frank-qlu   File: test_to_csv.py    License: Apache License 2.0 6 votes vote down vote up
def test_to_csv_from_csv4(self):

        with ensure_clean('__tmp_to_csv_from_csv4__') as path:
            # GH 10833 (TimedeltaIndex formatting)
            dt = pd.Timedelta(seconds=1)
            df = pd.DataFrame({'dt_data': [i * dt for i in range(3)]},
                              index=pd.Index([i * dt for i in range(3)],
                                             name='dt_index'))
            df.to_csv(path)

            result = pd.read_csv(path, index_col='dt_index')
            result.index = pd.to_timedelta(result.index)
            # TODO: remove renaming when GH 10875 is solved
            result.index = result.index.rename('dt_index')
            result['dt_data'] = pd.to_timedelta(result['dt_data'])

            assert_frame_equal(df, result, check_index_type=True) 
Example 15
Project: recruit   Author: Frank-qlu   File: test_analytics.py    License: Apache License 2.0 6 votes vote down vote up
def test_isin_empty_datetimelike(self):
        # GH 15473
        df1_ts = DataFrame({'date':
                            pd.to_datetime(['2014-01-01', '2014-01-02'])})
        df1_td = DataFrame({'date':
                            [pd.Timedelta(1, 's'), pd.Timedelta(2, 's')]})
        df2 = DataFrame({'date': []})
        df3 = DataFrame()

        expected = DataFrame({'date': [False, False]})

        result = df1_ts.isin(df2)
        tm.assert_frame_equal(result, expected)
        result = df1_ts.isin(df3)
        tm.assert_frame_equal(result, expected)

        result = df1_td.isin(df2)
        tm.assert_frame_equal(result, expected)
        result = df1_td.isin(df3)
        tm.assert_frame_equal(result, expected)

    # Rounding 
Example 16
Project: recruit   Author: Frank-qlu   File: test_timedelta64.py    License: Apache License 2.0 6 votes vote down vote up
def test_dti_tdi_numeric_ops(self):
        # These are normally union/diff set-like ops
        tdi = TimedeltaIndex(['1 days', pd.NaT, '2 days'], name='foo')
        dti = pd.date_range('20130101', periods=3, name='bar')

        # TODO(wesm): unused?
        # td = Timedelta('1 days')
        # dt = Timestamp('20130101')

        result = tdi - tdi
        expected = TimedeltaIndex(['0 days', pd.NaT, '0 days'], name='foo')
        tm.assert_index_equal(result, expected)

        result = tdi + tdi
        expected = TimedeltaIndex(['2 days', pd.NaT, '4 days'], name='foo')
        tm.assert_index_equal(result, expected)

        result = dti - tdi  # name will be reset
        expected = DatetimeIndex(['20121231', pd.NaT, '20130101'])
        tm.assert_index_equal(result, expected) 
Example 17
Project: recruit   Author: Frank-qlu   File: test_timedelta64.py    License: Apache License 2.0 6 votes vote down vote up
def test_td64arr_add_sub_td64_nat(self, box):
        # GH#23320 special handling for timedelta64("NaT")
        tdi = pd.TimedeltaIndex([NaT, Timedelta('1s')])
        other = np.timedelta64("NaT")
        expected = pd.TimedeltaIndex(["NaT"] * 2)

        obj = tm.box_expected(tdi, box)
        expected = tm.box_expected(expected, box)

        result = obj + other
        tm.assert_equal(result, expected)
        result = other + obj
        tm.assert_equal(result, expected)
        result = obj - other
        tm.assert_equal(result, expected)
        result = other - obj
        tm.assert_equal(result, expected) 
Example 18
Project: recruit   Author: Frank-qlu   File: test_timedelta64.py    License: Apache License 2.0 6 votes vote down vote up
def test_timedelta64_conversions(self, m, unit):
        startdate = Series(pd.date_range('2013-01-01', '2013-01-03'))
        enddate = Series(pd.date_range('2013-03-01', '2013-03-03'))

        ser = enddate - startdate
        ser[2] = np.nan

        # op
        expected = Series([x / np.timedelta64(m, unit) for x in ser])
        result = ser / np.timedelta64(m, unit)
        tm.assert_series_equal(result, expected)

        # reverse op
        expected = Series([Timedelta(np.timedelta64(m, unit)) / x
                           for x in ser])
        result = np.timedelta64(m, unit) / ser
        tm.assert_series_equal(result, expected)

    # ------------------------------------------------------------------
    # Multiplication
    # organized with scalar others first, then array-like 
Example 19
Project: pysat   Author: pysat   File: _orbits.py    License: BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def __init__(self, sat=None, index=None, kind=None, period=None):
        # create null arrays for storing orbit info
        if sat is None:
            raise ValueError('Must provide a pysat instrument object when ' +
                             'initializing orbits class.')
        else:
            # self.sat = weakref.proxy(sat)
            self.sat = sat

        if kind is None:
            kind = 'local time'
        else:
            kind = kind.lower()

        if period is None:
            period = pds.Timedelta(np.timedelta64(97, 'm'))
        self.orbit_period = pds.Timedelta(period)

        if (kind == 'local time') or (kind == 'lt'):
            self._detBreaks = functools.partial(self._equaBreaks,
                                                orbit_index_period=24.)
        elif (kind == 'longitude') or (kind == 'long'):
            self._detBreaks = functools.partial(self._equaBreaks,
                                                orbit_index_period=360.)
        elif kind == 'polar':
            self._detBreaks = self._polarBreaks
        elif kind == 'orbit':
            self._detBreaks = self._orbitNumberBreaks
        else:
            raise ValueError('Unknown kind of orbit requested.')

        self._orbit_breaks = []
        self.num = 0
        self._current = 0
        self.orbit_index = index 
Example 20
Project: arctic   Author: man-group   File: test_fixes.py    License: GNU Lesser General Public License v2.1 5 votes vote down vote up
def test_compression(chunkstore_lib):
    """
    Issue 407 - Chunkstore was not removing the 1st segment, with segment id -1
    so on an append it would append new chunks with id 0 and 1, and a subsequent read
    would still pick up -1 (which should have been removed or overwritten).
    Since the -1 segment (which previously indicated a standalone segment) is no
    longer needed, the special -1 segment id is now removed
    """
    def generate_data(date):
        """
        Generates a dataframe that is almost exactly the size of
        a segment in chunkstore
        """
        df = pd.DataFrame(np.random.randn(10000*16, 12),
                          columns=['beta', 'btop', 'earnyild', 'growth', 'industry', 'leverage',
                                   'liquidty', 'momentum', 'resvol', 'sid', 'size', 'sizenl'])
        df['date'] = date

        return df

    date = pd.Timestamp('2000-01-01')
    df = generate_data(date)
    chunkstore_lib.write('test', df, chunk_size='A')
    date += pd.Timedelta(1, unit='D')
    df2 = generate_data(date)
    chunkstore_lib.append('test', df2)
    read = chunkstore_lib.read('test')
    assert_frame_equal(read, pd.concat([df, df2], ignore_index=True))


# issue #420 - ChunkStore doesnt respect DateRange interval 
Example 21
Project: recruit   Author: Frank-qlu   File: test_window.py    License: Apache License 2.0 5 votes vote down vote up
def test_missing_minp_zero_variable(self):
        # https://github.com/pandas-dev/pandas/pull/18921
        x = pd.Series([np.nan] * 4,
                      index=pd.DatetimeIndex(['2017-01-01', '2017-01-04',
                                              '2017-01-06', '2017-01-07']))
        result = x.rolling(pd.Timedelta("2d"), min_periods=0).sum()
        expected = pd.Series(0.0, index=x.index)
        tm.assert_series_equal(result, expected) 
Example 22
Project: recruit   Author: Frank-qlu   File: test_timedeltas.py    License: Apache License 2.0 5 votes vote down vote up
def test_astype_int(self, dtype):
        arr = TimedeltaArray._from_sequence([pd.Timedelta('1H'),
                                             pd.Timedelta('2H')])
        result = arr.astype(dtype)

        if np.dtype(dtype).kind == 'u':
            expected_dtype = np.dtype('uint64')
        else:
            expected_dtype = np.dtype('int64')
        expected = arr.astype(expected_dtype)

        assert result.dtype == expected_dtype
        tm.assert_numpy_array_equal(result, expected) 
Example 23
Project: recruit   Author: Frank-qlu   File: test_timedeltas.py    License: Apache License 2.0 5 votes vote down vote up
def test_setitem_clears_freq(self):
        a = TimedeltaArray(pd.timedelta_range('1H', periods=2, freq='H'))
        a[0] = pd.Timedelta("1H")
        assert a.freq is None 
Example 24
Project: recruit   Author: Frank-qlu   File: test_datetimelike.py    License: Apache License 2.0 5 votes vote down vote up
def test_round(self, tz_naive_fixture):
        # GH#24064
        tz = tz_naive_fixture
        dti = pd.date_range('2016-01-01 01:01:00', periods=3, freq='H', tz=tz)

        result = dti.round(freq='2T')
        expected = dti - pd.Timedelta(minutes=1)
        tm.assert_index_equal(result, expected) 
Example 25
Project: recruit   Author: Frank-qlu   File: test_timedelta.py    License: Apache License 2.0 5 votes vote down vote up
def test_fillna_timedelta(self):
        # GH 11343
        idx = pd.TimedeltaIndex(['1 day', pd.NaT, '3 day'])

        exp = pd.TimedeltaIndex(['1 day', '2 day', '3 day'])
        tm.assert_index_equal(idx.fillna(pd.Timedelta('2 day')), exp)

        exp = pd.TimedeltaIndex(['1 day', '3 hour', '3 day'])
        idx.fillna(pd.Timedelta('3 hour'))

        exp = pd.Index(
            [pd.Timedelta('1 day'), 'x', pd.Timedelta('3 day')], dtype=object)
        tm.assert_index_equal(idx.fillna('x'), exp) 
Example 26
Project: recruit   Author: Frank-qlu   File: test_timedelta.py    License: Apache License 2.0 5 votes vote down vote up
def test_misc_coverage(self):

        rng = timedelta_range('1 day', periods=5)
        result = rng.groupby(rng.days)
        assert isinstance(list(result.values())[0][0], Timedelta)

        idx = TimedeltaIndex(['3d', '1d', '2d'])
        assert not idx.equals(list(idx))

        non_td = Index(list('abc'))
        assert not idx.equals(list(non_td)) 
Example 27
Project: recruit   Author: Frank-qlu   File: test_timedelta.py    License: Apache License 2.0 5 votes vote down vote up
def test_append_join_nondatetimeindex(self):
        rng = timedelta_range('1 days', periods=10)
        idx = Index(['a', 'b', 'c', 'd'])

        result = rng.append(idx)
        assert isinstance(result[0], Timedelta)

        # it works
        rng.join(idx, how='outer') 
Example 28
Project: recruit   Author: Frank-qlu   File: test_indexing.py    License: Apache License 2.0 5 votes vote down vote up
def test_getitem(self):
        idx1 = timedelta_range('1 day', '31 day', freq='D', name='idx')

        for idx in [idx1]:
            result = idx[0]
            assert result == Timedelta('1 day')

            result = idx[0:5]
            expected = timedelta_range('1 day', '5 day', freq='D',
                                       name='idx')
            tm.assert_index_equal(result, expected)
            assert result.freq == expected.freq

            result = idx[0:10:2]
            expected = timedelta_range('1 day', '9 day', freq='2D',
                                       name='idx')
            tm.assert_index_equal(result, expected)
            assert result.freq == expected.freq

            result = idx[-20:-5:3]
            expected = timedelta_range('12 day', '24 day', freq='3D',
                                       name='idx')
            tm.assert_index_equal(result, expected)
            assert result.freq == expected.freq

            result = idx[4::-1]
            expected = TimedeltaIndex(['5 day', '4 day', '3 day',
                                       '2 day', '1 day'],
                                      freq='-1D', name='idx')
            tm.assert_index_equal(result, expected)
            assert result.freq == expected.freq 
Example 29
Project: recruit   Author: Frank-qlu   File: test_indexing.py    License: Apache License 2.0 5 votes vote down vote up
def test_take(self):
        # GH 10295
        idx1 = timedelta_range('1 day', '31 day', freq='D', name='idx')

        for idx in [idx1]:
            result = idx.take([0])
            assert result == Timedelta('1 day')

            result = idx.take([-1])
            assert result == Timedelta('31 day')

            result = idx.take([0, 1, 2])
            expected = timedelta_range('1 day', '3 day', freq='D',
                                       name='idx')
            tm.assert_index_equal(result, expected)
            assert result.freq == expected.freq

            result = idx.take([0, 2, 4])
            expected = timedelta_range('1 day', '5 day', freq='2D',
                                       name='idx')
            tm.assert_index_equal(result, expected)
            assert result.freq == expected.freq

            result = idx.take([7, 4, 1])
            expected = timedelta_range('8 day', '2 day', freq='-3D',
                                       name='idx')
            tm.assert_index_equal(result, expected)
            assert result.freq == expected.freq

            result = idx.take([3, 2, 5])
            expected = TimedeltaIndex(['4 day', '3 day', '6 day'], name='idx')
            tm.assert_index_equal(result, expected)
            assert result.freq is None

            result = idx.take([-3, 2, 5])
            expected = TimedeltaIndex(['29 day', '3 day', '6 day'], name='idx')
            tm.assert_index_equal(result, expected)
            assert result.freq is None 
Example 30
Project: recruit   Author: Frank-qlu   File: test_indexing.py    License: Apache License 2.0 5 votes vote down vote up
def test_get_loc(self):
        idx = pd.to_timedelta(['0 days', '1 days', '2 days'])

        for method in [None, 'pad', 'backfill', 'nearest']:
            assert idx.get_loc(idx[1], method) == 1
            assert idx.get_loc(idx[1].to_pytimedelta(), method) == 1
            assert idx.get_loc(str(idx[1]), method) == 1

        assert idx.get_loc(idx[1], 'pad',
                           tolerance=Timedelta(0)) == 1
        assert idx.get_loc(idx[1], 'pad',
                           tolerance=np.timedelta64(0, 's')) == 1
        assert idx.get_loc(idx[1], 'pad',
                           tolerance=timedelta(0)) == 1

        with pytest.raises(ValueError, match='unit abbreviation w/o a number'):
            idx.get_loc(idx[1], method='nearest', tolerance='foo')

        with pytest.raises(
                ValueError,
                match='tolerance size must match'):
            idx.get_loc(idx[1], method='nearest',
                        tolerance=[Timedelta(0).to_timedelta64(),
                                   Timedelta(0).to_timedelta64()])

        for method, loc in [('pad', 1), ('backfill', 2), ('nearest', 1)]:
            assert idx.get_loc('1 day 1 hour', method) == loc

        # GH 16909
        assert idx.get_loc(idx[1].to_timedelta64()) == 1

        # GH 16896
        assert idx.get_loc('0 days') == 0