Python pandas.period_range() Examples

The following are 30 code examples of pandas.period_range(). 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_partial_slicing.py    From recruit with Apache License 2.0 6 votes vote down vote up
def test_slice_with_negative_step(self):
        ts = Series(np.arange(20),
                    period_range('2014-01', periods=20, freq='M'))
        SLC = pd.IndexSlice

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

        assert_slices_equivalent(SLC[Period('2014-10')::-1], SLC[9::-1])
        assert_slices_equivalent(SLC['2014-10'::-1], SLC[9::-1])

        assert_slices_equivalent(SLC[:Period('2014-10'):-1], SLC[:8:-1])
        assert_slices_equivalent(SLC[:'2014-10':-1], SLC[:8:-1])

        assert_slices_equivalent(SLC['2015-02':'2014-10':-1], SLC[13:8:-1])
        assert_slices_equivalent(SLC[Period('2015-02'):Period('2014-10'):-1],
                                 SLC[13:8:-1])
        assert_slices_equivalent(SLC['2015-02':Period('2014-10'):-1],
                                 SLC[13:8:-1])
        assert_slices_equivalent(SLC[Period('2015-02'):'2014-10':-1],
                                 SLC[13:8:-1])

        assert_slices_equivalent(SLC['2014-10':'2015-02':-1], SLC[:0]) 
Example #2
Source File: test_period.py    From recruit with Apache License 2.0 6 votes vote down vote up
def test_difference_freq(self, sort):
        # GH14323: difference of Period MUST preserve frequency
        # but the ability to union results must be preserved

        index = period_range("20160920", "20160925", freq="D")

        other = period_range("20160921", "20160924", freq="D")
        expected = PeriodIndex(["20160920", "20160925"], freq='D')
        idx_diff = index.difference(other, sort)
        tm.assert_index_equal(idx_diff, expected)
        tm.assert_attr_equal('freq', idx_diff, expected)

        other = period_range("20160922", "20160925", freq="D")
        idx_diff = index.difference(other, sort)
        expected = PeriodIndex(["20160920", "20160921"], freq='D')
        tm.assert_index_equal(idx_diff, expected)
        tm.assert_attr_equal('freq', idx_diff, expected) 
Example #3
Source File: test_indexing.py    From recruit with Apache License 2.0 6 votes vote down vote up
def test_where_other(self):
        i = period_range('20130101', periods=5, freq='D')
        for arr in [np.nan, pd.NaT]:
            result = i.where(notna(i), other=np.nan)
            expected = i
            tm.assert_index_equal(result, expected)

        i2 = i.copy()
        i2 = pd.PeriodIndex([pd.NaT, pd.NaT] + i[2:].tolist(),
                            freq='D')
        result = i.where(notna(i2), i2)
        tm.assert_index_equal(result, i2)

        i2 = i.copy()
        i2 = pd.PeriodIndex([pd.NaT, pd.NaT] + i[2:].tolist(),
                            freq='D')
        result = i.where(notna(i2), i2.values)
        tm.assert_index_equal(result, i2) 
Example #4
Source File: test_indexing.py    From recruit with Apache License 2.0 6 votes vote down vote up
def test_getitem_seconds(self):
        # GH#6716
        didx = pd.date_range(start='2013/01/01 09:00:00', freq='S',
                             periods=4000)
        pidx = period_range(start='2013/01/01 09:00:00', freq='S',
                            periods=4000)

        for idx in [didx, pidx]:
            # getitem against index should raise ValueError
            values = ['2014', '2013/02', '2013/01/02', '2013/02/01 9H',
                      '2013/02/01 09:00']
            for v in values:
                # GH7116
                # these show deprecations as we are trying
                # to slice with non-integer indexers
                # with pytest.raises(IndexError):
                #    idx[v]
                continue

            s = Series(np.random.rand(len(idx)), index=idx)
            tm.assert_series_equal(s['2013/01/01 10:00'], s[3600:3660])
            tm.assert_series_equal(s['2013/01/01 9H'], s[:3600])
            for d in ['2013/01/01', '2013/01', '2013']:
                tm.assert_series_equal(s[d], s) 
Example #5
Source File: test_period.py    From recruit with Apache License 2.0 6 votes vote down vote up
def test_is_(self):
        create_index = lambda: period_range(freq='A', start='1/1/2001',
                                            end='12/1/2009')
        index = create_index()
        assert index.is_(index)
        assert not index.is_(create_index())
        assert index.is_(index.view())
        assert index.is_(index.view().view().view().view().view())
        assert index.view().is_(index)
        ind2 = index.view()
        index.name = "Apple"
        assert ind2.is_(index)
        assert not index.is_(index[:])
        assert not index.is_(index.asfreq('M'))
        assert not index.is_(index.asfreq('A'))

        assert not index.is_(index - 2)
        assert not index.is_(index - 0) 
Example #6
Source File: test_tools.py    From recruit with Apache License 2.0 6 votes vote down vote up
def test_dti_to_period(self):
        dti = pd.date_range(start='1/1/2005', end='12/1/2005', freq='M')
        pi1 = dti.to_period()
        pi2 = dti.to_period(freq='D')
        pi3 = dti.to_period(freq='3D')

        assert pi1[0] == Period('Jan 2005', freq='M')
        assert pi2[0] == Period('1/31/2005', freq='D')
        assert pi3[0] == Period('1/31/2005', freq='3D')

        assert pi1[-1] == Period('Nov 2005', freq='M')
        assert pi2[-1] == Period('11/30/2005', freq='D')
        assert pi3[-1], Period('11/30/2005', freq='3D')

        tm.assert_index_equal(pi1, period_range('1/1/2005', '11/1/2005',
                                                freq='M'))
        tm.assert_index_equal(pi2, period_range('1/1/2005', '11/1/2005',
                                                freq='M').asfreq('D'))
        tm.assert_index_equal(pi3, period_range('1/1/2005', '11/1/2005',
                                                freq='M').asfreq('3D')) 
Example #7
Source File: test_setops.py    From recruit with Apache License 2.0 6 votes vote down vote up
def test_intersection(self, sort):
        index = period_range('1/1/2000', '1/20/2000', freq='D')

        result = index[:-5].intersection(index[10:], sort=sort)
        tm.assert_index_equal(result, index[10:-5])

        # not in order
        left = _permute(index[:-5])
        right = _permute(index[10:])
        result = left.intersection(right, sort=sort)
        if sort is None:
            tm.assert_index_equal(result, index[10:-5])
        assert tm.equalContents(result, index[10:-5])

        # raise if different frequencies
        index = period_range('1/1/2000', '1/20/2000', freq='D')
        index2 = period_range('1/1/2000', '1/20/2000', freq='W-WED')
        with pytest.raises(period.IncompatibleFrequency):
            index.intersection(index2, sort=sort)

        index3 = period_range('1/1/2000', '1/20/2000', freq='2D')
        with pytest.raises(period.IncompatibleFrequency):
            index.intersection(index3, sort=sort) 
Example #8
Source File: test_period.py    From recruit with Apache License 2.0 6 votes vote down vote up
def test_pindex_multiples(self):
        with tm.assert_produces_warning(FutureWarning):
            pi = PeriodIndex(start='1/1/11', end='12/31/11', freq='2M')
        expected = PeriodIndex(['2011-01', '2011-03', '2011-05', '2011-07',
                                '2011-09', '2011-11'], freq='2M')
        tm.assert_index_equal(pi, expected)
        assert pi.freq == offsets.MonthEnd(2)
        assert pi.freqstr == '2M'

        pi = period_range(start='1/1/11', end='12/31/11', freq='2M')
        tm.assert_index_equal(pi, expected)
        assert pi.freq == offsets.MonthEnd(2)
        assert pi.freqstr == '2M'

        pi = period_range(start='1/1/11', periods=6, freq='2M')
        tm.assert_index_equal(pi, expected)
        assert pi.freq == offsets.MonthEnd(2)
        assert pi.freqstr == '2M' 
Example #9
Source File: test_setops.py    From recruit with Apache License 2.0 6 votes vote down vote up
def test_union_misc(self, sort):
        index = period_range('1/1/2000', '1/20/2000', freq='D')

        result = index[:-5].union(index[10:], sort=sort)
        tm.assert_index_equal(result, index)

        # not in order
        result = _permute(index[:-5]).union(_permute(index[10:]), sort=sort)
        if sort is None:
            tm.assert_index_equal(result, index)
        assert tm.equalContents(result, index)

        # raise if different frequencies
        index = period_range('1/1/2000', '1/20/2000', freq='D')
        index2 = period_range('1/1/2000', '1/20/2000', freq='W-WED')
        with pytest.raises(period.IncompatibleFrequency):
            index.union(index2, sort=sort)

        msg = 'can only call with other PeriodIndex-ed objects'
        with pytest.raises(ValueError, match=msg):
            index.join(index.to_timestamp())

        index3 = period_range('1/1/2000', '1/20/2000', freq='2D')
        with pytest.raises(period.IncompatibleFrequency):
            index.join(index3) 
Example #10
Source File: test_tools.py    From recruit with Apache License 2.0 6 votes vote down vote up
def test_to_timestamp_pi_combined(self):
        idx = period_range(start='2011', periods=2, freq='1D1H', name='idx')

        result = idx.to_timestamp()
        expected = DatetimeIndex(['2011-01-01 00:00', '2011-01-02 01:00'],
                                 name='idx')
        tm.assert_index_equal(result, expected)

        result = idx.to_timestamp(how='E')
        expected = DatetimeIndex(['2011-01-02 00:59:59',
                                  '2011-01-03 01:59:59'],
                                 name='idx')
        expected = expected + Timedelta(1, 's') - Timedelta(1, 'ns')
        tm.assert_index_equal(result, expected)

        result = idx.to_timestamp(how='E', freq='H')
        expected = DatetimeIndex(['2011-01-02 00:00', '2011-01-03 01:00'],
                                 name='idx')
        expected = expected + Timedelta(1, 'h') - Timedelta(1, 'ns')
        tm.assert_index_equal(result, expected) 
Example #11
Source File: test_partial_slicing.py    From recruit with Apache License 2.0 6 votes vote down vote up
def test_range_slice_seconds(self):
        # GH#6716
        didx = pd.date_range(start='2013/01/01 09:00:00', freq='S',
                             periods=4000)
        pidx = period_range(start='2013/01/01 09:00:00', freq='S',
                            periods=4000)

        for idx in [didx, pidx]:
            # slices against index should raise IndexError
            values = ['2014', '2013/02', '2013/01/02', '2013/02/01 9H',
                      '2013/02/01 09:00']
            for v in values:
                with pytest.raises(TypeError):
                    idx[v:]

            s = Series(np.random.rand(len(idx)), index=idx)

            tm.assert_series_equal(s['2013/01/01 09:05':'2013/01/01 09:10'],
                                   s[300:660])
            tm.assert_series_equal(s['2013/01/01 10:00':'2013/01/01 10:05'],
                                   s[3600:3960])
            tm.assert_series_equal(s['2013/01/01 10H':], s[3600:])
            tm.assert_series_equal(s[:'2013/01/01 09:30'], s[:1860])
            for d in ['2013/01/01', '2013/01', '2013']:
                tm.assert_series_equal(s[d:], s) 
Example #12
Source File: test_astype.py    From recruit with Apache License 2.0 6 votes vote down vote up
def test_astype_conversion(self):
        # GH#13149, GH#13209
        idx = PeriodIndex(['2016-05-16', 'NaT', NaT, np.NaN], freq='D')

        result = idx.astype(object)
        expected = Index([Period('2016-05-16', freq='D')] +
                         [Period(NaT, freq='D')] * 3, dtype='object')
        tm.assert_index_equal(result, expected)

        result = idx.astype(np.int64)
        expected = Int64Index([16937] + [-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)

        idx = period_range('1990', '2009', freq='A')
        result = idx.astype('i8')
        tm.assert_index_equal(result, Index(idx.asi8))
        tm.assert_numpy_array_equal(result.values, idx.asi8) 
Example #13
Source File: test_partial_slicing.py    From recruit with Apache License 2.0 6 votes vote down vote up
def test_range_slice_day(self):
        # GH#6716
        didx = pd.date_range(start='2013/01/01', freq='D', periods=400)
        pidx = period_range(start='2013/01/01', freq='D', periods=400)

        for idx in [didx, pidx]:
            # slices against index should raise IndexError
            values = ['2014', '2013/02', '2013/01/02', '2013/02/01 9H',
                      '2013/02/01 09:00']
            for v in values:
                with pytest.raises(TypeError):
                    idx[v:]

            s = Series(np.random.rand(len(idx)), index=idx)

            tm.assert_series_equal(s['2013/01/02':], s[1:])
            tm.assert_series_equal(s['2013/01/02':'2013/01/05'], s[1:5])
            tm.assert_series_equal(s['2013/02':], s[31:])
            tm.assert_series_equal(s['2014':], s[365:])

            invalid = ['2013/02/01 9H', '2013/02/01 09:00']
            for v in invalid:
                with pytest.raises(TypeError):
                    idx[v:] 
Example #14
Source File: test_partial_slicing.py    From recruit with Apache License 2.0 6 votes vote down vote up
def test_range_slice_outofbounds(self):
        # GH#5407
        didx = pd.date_range(start='2013/10/01', freq='D', periods=10)
        pidx = period_range(start='2013/10/01', freq='D', periods=10)

        for idx in [didx, pidx]:
            df = DataFrame(dict(units=[100 + i for i in range(10)]), index=idx)
            empty = DataFrame(index=idx.__class__([], freq='D'),
                              columns=['units'])
            empty['units'] = empty['units'].astype('int64')

            tm.assert_frame_equal(df['2013/09/01':'2013/09/30'], empty)
            tm.assert_frame_equal(df['2013/09/30':'2013/10/02'], df.iloc[:2])
            tm.assert_frame_equal(df['2013/10/01':'2013/10/02'], df.iloc[:2])
            tm.assert_frame_equal(df['2013/10/02':'2013/09/30'], empty)
            tm.assert_frame_equal(df['2013/10/15':'2013/10/17'], empty)
            tm.assert_frame_equal(df['2013-06':'2013-09'], empty)
            tm.assert_frame_equal(df['2013-11':'2013-12'], empty) 
Example #15
Source File: test_ops.py    From recruit with Apache License 2.0 6 votes vote down vote up
def test_drop_duplicates(self):
        # to check Index/Series compat
        base = pd.period_range('2011-01-01', '2011-01-31', freq='D',
                               name='idx')
        idx = base.append(base[:5])

        res = idx.drop_duplicates()
        tm.assert_index_equal(res, base)
        res = Series(idx).drop_duplicates()
        tm.assert_series_equal(res, Series(base))

        res = idx.drop_duplicates(keep='last')
        exp = base[5:].append(base[:5])
        tm.assert_index_equal(res, exp)
        res = Series(idx).drop_duplicates(keep='last')
        tm.assert_series_equal(res, Series(exp, index=np.arange(5, 36)))

        res = idx.drop_duplicates(keep=False)
        tm.assert_index_equal(res, base[5:])
        res = Series(idx).drop_duplicates(keep=False)
        tm.assert_series_equal(res, Series(base[5:], index=np.arange(5, 31))) 
Example #16
Source File: test_arithmetic.py    From recruit with Apache License 2.0 5 votes vote down vote up
def test_shift_gh8083(self):
        # test shift for PeriodIndex
        # GH#8083
        drange = pd.period_range('20130101', periods=5, freq='D')
        result = drange.shift(1)
        expected = PeriodIndex(['2013-01-02', '2013-01-03', '2013-01-04',
                                '2013-01-05', '2013-01-06'], freq='D')
        tm.assert_index_equal(result, expected) 
Example #17
Source File: test_period.py    From recruit with Apache License 2.0 5 votes vote down vote up
def test_convert_array_of_periods(self):
        rng = period_range('1/1/2000', periods=20, freq='D')
        periods = list(rng)

        result = pd.Index(periods)
        assert isinstance(result, PeriodIndex) 
Example #18
Source File: test_ops.py    From recruit with Apache License 2.0 5 votes vote down vote up
def test_value_counts_unique(self):
        # GH 7735
        idx = pd.period_range('2011-01-01 09:00', freq='H', periods=10)
        # create repeated values, 'n'th element is repeated by n+1 times
        idx = PeriodIndex(np.repeat(idx._values, range(1, len(idx) + 1)),
                          freq='H')

        exp_idx = PeriodIndex(['2011-01-01 18:00', '2011-01-01 17:00',
                               '2011-01-01 16:00', '2011-01-01 15:00',
                               '2011-01-01 14:00', '2011-01-01 13:00',
                               '2011-01-01 12:00', '2011-01-01 11:00',
                               '2011-01-01 10:00',
                               '2011-01-01 09:00'], freq='H')
        expected = Series(range(10, 0, -1), index=exp_idx, dtype='int64')

        for obj in [idx, Series(idx)]:
            tm.assert_series_equal(obj.value_counts(), expected)

        expected = pd.period_range('2011-01-01 09:00', freq='H',
                                   periods=10)
        tm.assert_index_equal(idx.unique(), expected)

        idx = PeriodIndex(['2013-01-01 09:00', '2013-01-01 09:00',
                           '2013-01-01 09:00', '2013-01-01 08:00',
                           '2013-01-01 08:00', NaT], freq='H')

        exp_idx = PeriodIndex(['2013-01-01 09:00', '2013-01-01 08:00'],
                              freq='H')
        expected = Series([3, 2], index=exp_idx)

        for obj in [idx, Series(idx)]:
            tm.assert_series_equal(obj.value_counts(), expected)

        exp_idx = PeriodIndex(['2013-01-01 09:00', '2013-01-01 08:00',
                               NaT], freq='H')
        expected = Series([3, 2, 1], index=exp_idx)

        for obj in [idx, Series(idx)]:
            tm.assert_series_equal(obj.value_counts(dropna=False), expected)

        tm.assert_index_equal(idx.unique(), exp_idx) 
Example #19
Source File: test_period.py    From recruit with Apache License 2.0 5 votes vote down vote up
def test_insert(self):
        # GH 18295 (test missing)
        expected = PeriodIndex(
            ['2017Q1', pd.NaT, '2017Q2', '2017Q3', '2017Q4'], freq='Q')
        for na in (np.nan, pd.NaT, None):
            result = period_range('2017Q1', periods=4, freq='Q').insert(1, na)
            tm.assert_index_equal(result, expected) 
Example #20
Source File: test_ops.py    From recruit with Apache License 2.0 5 votes vote down vote up
def test_resolution(self):
        for freq, expected in zip(['A', 'Q', 'M', 'D', 'H',
                                   'T', 'S', 'L', 'U'],
                                  ['day', 'day', 'day', 'day',
                                   'hour', 'minute', 'second',
                                   'millisecond', 'microsecond']):

            idx = pd.period_range(start='2013-04-01', periods=30, freq=freq)
            assert idx.resolution == expected 
Example #21
Source File: test_period.py    From recruit with Apache License 2.0 5 votes vote down vote up
def test_pickle_freq(self):
        # GH2891
        prng = period_range('1/1/2011', '1/1/2012', freq='M')
        new_prng = tm.round_trip_pickle(prng)
        assert new_prng.freq == offsets.MonthEnd()
        assert new_prng.freqstr == 'M' 
Example #22
Source File: test_period.py    From recruit with Apache License 2.0 5 votes vote down vote up
def test_shallow_copy_changing_freq_raises(self):
        pi = period_range("2018-01-01", periods=3, freq="2D")
        with pytest.raises(IncompatibleFrequency, match="are different"):
            pi._shallow_copy(pi, freq="H") 
Example #23
Source File: test_partial_slicing.py    From recruit with Apache License 2.0 5 votes vote down vote up
def test_slice_with_zero_step_raises(self):
        ts = Series(np.arange(20),
                    period_range('2014-01', periods=20, freq='M'))
        with pytest.raises(ValueError, match='slice step cannot be zero'):
            ts[::0]
        with pytest.raises(ValueError, match='slice step cannot be zero'):
            ts.loc[::0]
        with pytest.raises(ValueError, match='slice step cannot be zero'):
            ts.loc[::0] 
Example #24
Source File: test_period.py    From recruit with Apache License 2.0 5 votes vote down vote up
def setup_method(self, method):
        self.indices = dict(index=tm.makePeriodIndex(10),
                            index_dec=period_range('20130101', periods=10,
                                                   freq='D')[::-1])
        self.setup_indices() 
Example #25
Source File: test_arithmetic.py    From recruit with Apache License 2.0 5 votes vote down vote up
def test_shift_periods(self):
        # GH #22458 : argument 'n' was deprecated in favor of 'periods'
        idx = period_range(freq='A', start='1/1/2001', end='12/1/2009')
        tm.assert_index_equal(idx.shift(periods=0), idx)
        tm.assert_index_equal(idx.shift(0), idx)
        with tm.assert_produces_warning(FutureWarning,
                                        check_stacklevel=True):
            tm.assert_index_equal(idx.shift(n=0), idx) 
Example #26
Source File: test_partial_slicing.py    From recruit with Apache License 2.0 5 votes vote down vote up
def test_slice_keep_name(self):
        idx = period_range('20010101', periods=10, freq='D', name='bob')
        assert idx.name == idx[1:].name 
Example #27
Source File: test_arithmetic.py    From recruit with Apache License 2.0 5 votes vote down vote up
def test_shift(self):
        pi1 = period_range(freq='A', start='1/1/2001', end='12/1/2009')
        pi2 = period_range(freq='A', start='1/1/2002', end='12/1/2010')

        tm.assert_index_equal(pi1.shift(0), pi1)

        assert len(pi1) == len(pi2)
        tm.assert_index_equal(pi1.shift(1), pi2)

        pi1 = period_range(freq='A', start='1/1/2001', end='12/1/2009')
        pi2 = period_range(freq='A', start='1/1/2000', end='12/1/2008')
        assert len(pi1) == len(pi2)
        tm.assert_index_equal(pi1.shift(-1), pi2)

        pi1 = period_range(freq='M', start='1/1/2001', end='12/1/2009')
        pi2 = period_range(freq='M', start='2/1/2001', end='1/1/2010')
        assert len(pi1) == len(pi2)
        tm.assert_index_equal(pi1.shift(1), pi2)

        pi1 = period_range(freq='M', start='1/1/2001', end='12/1/2009')
        pi2 = period_range(freq='M', start='12/1/2000', end='11/1/2009')
        assert len(pi1) == len(pi2)
        tm.assert_index_equal(pi1.shift(-1), pi2)

        pi1 = period_range(freq='D', start='1/1/2001', end='12/1/2009')
        pi2 = period_range(freq='D', start='1/2/2001', end='12/2/2009')
        assert len(pi1) == len(pi2)
        tm.assert_index_equal(pi1.shift(1), pi2)

        pi1 = period_range(freq='D', start='1/1/2001', end='12/1/2009')
        pi2 = period_range(freq='D', start='12/31/2000', end='11/30/2009')
        assert len(pi1) == len(pi2)
        tm.assert_index_equal(pi1.shift(-1), pi2) 
Example #28
Source File: test_period_range.py    From recruit with Apache License 2.0 5 votes vote down vote up
def test_errors(self):
        # not enough params
        msg = ('Of the three parameters: start, end, and periods, '
               'exactly two must be specified')
        with pytest.raises(ValueError, match=msg):
            period_range(start='2017Q1')

        with pytest.raises(ValueError, match=msg):
            period_range(end='2017Q1')

        with pytest.raises(ValueError, match=msg):
            period_range(periods=5)

        with pytest.raises(ValueError, match=msg):
            period_range()

        # too many params
        with pytest.raises(ValueError, match=msg):
            period_range(start='2017Q1', end='2018Q1', periods=8, freq='Q')

        # start/end NaT
        msg = 'start and end must not be NaT'
        with pytest.raises(ValueError, match=msg):
            period_range(start=NaT, end='2018Q1')

        with pytest.raises(ValueError, match=msg):
            period_range(start='2017Q1', end=NaT)

        # invalid periods param
        msg = 'periods must be a number, got foo'
        with pytest.raises(TypeError, match=msg):
            period_range(start='2017Q1', periods='foo') 
Example #29
Source File: test_period_range.py    From recruit with Apache License 2.0 5 votes vote down vote up
def test_construction_from_string(self, freq):
        # non-empty
        expected = date_range(start='2017-01-01', periods=5,
                              freq=freq, name='foo').to_period()
        start, end = str(expected[0]), str(expected[-1])

        result = period_range(start=start, end=end, freq=freq, name='foo')
        tm.assert_index_equal(result, expected)

        result = period_range(start=start, periods=5, freq=freq, name='foo')
        tm.assert_index_equal(result, expected)

        result = period_range(end=end, periods=5, freq=freq, name='foo')
        tm.assert_index_equal(result, expected)

        # empty
        expected = PeriodIndex([], freq=freq, name='foo')

        result = period_range(start=start, periods=0, freq=freq, name='foo')
        tm.assert_index_equal(result, expected)

        result = period_range(end=end, periods=0, freq=freq, name='foo')
        tm.assert_index_equal(result, expected)

        result = period_range(start=end, end=start, freq=freq, name='foo')
        tm.assert_index_equal(result, expected) 
Example #30
Source File: test_asfreq.py    From recruit with Apache License 2.0 5 votes vote down vote up
def test_asfreq_ts(self):
        index = period_range(freq='A', start='1/1/2001', end='12/31/2010')
        ts = Series(np.random.randn(len(index)), index=index)
        df = DataFrame(np.random.randn(len(index), 3), index=index)

        result = ts.asfreq('D', how='end')
        df_result = df.asfreq('D', how='end')
        exp_index = index.asfreq('D', how='end')
        assert len(result) == len(ts)
        tm.assert_index_equal(result.index, exp_index)
        tm.assert_index_equal(df_result.index, exp_index)

        result = ts.asfreq('D', how='start')
        assert len(result) == len(ts)
        tm.assert_index_equal(result.index, index.asfreq('D', how='start'))