Python pandas.DateOffset() Examples

The following are 30 code examples of pandas.DateOffset(). 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 , or try the search function .
Example #1
Source File: test_sw.py    From pysat with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_calc_f107a_daily_missing(self):
        """ Test the calc_f107a routine with some daily data missing"""

        self.testInst.data = pds.DataFrame({'f107': np.linspace(70, 200, 160)},
                                           index=[pysat.datetime(2009, 1, 1)
                                                  + pds.DateOffset(days=2*i+1)
                                                  for i in range(160)])
        sw_f107.calc_f107a(self.testInst, f107_name='f107', f107a_name='f107a')

        # Assert that new data and metadata exist
        assert 'f107a' in self.testInst.data.columns
        assert 'f107a' in self.testInst.meta.keys()

        # Assert the finite values have realistic means
        assert(np.nanmin(self.testInst['f107a'])
               > np.nanmin(self.testInst['f107']))
        assert(np.nanmax(self.testInst['f107a'])
               < np.nanmax(self.testInst['f107']))

        # Assert the expected number of fill values
        assert(len(self.testInst['f107a'][np.isnan(self.testInst['f107a'])])
               == 40) 
Example #2
Source File: test_algos.py    From bt with MIT License 6 votes vote down vote up
def test_weigh_erc(mock_erc):
    algo = algos.WeighERC(lookback=pd.DateOffset(days=5))

    mock_erc.return_value = pd.Series({'c1': 0.3, 'c2': 0.7})

    s = bt.Strategy('s')

    dts = pd.date_range('2010-01-01', periods=5)
    data = pd.DataFrame(index=dts, columns=['c1', 'c2'], data=100.)

    s.setup(data)
    s.update(dts[4])
    s.temp['selected'] = ['c1', 'c2']

    assert algo(s)
    assert mock_erc.called
    rets = mock_erc.call_args[0][0]
    assert len(rets) == 4
    assert 'c1' in rets
    assert 'c2' in rets

    weights = s.temp['weights']
    assert len(weights) == 2
    assert weights['c1'] == 0.3
    assert weights['c2'] == 0.7 
Example #3
Source File: test_algos.py    From bt with MIT License 6 votes vote down vote up
def test_select_has_data_preselected():
    algo = algos.SelectHasData(min_count=3, lookback=pd.DateOffset(days=3))

    s = bt.Strategy('s')

    dts = pd.date_range('2010-01-01', periods=3)
    data = pd.DataFrame(index=dts, columns=['c1', 'c2'], data=100.)
    data['c1'].ix[dts[0]] = np.nan
    data['c1'].ix[dts[1]] = np.nan

    s.setup(data)
    s.update(dts[2])
    s.temp['selected'] = ['c1']

    assert algo(s)
    selected = s.temp['selected']
    assert len(selected) == 0 
Example #4
Source File: test_custom.py    From pysat with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_single_adding_custom_function_wrong_times(self):
        """Only the data at the correct time should be accepted, otherwise it
        returns nan
        """
        def custom1(inst):
            new_index = inst.index+pds.DateOffset(milliseconds=500)
            d = pds.Series(2.0 * inst['mlt'], index=new_index)
            d.name = 'doubleMLT'
            print(new_index)
            return d

        self.add(custom1, 'add')
        self.testInst.load(2009, 1)
        ans = (self.testInst['doubleMLT'].isnull()).all()
        if self.testInst.pandas_format:
            assert ans
        else:
            print("Warning! Xarray doesn't enforce the same times on all " +
                  "parameters in dataset.") 
Example #5
Source File: test_orbits.py    From pysat with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def setup(self):
        """Runs before every method to create a clean testing setup."""
        info = {'index': 'mlt'}
        self.testInst = pysat.Instrument('pysat', 'testing_xarray',
                                         clean_level='clean',
                                         orbit_info=info)
        times = [[pysat.datetime(2008, 12, 31, 4),
                  pysat.datetime(2008, 12, 31, 5, 37)],
                 [pysat.datetime(2009, 1, 1),
                  pysat.datetime(2009, 1, 1, 1, 37)]
                 ]
        for seconds in np.arange(38):
            day = pysat.datetime(2009, 1, 2) + \
                pds.DateOffset(days=int(seconds))
            times.append([day, day +
                          pds.DateOffset(hours=1, minutes=37,
                                         seconds=int(seconds)) -
                          pds.DateOffset(seconds=20)])

        self.testInst.custom.add(filter_data2, 'modify', times=times) 
Example #6
Source File: test_orbits.py    From pysat with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def setup(self):
        """Runs before every method to create a clean testing setup."""
        info = {'index': 'mlt'}
        self.testInst = pysat.Instrument('pysat', 'testing',
                                         clean_level='clean',
                                         orbit_info=info)
        times = [[pysat.datetime(2008, 12, 31, 4),
                  pysat.datetime(2008, 12, 31, 5, 37)],
                 [pysat.datetime(2009, 1, 1),
                  pysat.datetime(2009, 1, 1, 1, 37)]
                 ]
        for seconds in np.arange(38):
            day = pysat.datetime(2009, 1, 2) + \
                pds.DateOffset(days=int(seconds))
            times.append([day, day +
                          pds.DateOffset(hours=1, minutes=37,
                                         seconds=int(seconds)) -
                          pds.DateOffset(seconds=20)])

        self.testInst.custom.add(filter_data2, 'modify', times=times) 
Example #7
Source File: test_algos.py    From bt with MIT License 6 votes vote down vote up
def test_select_momentum():
    algo = algos.SelectMomentum(n=1, lookback=pd.DateOffset(days=3))

    s = bt.Strategy('s')

    dts = pd.date_range('2010-01-01', periods=3)
    data = pd.DataFrame(index=dts, columns=['c1', 'c2'], data=100.)
    data['c1'].ix[dts[2]] = 105
    data['c2'].ix[dts[2]] = 95

    s.setup(data)
    s.update(dts[2])
    s.temp['selected'] = ['c1', 'c2']

    assert algo(s)
    actual = s.temp['selected']
    assert len(actual) == 1
    assert 'c1' in actual 
Example #8
Source File: test_algos.py    From bt with MIT License 6 votes vote down vote up
def test_weigh_mean_var(mock_mv):
    algo = algos.WeighMeanVar(lookback=pd.DateOffset(days=5))

    mock_mv.return_value = pd.Series({'c1': 0.3, 'c2': 0.7})

    s = bt.Strategy('s')

    dts = pd.date_range('2010-01-01', periods=5)
    data = pd.DataFrame(index=dts, columns=['c1', 'c2'], data=100.)

    s.setup(data)
    s.update(dts[4])
    s.temp['selected'] = ['c1', 'c2']

    assert algo(s)
    assert mock_mv.called
    rets = mock_mv.call_args[0][0]
    assert len(rets) == 4
    assert 'c1' in rets
    assert 'c2' in rets

    weights = s.temp['weights']
    assert len(weights) == 2
    assert weights['c1'] == 0.3
    assert weights['c2'] == 0.7 
Example #9
Source File: test_sw.py    From pysat with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_calc_f107a_high_rate(self):
        """ Test the calc_f107a routine with sub-daily data"""
        self.testInst.data = pds.DataFrame({'f107': np.linspace(70, 200,
                                                                3840)},
                                           index=[pysat.datetime(2009, 1, 1)
                                                  + pds.DateOffset(hours=i)
                                                  for i in range(3840)])
        sw_f107.calc_f107a(self.testInst, f107_name='f107', f107a_name='f107a')

        # Assert that new data and metadata exist
        assert 'f107a' in self.testInst.data.columns
        assert 'f107a' in self.testInst.meta.keys()

        # Assert the values are finite and realistic means
        assert np.all(np.isfinite(self.testInst['f107a']))
        assert self.testInst['f107a'].min() > self.testInst['f107'].min()
        assert self.testInst['f107a'].max() < self.testInst['f107'].max()

        # Assert the same mean value is used for a day
        assert len(np.unique(self.testInst['f107a'][:24])) == 1 
Example #10
Source File: test_arithmetic.py    From vnpy_crypto with MIT License 6 votes vote down vote up
def test_dt64_with_offset_array(klass, assert_func):
    # GH#10699
    # array of offsets
    box = Series if klass is Series else pd.Index
    with tm.assert_produces_warning(PerformanceWarning):
        s = klass([Timestamp('2000-1-1'), Timestamp('2000-2-1')])
        result = s + box([pd.offsets.DateOffset(years=1),
                          pd.offsets.MonthEnd()])
        exp = klass([Timestamp('2001-1-1'), Timestamp('2000-2-29')])
        assert_func(result, exp)

        # same offset
        result = s + box([pd.offsets.DateOffset(years=1),
                          pd.offsets.DateOffset(years=1)])
        exp = klass([Timestamp('2001-1-1'), Timestamp('2001-2-1')])
        assert_func(result, exp) 
Example #11
Source File: test_sw.py    From pysat with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def setup(self):
        """Runs before every method to create a clean testing setup"""
        # Load a test instrument with 3hr ap data
        self.testInst = pysat.Instrument()
        self.testInst.data = pds.DataFrame({'3hr_ap': [0, 2, 3, 4, 5, 6, 7, 9,
                                                       12, 15]},
                                           index=[pysat.datetime(2009, 1, 1)
                                                  + pds.DateOffset(hours=3*i)
                                                  for i in range(10)])
        self.testInst.meta = pysat.Meta()
        self.meta_dict = {self.testInst.meta.units_label: '',
                          self.testInst.meta.name_label: 'ap',
                          self.testInst.meta.desc_label:
                          "3-hour ap (equivalent range) index",
                          self.testInst.meta.plot_label: "ap",
                          self.testInst.meta.axis_label: "ap",
                          self.testInst.meta.scale_label: 'linear',
                          self.testInst.meta.min_label: 0,
                          self.testInst.meta.max_label: 400,
                          self.testInst.meta.fill_label: np.nan,
                          self.testInst.meta.notes_label: 'test ap'}
        self.testInst.meta.__setitem__('3hr_ap', self.meta_dict) 
Example #12
Source File: resample.py    From recruit with Apache License 2.0 6 votes vote down vote up
def _apply_loffset(self, result):
        """
        If loffset is set, offset the result index.

        This is NOT an idempotent routine, it will be applied
        exactly once to the result.

        Parameters
        ----------
        result : Series or DataFrame
            the result of resample
        """

        needs_offset = (
            isinstance(self.loffset, (DateOffset, timedelta,
                                      np.timedelta64)) and
            isinstance(result.index, DatetimeIndex) and
            len(result.index) > 0
        )

        if needs_offset:
            result.index = result.index + self.loffset

        self.loffset = None
        return result 
Example #13
Source File: test_algos.py    From bt with MIT License 6 votes vote down vote up
def test_select_has_data():
    algo = algos.SelectHasData(min_count=3, lookback=pd.DateOffset(days=3))

    s = bt.Strategy('s')

    dts = pd.date_range('2010-01-01', periods=10)
    data = pd.DataFrame(index=dts, columns=['c1', 'c2'], data=100.)
    data['c1'].ix[dts[0]] = np.nan
    data['c1'].ix[dts[1]] = np.nan

    s.setup(data)
    s.update(dts[2])

    assert algo(s)
    selected = s.temp['selected']
    assert len(selected) == 1
    assert 'c2' in selected 
Example #14
Source File: test_sw.py    From pysat with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def setup(self):
        """Runs before every method to create a clean testing setup"""
        # Load a test instrument
        self.testInst = pysat.Instrument()
        self.testInst.data = pds.DataFrame({'Kp': np.arange(0, 4, 1.0/3.0),
                                            'ap_nan': np.full(shape=12, \
                                                            fill_value=np.nan),
                                            'ap_inf': np.full(shape=12, \
                                                            fill_value=np.inf)},
                                           index=[pysat.datetime(2009, 1, 1)
                                                  + pds.DateOffset(hours=3*i)
                                                  for i in range(12)])
        self.testInst.meta = pysat.Meta()
        self.testInst.meta.__setitem__('Kp', {self.testInst.meta.fill_label:
                                              np.nan})
        self.testInst.meta.__setitem__('ap_nan', {self.testInst.meta.fill_label:
                                                  np.nan})
        self.testInst.meta.__setitem__('ap_inv', {self.testInst.meta.fill_label:
                                                  np.inf})

        # Load a test Metadata
        self.testMeta = pysat.Meta() 
Example #15
Source File: test_common.py    From vnpy_crypto with MIT License 5 votes vote down vote up
def test_is_offsetlike():
    assert com.is_offsetlike(np.array([pd.DateOffset(month=3),
                                       pd.offsets.Nano()]))
    assert com.is_offsetlike(pd.offsets.MonthEnd())
    assert com.is_offsetlike(pd.Index([pd.DateOffset(second=1)]))

    assert not com.is_offsetlike(pd.Timedelta(1))
    assert not com.is_offsetlike(np.array([1 + 1j, 5]))

    # mixed case
    assert not com.is_offsetlike(np.array([pd.DateOffset(), pd.Timestamp(0)])) 
Example #16
Source File: datetimelike.py    From vnpy_crypto with MIT License 5 votes vote down vote up
def _addsub_offset_array(self, other, op):
        """
        Add or subtract array-like of DateOffset objects

        Parameters
        ----------
        other : Index, np.ndarray
            object-dtype containing pd.DateOffset objects
        op : {operator.add, operator.sub}

        Returns
        -------
        result : same class as self
        """
        assert op in [operator.add, operator.sub]
        if len(other) == 1:
            return op(self, other[0])

        warnings.warn("Adding/subtracting array of DateOffsets to "
                      "{cls} not vectorized"
                      .format(cls=type(self).__name__), PerformanceWarning)

        res_values = op(self.astype('O').values, np.array(other))
        kwargs = {}
        if not is_period_dtype(self):
            kwargs['freq'] = 'infer'
        return self._constructor(res_values, **kwargs) 
Example #17
Source File: datetimelike.py    From vnpy_crypto with MIT License 5 votes vote down vote up
def shift(self, n, freq=None):
        """
        Specialized shift which produces a DatetimeIndex

        Parameters
        ----------
        n : int
            Periods to shift by
        freq : DateOffset or timedelta-like, optional

        Returns
        -------
        shifted : DatetimeIndex
        """
        if freq is not None and freq != self.freq:
            if isinstance(freq, compat.string_types):
                freq = frequencies.to_offset(freq)
            offset = n * freq
            result = self + offset

            if hasattr(self, 'tz'):
                result._tz = self.tz

            return result

        if n == 0:
            # immutable so OK
            return self

        if self.freq is None:
            raise NullFrequencyError("Cannot shift with no freq")

        start = self[0] + n * self.freq
        end = self[-1] + n * self.freq
        attribs = self._get_attributes_dict()
        attribs['start'] = start
        attribs['end'] = end
        return type(self)(**attribs) 
Example #18
Source File: option_class.py    From fftoptionlib with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def set_time_to_maturity(self, time_to_maturity_in_days):
        if self.get_evaluation_date() is None:
            self.set_evaluation_date(Timestamp(datetime.now().strftime('%Y-%m-%d')))
        self.set_maturity_date(self.get_evaluation_date() + DateOffset(days=time_to_maturity_in_days))
        return self 
Example #19
Source File: test_generic.py    From vnpy_crypto with MIT License 5 votes vote down vote up
def test_abc_types(self):
        assert isinstance(pd.Index(['a', 'b', 'c']), gt.ABCIndex)
        assert isinstance(pd.Int64Index([1, 2, 3]), gt.ABCInt64Index)
        assert isinstance(pd.UInt64Index([1, 2, 3]), gt.ABCUInt64Index)
        assert isinstance(pd.Float64Index([1, 2, 3]), gt.ABCFloat64Index)
        assert isinstance(self.multi_index, gt.ABCMultiIndex)
        assert isinstance(self.datetime_index, gt.ABCDatetimeIndex)
        assert isinstance(self.timedelta_index, gt.ABCTimedeltaIndex)
        assert isinstance(self.period_index, gt.ABCPeriodIndex)
        assert isinstance(self.categorical_df.index, gt.ABCCategoricalIndex)
        assert isinstance(pd.Index(['a', 'b', 'c']), gt.ABCIndexClass)
        assert isinstance(pd.Int64Index([1, 2, 3]), gt.ABCIndexClass)
        assert isinstance(pd.Series([1, 2, 3]), gt.ABCSeries)
        assert isinstance(self.df, gt.ABCDataFrame)
        with catch_warnings(record=True):
            assert isinstance(self.df.to_panel(), gt.ABCPanel)
        assert isinstance(self.sparse_series, gt.ABCSparseSeries)
        assert isinstance(self.sparse_array, gt.ABCSparseArray)
        assert isinstance(self.sparse_frame, gt.ABCSparseDataFrame)
        assert isinstance(self.categorical, gt.ABCCategorical)
        assert isinstance(pd.Period('2012', freq='A-DEC'), gt.ABCPeriod)

        assert isinstance(pd.DateOffset(), gt.ABCDateOffset)
        assert isinstance(pd.Period('2012', freq='A-DEC').freq,
                          gt.ABCDateOffset)
        assert not isinstance(pd.Period('2012', freq='A-DEC'),
                              gt.ABCDateOffset)
        assert isinstance(pd.Interval(0, 1.5), gt.ABCInterval)
        assert not isinstance(pd.Period('2012', freq='A-DEC'), gt.ABCInterval) 
Example #20
Source File: test_timeseries.py    From Computable with MIT License 5 votes vote down vote up
def test_series_interpolate_intraday(self):
        # #1698
        index = pd.date_range('1/1/2012', periods=4, freq='12D')
        ts = pd.Series([0, 12, 24, 36], index)
        new_index = index.append(index + pd.DateOffset(days=1)).order()

        exp = ts.reindex(new_index).interpolate(method='time')

        index = pd.date_range('1/1/2012', periods=4, freq='12H')
        ts = pd.Series([0, 12, 24, 36], index)
        new_index = index.append(index + pd.DateOffset(hours=1)).order()
        result = ts.reindex(new_index).interpolate(method='time')

        self.assert_(np.array_equal(result.values, exp.values)) 
Example #21
Source File: test_timeseries.py    From Computable with MIT License 5 votes vote down vote up
def test_union_bug_4564(self):
        from pandas import DateOffset
        left = date_range("2013-01-01", "2013-02-01")
        right = left + DateOffset(minutes=15)

        result = left.union(right)
        exp = DatetimeIndex(sorted(set(list(left)) | set(list(right))))
        self.assert_(result.equals(exp)) 
Example #22
Source File: test_setops.py    From vnpy_crypto with MIT License 5 votes vote down vote up
def test_intersection_bug_1708(self):
        from pandas import DateOffset
        index_1 = date_range('1/1/2012', periods=4, freq='12H')
        index_2 = index_1 + DateOffset(hours=1)

        result = index_1 & index_2
        assert len(result) == 0 
Example #23
Source File: temperature.py    From bigquery-bokeh-dashboard with Apache License 2.0 5 votes vote down vote up
def fetch_data(self, state):
        query = QUERY % {'state': NAMES_TO_CODES[state], 'year': YEAR}
        dataframe = run_query(query, cache_key='temperature-%s' % NAMES_TO_CODES[state])
        dataframe['date'] = pd.to_datetime(dataframe[['year', 'month', 'day']])
        dataframe['date_readable'] = dataframe['date'].apply(lambda x: x.strftime("%Y-%m-%d"))
        dataframe['left'] = dataframe.date - pd.DateOffset(days=0.5)
        dataframe['right'] = dataframe.date + pd.DateOffset(days=0.5)
        dataframe = dataframe.set_index(['date'])
        dataframe.sort_index(inplace=True)
        return dataframe 
Example #24
Source File: test_core.py    From ffn with MIT License 5 votes vote down vote up
def test_get_num_days_required():
    actual = ffn.core.get_num_days_required(pd.DateOffset(months=3),
                                            perc_required=1.)
    assert actual >= 60

    actual = ffn.core.get_num_days_required(pd.DateOffset(months=3),
                                            perc_required=1.,
                                            period='m')
    assert actual >= 3 
Example #25
Source File: core.py    From ffn with MIT License 5 votes vote down vote up
def get_num_days_required(offset, period='d', perc_required=0.90):
    """
    Estimates the number of days required to assume that data is OK.

    Helper function used to determine if there are enough "good" data
    days over a given period.

    Args:
        * offset (DateOffset): Offset (lookback) period.
        * period (str): Period string.
        * perc_required (float): percentage of number of days
            expected required.

    """
    x = pd.to_datetime('2010-01-01')
    delta = x - (x - offset)
    # convert to 'trading days' - rough guestimate
    days = delta.days * 0.69

    if period == 'd':
        req = days * perc_required
    elif period == 'm':
        req = (days / 20) * perc_required
    elif period == 'y':
        req = (days / 252) * perc_required
    else:
        raise NotImplementedError(
            'period not supported. Supported periods are d, m, y')

    return req 
Example #26
Source File: resample.py    From recruit with Apache License 2.0 5 votes vote down vote up
def _get_period_range_edges(first, last, offset, closed='left', base=0):
    """
    Adjust the provided `first` and `last` Periods to the respective Period of
    the given offset that encompasses them.

    Parameters
    ----------
    first : pd.Period
        The beginning Period of the range to be adjusted.
    last : pd.Period
        The ending Period of the range to be adjusted.
    offset : pd.DateOffset
        The dateoffset to which the Periods will be adjusted.
    closed : {'right', 'left'}, default None
        Which side of bin interval is closed.
    base : int, default 0
        The "origin" of the adjusted Periods.

    Returns
    -------
    A tuple of length 2, containing the adjusted pd.Period objects.
    """
    if not all(isinstance(obj, pd.Period) for obj in [first, last]):
        raise TypeError("'first' and 'last' must be instances of type Period")

    # GH 23882
    first = first.to_timestamp()
    last = last.to_timestamp()
    adjust_first = not offset.onOffset(first)
    adjust_last = offset.onOffset(last)

    first, last = _get_timestamp_range_edges(first, last, offset,
                                             closed=closed, base=base)

    first = (first + adjust_first * offset).to_period(offset)
    last = (last - adjust_last * offset).to_period(offset)
    return first, last 
Example #27
Source File: test_generic.py    From recruit with Apache License 2.0 5 votes vote down vote up
def test_abc_types(self):
        assert isinstance(pd.Index(['a', 'b', 'c']), gt.ABCIndex)
        assert isinstance(pd.Int64Index([1, 2, 3]), gt.ABCInt64Index)
        assert isinstance(pd.UInt64Index([1, 2, 3]), gt.ABCUInt64Index)
        assert isinstance(pd.Float64Index([1, 2, 3]), gt.ABCFloat64Index)
        assert isinstance(self.multi_index, gt.ABCMultiIndex)
        assert isinstance(self.datetime_index, gt.ABCDatetimeIndex)
        assert isinstance(self.timedelta_index, gt.ABCTimedeltaIndex)
        assert isinstance(self.period_index, gt.ABCPeriodIndex)
        assert isinstance(self.categorical_df.index, gt.ABCCategoricalIndex)
        assert isinstance(pd.Index(['a', 'b', 'c']), gt.ABCIndexClass)
        assert isinstance(pd.Int64Index([1, 2, 3]), gt.ABCIndexClass)
        assert isinstance(pd.Series([1, 2, 3]), gt.ABCSeries)
        assert isinstance(self.df, gt.ABCDataFrame)
        with catch_warnings(record=True):
            simplefilter('ignore', FutureWarning)
            assert isinstance(self.df.to_panel(), gt.ABCPanel)
        assert isinstance(self.sparse_series, gt.ABCSparseSeries)
        assert isinstance(self.sparse_array, gt.ABCSparseArray)
        assert isinstance(self.sparse_frame, gt.ABCSparseDataFrame)
        assert isinstance(self.categorical, gt.ABCCategorical)
        assert isinstance(pd.Period('2012', freq='A-DEC'), gt.ABCPeriod)

        assert isinstance(pd.DateOffset(), gt.ABCDateOffset)
        assert isinstance(pd.Period('2012', freq='A-DEC').freq,
                          gt.ABCDateOffset)
        assert not isinstance(pd.Period('2012', freq='A-DEC'),
                              gt.ABCDateOffset)
        assert isinstance(pd.Interval(0, 1.5), gt.ABCInterval)
        assert not isinstance(pd.Period('2012', freq='A-DEC'), gt.ABCInterval)

        assert isinstance(self.datetime_array, gt.ABCDatetimeArray)
        assert not isinstance(self.datetime_index, gt.ABCDatetimeArray)

        assert isinstance(self.timedelta_array, gt.ABCTimedeltaArray)
        assert not isinstance(self.timedelta_index, gt.ABCTimedeltaArray) 
Example #28
Source File: test_datetime64.py    From recruit with Apache License 2.0 5 votes vote down vote up
def test_shift_months(years, months):
    dti = DatetimeIndex([Timestamp('2000-01-05 00:15:00'),
                         Timestamp('2000-01-31 00:23:00'),
                         Timestamp('2000-01-01'),
                         Timestamp('2000-02-29'),
                         Timestamp('2000-12-31')])
    actual = DatetimeIndex(shift_months(dti.asi8, years * 12 + months))

    raw = [x + pd.offsets.DateOffset(years=years, months=months)
           for x in dti]
    expected = DatetimeIndex(raw)
    tm.assert_index_equal(actual, expected) 
Example #29
Source File: test_datetime64.py    From recruit with Apache License 2.0 5 votes vote down vote up
def test_dt64arr_add_mixed_offset_array(self, box_with_array):
        # GH#10699
        # array of offsets
        s = DatetimeIndex([Timestamp('2000-1-1'), Timestamp('2000-2-1')])
        s = tm.box_expected(s, box_with_array)

        warn = None if box_with_array is pd.DataFrame else PerformanceWarning
        with tm.assert_produces_warning(warn,
                                        clear=[pd.core.arrays.datetimelike]):
            other = pd.Index([pd.offsets.DateOffset(years=1),
                              pd.offsets.MonthEnd()])
            other = tm.box_expected(other, box_with_array)
            result = s + other
            exp = DatetimeIndex([Timestamp('2001-1-1'),
                                 Timestamp('2000-2-29')])
            exp = tm.box_expected(exp, box_with_array)
            tm.assert_equal(result, exp)

            # same offset
            other = pd.Index([pd.offsets.DateOffset(years=1),
                              pd.offsets.DateOffset(years=1)])
            other = tm.box_expected(other, box_with_array)
            result = s + other
            exp = DatetimeIndex([Timestamp('2001-1-1'),
                                 Timestamp('2001-2-1')])
            exp = tm.box_expected(exp, box_with_array)
            tm.assert_equal(result, exp)

    # TODO: overlap with test_dt64arr_add_mixed_offset_array? 
Example #30
Source File: test_missing.py    From recruit with Apache License 2.0 5 votes vote down vote up
def test_series_interpolate_intraday(self):
        # #1698
        index = pd.date_range('1/1/2012', periods=4, freq='12D')
        ts = pd.Series([0, 12, 24, 36], index)
        new_index = index.append(index + pd.DateOffset(days=1)).sort_values()

        exp = ts.reindex(new_index).interpolate(method='time')

        index = pd.date_range('1/1/2012', periods=4, freq='12H')
        ts = pd.Series([0, 12, 24, 36], index)
        new_index = index.append(index + pd.DateOffset(hours=1)).sort_values()
        result = ts.reindex(new_index).interpolate(method='time')

        tm.assert_numpy_array_equal(result.values, exp.values)