Beispiel #1
0
    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')
        tm.assert_series_equal(idx.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', pd.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)
        tm.assert_series_equal(idx.value_counts(), expected)

        exp_idx = PeriodIndex(['2013-01-01 09:00', '2013-01-01 08:00', pd.NaT], freq='H')
        expected = Series([3, 2, 1], index=exp_idx)
        tm.assert_series_equal(idx.value_counts(dropna=False), expected)

        tm.assert_index_equal(idx.unique(), exp_idx)
Beispiel #2
0
    def test_series(self):

        # GH6407
        # inferring series

        # invalid type of Series
        for s in [Series(np.arange(10)), Series(np.arange(10.))]:
            self.assertRaises(TypeError, lambda: frequencies.infer_freq(s))

        # a non-convertible string
        self.assertRaises(ValueError,
                          lambda: frequencies.infer_freq(
                              Series(['foo', 'bar'])))

        # cannot infer on PeriodIndex
        for freq in [None, 'L']:
            s = Series(period_range('2013', periods=10, freq=freq))
            self.assertRaises(TypeError, lambda: frequencies.infer_freq(s))
        for freq in ['Y']:

            msg = frequencies._INVALID_FREQ_ERROR
            with tm.assertRaisesRegexp(ValueError, msg):
                s = Series(period_range('2013', periods=10, freq=freq))
            self.assertRaises(TypeError, lambda: frequencies.infer_freq(s))

        # DateTimeIndex
        for freq in ['M', 'L', 'S']:
            s = Series(date_range('20130101', periods=10, freq=freq))
            inferred = frequencies.infer_freq(s)
            self.assertEqual(inferred, freq)

        s = Series(date_range('20130101', '20130110'))
        inferred = frequencies.infer_freq(s)
        self.assertEqual(inferred, 'D')
Beispiel #3
0
    def test_constructor_field_arrays(self):
        # GH #1264

        years = np.arange(1990, 2010).repeat(4)[2:-2]
        quarters = np.tile(np.arange(1, 5), 20)[2:-2]

        index = PeriodIndex(year=years, quarter=quarters, freq='Q-DEC')
        expected = period_range('1990Q3', '2009Q2', freq='Q-DEC')
        tm.assert_index_equal(index, expected)

        index2 = PeriodIndex(year=years, quarter=quarters, freq='2Q-DEC')
        tm.assert_numpy_array_equal(index.asi8, index2.asi8)

        index = PeriodIndex(year=years, quarter=quarters)
        tm.assert_index_equal(index, expected)

        years = [2007, 2007, 2007]
        months = [1, 2]

        msg = "Mismatched Period array lengths"
        with pytest.raises(ValueError, match=msg):
            PeriodIndex(year=years, month=months, freq='M')
        with pytest.raises(ValueError, match=msg):
            PeriodIndex(year=years, month=months, freq='2M')

        msg = "Can either instantiate from fields or endpoints, but not both"
        with pytest.raises(ValueError, match=msg):
            PeriodIndex(year=years, month=months, freq='M',
                        start=Period('2007-01', freq='M'))

        years = [2007, 2007, 2007]
        months = [1, 2, 3]
        idx = PeriodIndex(year=years, month=months, freq='M')
        exp = period_range('2007-01', periods=3, freq='M')
        tm.assert_index_equal(idx, exp)
def slide9():
    p = pd.Period(2007, freq='A-DEC')
    print p
    print 'after 5 years'
    print p + 5
    print 'before 2 yeaars'
    print p - 2

    print pd.Period('2014', freq='A-DEC') - p

    rng = pd.period_range('1/1/2000', '6/30/2000', freq='M')
    print rng
    print Series(np.random.randn(6), index=rng)
    values = ['2001Q3', '2002Q2', '2003Q1']
    index = pd.PeriodIndex(values, freq='Q-DEC')
    print index

    print '2007, A-DEC'
    p = pd.Period('2007', freq='A-DEC')
    print p.asfreq('M', 'start')
    print p.asfreq('M', 'end')
    print '2007, A-JUN'
    p = pd.Period('2007', freq='A-JUN')
    print p.asfreq('M', 'start')
    print p.asfreq('M', 'end')

    p = pd.Period('2007-08', 'M')
    print p.asfreq('A-JUN')

    rng = pd.period_range('2006', '2009', freq='A-DEC')
    ts = Series(np.random.randn(len(rng)), index=rng)
    print ts
Beispiel #5
0
    def test_categorical_repr_period_ordered(self):
        idx = period_range('2011-01-01 09:00', freq='H', periods=5)
        c = Categorical(idx, ordered=True)
        exp = """[2011-01-01 09:00, 2011-01-01 10:00, 2011-01-01 11:00, 2011-01-01 12:00, 2011-01-01 13:00]
Categories (5, period[H]): [2011-01-01 09:00 < 2011-01-01 10:00 < 2011-01-01 11:00 < 2011-01-01 12:00 <
                            2011-01-01 13:00]"""  # noqa

        assert repr(c) == exp

        c = Categorical(idx.append(idx), categories=idx, ordered=True)
        exp = """[2011-01-01 09:00, 2011-01-01 10:00, 2011-01-01 11:00, 2011-01-01 12:00, 2011-01-01 13:00, 2011-01-01 09:00, 2011-01-01 10:00, 2011-01-01 11:00, 2011-01-01 12:00, 2011-01-01 13:00]
Categories (5, period[H]): [2011-01-01 09:00 < 2011-01-01 10:00 < 2011-01-01 11:00 < 2011-01-01 12:00 <
                            2011-01-01 13:00]"""  # noqa

        assert repr(c) == exp

        idx = period_range('2011-01', freq='M', periods=5)
        c = Categorical(idx, ordered=True)
        exp = """[2011-01, 2011-02, 2011-03, 2011-04, 2011-05]
Categories (5, period[M]): [2011-01 < 2011-02 < 2011-03 < 2011-04 < 2011-05]"""

        assert repr(c) == exp

        c = Categorical(idx.append(idx), categories=idx, ordered=True)
        exp = """[2011-01, 2011-02, 2011-03, 2011-04, 2011-05, 2011-01, 2011-02, 2011-03, 2011-04, 2011-05]
Categories (5, period[M]): [2011-01 < 2011-02 < 2011-03 < 2011-04 < 2011-05]"""  # noqa

        assert repr(c) == exp
    def row_process(s):
        row = [s['key']]
        
        for c in dataset['dimension_keys']:
            if c in s['dimensions']:
                row.append(s['dimensions'][c])
            else:
                row.append('')        
        
        p_start_date = pandas.Period(ordinal=s['start_date'], freq=freq)        
        p_end_date = pandas.Period(ordinal=s['end_date'], freq=freq)
        
        """
        pDmin : pandas.Period() la plus ancienne
        p_start_date-1 : périod en cours -1
            >>> p_start_date -1
            Period('1994', 'A-DEC')
            Bug: ne renvoi rien si
                p_start_date -1 devient identique à pDmin
        """

        # Les None sont pour les périodes qui n'ont pas de valeur correspondantes
        _row = [None for d in pandas.period_range(pDmin, p_start_date-1, freq=freq)]
        row.extend(_row)
        
        _row = [val["value"] for val in s['values']]
        row.extend(_row)

        _row = [None for d in pandas.period_range(p_end_date+1, pDmax, freq=freq)]
        row.extend(_row)
        
        return row
    def test_construction_from_period(self):
        # upsampling
        start, end = Period('2017Q1', freq='Q'), Period('2018Q1', freq='Q')
        expected = date_range(start='2017-03-31', end='2018-03-31', freq='M',
                              name='foo').to_period()
        result = period_range(start=start, end=end, freq='M', name='foo')
        tm.assert_index_equal(result, expected)

        # downsampling
        start, end = Period('2017-1', freq='M'), Period('2019-12', freq='M')
        expected = date_range(start='2017-01-31', end='2019-12-31', freq='Q',
                              name='foo').to_period()
        result = period_range(start=start, end=end, freq='Q', name='foo')
        tm.assert_index_equal(result, expected)

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

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

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

        result = period_range(start=end, end=start, freq='W', name='foo')
        tm.assert_index_equal(result, expected)
Beispiel #8
0
    def test_series(self):

        # GH6407
        # inferring series

        # invalid type of Series
        for s in [Series(np.arange(10)), Series(np.arange(10.0))]:
            self.assertRaises(TypeError, lambda: frequencies.infer_freq(s))

        # a non-convertible string
        self.assertRaises(ValueError, lambda: frequencies.infer_freq(Series(["foo", "bar"])))

        # cannot infer on PeriodIndex
        for freq in [None, "L"]:
            s = Series(period_range("2013", periods=10, freq=freq))
            self.assertRaises(TypeError, lambda: frequencies.infer_freq(s))
        for freq in ["Y"]:

            msg = frequencies._INVALID_FREQ_ERROR
            with tm.assertRaisesRegexp(ValueError, msg):
                s = Series(period_range("2013", periods=10, freq=freq))
            self.assertRaises(TypeError, lambda: frequencies.infer_freq(s))

        # DateTimeIndex
        for freq in ["M", "L", "S"]:
            s = Series(date_range("20130101", periods=10, freq=freq))
            inferred = frequencies.infer_freq(s)
            self.assertEqual(inferred, freq)

        s = Series(date_range("20130101", "20130110"))
        inferred = frequencies.infer_freq(s)
        self.assertEqual(inferred, "D")
    def test_constructor_freq_mult(self):
        # GH #7811
        for func in [PeriodIndex, period_range]:
            # must be the same, but for sure...
            pidx = func(start='2014-01', freq='2M', periods=4)
            expected = PeriodIndex(['2014-01', '2014-03',
                                    '2014-05', '2014-07'], freq='2M')
            tm.assert_index_equal(pidx, expected)

            pidx = func(start='2014-01-02', end='2014-01-15', freq='3D')
            expected = PeriodIndex(['2014-01-02', '2014-01-05',
                                    '2014-01-08', '2014-01-11',
                                    '2014-01-14'], freq='3D')
            tm.assert_index_equal(pidx, expected)

            pidx = func(end='2014-01-01 17:00', freq='4H', periods=3)
            expected = PeriodIndex(['2014-01-01 09:00', '2014-01-01 13:00',
                                    '2014-01-01 17:00'], freq='4H')
            tm.assert_index_equal(pidx, expected)

        msg = ('Frequency must be positive, because it'
               ' represents span: -1M')
        with tm.assertRaisesRegexp(ValueError, msg):
            PeriodIndex(['2011-01'], freq='-1M')

        msg = ('Frequency must be positive, because it' ' represents span: 0M')
        with tm.assertRaisesRegexp(ValueError, msg):
            PeriodIndex(['2011-01'], freq='0M')

        msg = ('Frequency must be positive, because it' ' represents span: 0M')
        with tm.assertRaisesRegexp(ValueError, msg):
            period_range('2011-01', periods=3, freq='0M')
Beispiel #10
0
    def test_get_loc2(self):
        idx = pd.period_range('2000-01-01', periods=3)

        for method in [None, 'pad', 'backfill', 'nearest']:
            assert idx.get_loc(idx[1], method) == 1
            assert idx.get_loc(idx[1].asfreq('H', how='start'), method) == 1
            assert idx.get_loc(idx[1].to_timestamp(), method) == 1
            assert idx.get_loc(idx[1].to_timestamp()
                               .to_pydatetime(), method) == 1
            assert idx.get_loc(str(idx[1]), method) == 1

        idx = pd.period_range('2000-01-01', periods=5)[::2]
        assert idx.get_loc('2000-01-02T12', method='nearest',
                           tolerance='1 day') == 1
        assert idx.get_loc('2000-01-02T12', method='nearest',
                           tolerance=pd.Timedelta('1D')) == 1
        assert idx.get_loc('2000-01-02T12', method='nearest',
                           tolerance=np.timedelta64(1, 'D')) == 1
        assert idx.get_loc('2000-01-02T12', method='nearest',
                           tolerance=timedelta(1)) == 1
        with tm.assert_raises_regex(ValueError,
                                    'unit abbreviation w/o a number'):
            idx.get_loc('2000-01-10', method='nearest', tolerance='foo')

        msg = 'Input has different freq from PeriodIndex\\(freq=D\\)'
        with tm.assert_raises_regex(ValueError, msg):
            idx.get_loc('2000-01-10', method='nearest', tolerance='1 hour')
        with pytest.raises(KeyError):
            idx.get_loc('2000-01-10', method='nearest', tolerance='1 day')
        with pytest.raises(
                ValueError,
                match='list-like tolerance size must match target index size'):
            idx.get_loc('2000-01-10', method='nearest',
                        tolerance=[pd.Timedelta('1 day').to_timedelta64(),
                                   pd.Timedelta('1 day').to_timedelta64()])
Beispiel #11
0
    def test_categorical_series_repr_period_ordered(self):
        idx = period_range('2011-01-01 09:00', freq='H', periods=5)
        s = Series(Categorical(idx, ordered=True))
        exp = """0   2011-01-01 09:00
1   2011-01-01 10:00
2   2011-01-01 11:00
3   2011-01-01 12:00
4   2011-01-01 13:00
dtype: category
Categories (5, period[H]): [2011-01-01 09:00 < 2011-01-01 10:00 < 2011-01-01 11:00 < 2011-01-01 12:00 <
                            2011-01-01 13:00]"""  # noqa

        assert repr(s) == exp

        idx = period_range('2011-01', freq='M', periods=5)
        s = Series(Categorical(idx, ordered=True))
        exp = """0   2011-01
1   2011-02
2   2011-03
3   2011-04
4   2011-05
dtype: category
Categories (5, period[M]): [2011-01 < 2011-02 < 2011-03 < 2011-04 < 2011-05]"""

        assert repr(s) == exp
Beispiel #12
0
    def test_pi_add_sub_td64_array_tick(self):
        # PeriodIndex + Timedelta-like is allowed only with
        #   tick-like frequencies
        rng = pd.period_range('1/1/2000', freq='90D', periods=3)
        tdi = pd.TimedeltaIndex(['-1 Day', '-1 Day', '-1 Day'])
        tdarr = tdi.values

        expected = pd.period_range('12/31/1999', freq='90D', periods=3)
        result = rng + tdi
        tm.assert_index_equal(result, expected)
        result = rng + tdarr
        tm.assert_index_equal(result, expected)
        result = tdi + rng
        tm.assert_index_equal(result, expected)
        result = tdarr + rng
        tm.assert_index_equal(result, expected)

        expected = pd.period_range('1/2/2000', freq='90D', periods=3)

        result = rng - tdi
        tm.assert_index_equal(result, expected)
        result = rng - tdarr
        tm.assert_index_equal(result, expected)

        with pytest.raises(TypeError):
            tdarr - rng

        with pytest.raises(TypeError):
            tdi - rng
Beispiel #13
0
    def test_series(self):

        # GH6407
        # inferring series

        # invalid type of Series
        for s in [ Series(np.arange(10)),
                   Series(np.arange(10.))]:
            self.assertRaises(TypeError, lambda : frequencies.infer_freq(s))

        # a non-convertible string
        self.assertRaises(ValueError, lambda : frequencies.infer_freq(Series(['foo','bar'])))

        # cannot infer on PeriodIndex
        for freq in [None, 'L']:
            s = Series(period_range('2013',periods=10,freq=freq))
            self.assertRaises(TypeError, lambda : frequencies.infer_freq(s))
        for freq in ['Y']:
            with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
                s = Series(period_range('2013',periods=10,freq=freq))
            self.assertRaises(TypeError, lambda : frequencies.infer_freq(s))

        # DateTimeIndex
        for freq in ['M', 'L', 'S']:
            s = Series(date_range('20130101',periods=10,freq=freq))
            inferred = frequencies.infer_freq(s)
            self.assertEqual(inferred,freq)

        s = Series(date_range('20130101','20130110'))
        inferred = frequencies.infer_freq(s)
        self.assertEqual(inferred,'D')
    def test_get_loc(self):
        idx = pd.period_range('2000-01-01', periods=3)

        for method in [None, 'pad', 'backfill', 'nearest']:
            self.assertEqual(idx.get_loc(idx[1], method), 1)
            self.assertEqual(
                idx.get_loc(idx[1].asfreq('H', how='start'), method), 1)
            self.assertEqual(idx.get_loc(idx[1].to_timestamp(), method), 1)
            self.assertEqual(
                idx.get_loc(idx[1].to_timestamp().to_pydatetime(), method), 1)
            self.assertEqual(idx.get_loc(str(idx[1]), method), 1)

        idx = pd.period_range('2000-01-01', periods=5)[::2]
        self.assertEqual(idx.get_loc('2000-01-02T12', method='nearest',
                                     tolerance='1 day'), 1)
        self.assertEqual(idx.get_loc('2000-01-02T12', method='nearest',
                                     tolerance=pd.Timedelta('1D')), 1)
        self.assertEqual(idx.get_loc('2000-01-02T12', method='nearest',
                                     tolerance=np.timedelta64(1, 'D')), 1)
        self.assertEqual(idx.get_loc('2000-01-02T12', method='nearest',
                                     tolerance=timedelta(1)), 1)
        with tm.assertRaisesRegexp(ValueError, 'must be convertible'):
            idx.get_loc('2000-01-10', method='nearest', tolerance='foo')

        msg = 'Input has different freq from PeriodIndex\\(freq=D\\)'
        with tm.assertRaisesRegexp(ValueError, msg):
            idx.get_loc('2000-01-10', method='nearest', tolerance='1 hour')
        with tm.assertRaises(KeyError):
            idx.get_loc('2000-01-10', method='nearest', tolerance='1 day')
Beispiel #15
0
    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)
Beispiel #16
0
    def test_constructor_field_arrays(self):
        # GH #1264

        years = np.arange(1990, 2010).repeat(4)[2:-2]
        quarters = np.tile(np.arange(1, 5), 20)[2:-2]

        index = PeriodIndex(year=years, quarter=quarters, freq='Q-DEC')
        expected = period_range('1990Q3', '2009Q2', freq='Q-DEC')
        tm.assert_index_equal(index, expected)

        index2 = PeriodIndex(year=years, quarter=quarters, freq='2Q-DEC')
        tm.assert_numpy_array_equal(index.asi8, index2.asi8)

        index = PeriodIndex(year=years, quarter=quarters)
        tm.assert_index_equal(index, expected)

        years = [2007, 2007, 2007]
        months = [1, 2]
        self.assertRaises(ValueError, PeriodIndex, year=years, month=months,
                          freq='M')
        self.assertRaises(ValueError, PeriodIndex, year=years, month=months,
                          freq='2M')
        self.assertRaises(ValueError, PeriodIndex, year=years, month=months,
                          freq='M', start=Period('2007-01', freq='M'))

        years = [2007, 2007, 2007]
        months = [1, 2, 3]
        idx = PeriodIndex(year=years, month=months, freq='M')
        exp = period_range('2007-01', periods=3, freq='M')
        tm.assert_index_equal(idx, exp)
 def test_resample_count(self, freq, expected_vals):
     # GH12774
     series = Series(1, index=pd.period_range(start='2000', periods=100))
     result = series.resample(freq).count()
     expected_index = pd.period_range(start='2000', freq=freq,
                                      periods=len(expected_vals))
     expected = Series(expected_vals, index=expected_index)
     assert_series_equal(result, expected)
Beispiel #18
0
 def test_pi_add_iadd_int(self, one):
     # Variants of `one` for #19012
     rng = pd.period_range('2000-01-01 09:00', freq='H', periods=10)
     result = rng + one
     expected = pd.period_range('2000-01-01 10:00', freq='H', periods=10)
     tm.assert_index_equal(result, expected)
     rng += one
     tm.assert_index_equal(rng, expected)
Beispiel #19
0
    def test_parr_sub_pi_mismatched_freq(self, box_with_array):
        rng = pd.period_range('1/1/2000', freq='D', periods=5)
        other = pd.period_range('1/6/2000', freq='H', periods=5)
        # TODO: parametrize over boxes for other?

        rng = tm.box_expected(rng, box_with_array)
        with pytest.raises(IncompatibleFrequency):
            rng - other
Beispiel #20
0
    def test_representation_to_series(self):
        # GH#10971
        idx1 = PeriodIndex([], freq='D')
        idx2 = PeriodIndex(['2011-01-01'], freq='D')
        idx3 = PeriodIndex(['2011-01-01', '2011-01-02'], freq='D')
        idx4 = PeriodIndex(['2011-01-01', '2011-01-02', '2011-01-03'],
                           freq='D')
        idx5 = PeriodIndex(['2011', '2012', '2013'], freq='A')
        idx6 = PeriodIndex(['2011-01-01 09:00', '2012-02-01 10:00', 'NaT'],
                           freq='H')

        idx7 = pd.period_range('2013Q1', periods=1, freq="Q")
        idx8 = pd.period_range('2013Q1', periods=2, freq="Q")
        idx9 = pd.period_range('2013Q1', periods=3, freq="Q")

        exp1 = """Series([], dtype: period[D])"""

        exp2 = """0    2011-01-01
dtype: period[D]"""

        exp3 = """0    2011-01-01
1    2011-01-02
dtype: period[D]"""

        exp4 = """0    2011-01-01
1    2011-01-02
2    2011-01-03
dtype: period[D]"""

        exp5 = """0    2011
1    2012
2    2013
dtype: period[A-DEC]"""

        exp6 = """0    2011-01-01 09:00
1    2012-02-01 10:00
2                 NaT
dtype: period[H]"""

        exp7 = """0    2013Q1
dtype: period[Q-DEC]"""

        exp8 = """0    2013Q1
1    2013Q2
dtype: period[Q-DEC]"""

        exp9 = """0    2013Q1
1    2013Q2
2    2013Q3
dtype: period[Q-DEC]"""

        for idx, expected in zip([idx1, idx2, idx3, idx4, idx5,
                                  idx6, idx7, idx8, idx9],
                                 [exp1, exp2, exp3, exp4, exp5,
                                  exp6, exp7, exp8, exp9]):
            result = repr(pd.Series(idx))
            assert result == expected
Beispiel #21
0
    def test_pi_add_iadd_timedeltalike_M(self):
        rng = pd.period_range('2014-01', '2016-12', freq='M')
        expected = pd.period_range('2014-06', '2017-05', freq='M')

        result = rng + pd.offsets.MonthEnd(5)
        tm.assert_index_equal(result, expected)

        rng += pd.offsets.MonthEnd(5)
        tm.assert_index_equal(rng, expected)
Beispiel #22
0
 def test_add_iadd_timedeltalike_annual(self):
     # offset
     # DateOffset
     rng = pd.period_range('2014', '2024', freq='A')
     result = rng + pd.offsets.YearEnd(5)
     expected = pd.period_range('2019', '2029', freq='A')
     tm.assert_index_equal(result, expected)
     rng += pd.offsets.YearEnd(5)
     tm.assert_index_equal(rng, expected)
Beispiel #23
0
    def test_union_dataframe_index(self):
        rng1 = pd.period_range('1/1/1999', '1/1/2012', freq='M')
        s1 = pd.Series(np.random.randn(len(rng1)), rng1)

        rng2 = pd.period_range('1/1/1980', '12/1/2001', freq='M')
        s2 = pd.Series(np.random.randn(len(rng2)), rng2)
        df = pd.DataFrame({'s1': s1, 's2': s2})

        exp = pd.period_range('1/1/1980', '1/1/2012', freq='M')
        tm.assert_index_equal(df.index, exp)
Beispiel #24
0
    def test_constructor_use_start_freq(self):
        # GH #1118
        p = Period('4/2/2012', freq='B')
        with tm.assert_produces_warning(FutureWarning):
            index = PeriodIndex(start=p, periods=10)
        expected = period_range(start='4/2/2012', periods=10, freq='B')
        tm.assert_index_equal(index, expected)

        index = period_range(start=p, periods=10)
        tm.assert_index_equal(index, expected)
Beispiel #25
0
    def test_pi_add_iadd_pi_raises(self):
        rng = pd.period_range('1/1/2000', freq='D', periods=5)
        other = pd.period_range('1/6/2000', freq='D', periods=5)

        # previously performed setop union, now raises TypeError (GH14164)
        with pytest.raises(TypeError):
            rng + other

        with pytest.raises(TypeError):
            rng += other
Beispiel #26
0
    def test_period_set_index_reindex(self):
        # GH 6631
        df = DataFrame(np.random.random(6))
        idx1 = period_range('2011/01/01', periods=6, freq='M')
        idx2 = period_range('2013', periods=6, freq='A')

        df = df.set_index(idx1)
        tm.assert_index_equal(df.index, idx1)
        df = df.set_index(idx2)
        tm.assert_index_equal(df.index, idx2)
Beispiel #27
0
    def test_fields(self):
        # year, month, day, hour, minute
        # second, weekofyear, week, dayofweek, weekday, dayofyear, quarter
        # qyear
        pi = period_range(freq='A', start='1/1/2001', end='12/1/2005')
        self._check_all_fields(pi)

        pi = period_range(freq='Q', start='1/1/2001', end='12/1/2002')
        self._check_all_fields(pi)

        pi = period_range(freq='M', start='1/1/2001', end='1/1/2002')
        self._check_all_fields(pi)

        pi = period_range(freq='D', start='12/1/2001', end='6/1/2001')
        self._check_all_fields(pi)

        pi = period_range(freq='B', start='12/1/2001', end='6/1/2001')
        self._check_all_fields(pi)

        pi = period_range(freq='H', start='12/31/2001', end='1/1/2002 23:00')
        self._check_all_fields(pi)

        pi = period_range(freq='Min', start='12/31/2001', end='1/1/2002 00:20')
        self._check_all_fields(pi)

        pi = period_range(freq='S', start='12/31/2001 00:00:00',
                          end='12/31/2001 00:05:00')
        self._check_all_fields(pi)

        end_intv = Period('2006-12-31', 'W')
        i1 = period_range(end=end_intv, periods=10)
        self._check_all_fields(i1)
    def test_union_categorical(self):
        # GH 13361
        data = [
            (list('abc'), list('abd'), list('abcabd')),
            ([0, 1, 2], [2, 3, 4], [0, 1, 2, 2, 3, 4]),
            ([0, 1.2, 2], [2, 3.4, 4], [0, 1.2, 2, 2, 3.4, 4]),

            (['b', 'b', np.nan, 'a'], ['a', np.nan, 'c'],
             ['b', 'b', np.nan, 'a', 'a', np.nan, 'c']),

            (pd.date_range('2014-01-01', '2014-01-05'),
             pd.date_range('2014-01-06', '2014-01-07'),
             pd.date_range('2014-01-01', '2014-01-07')),

            (pd.date_range('2014-01-01', '2014-01-05', tz='US/Central'),
             pd.date_range('2014-01-06', '2014-01-07', tz='US/Central'),
             pd.date_range('2014-01-01', '2014-01-07', tz='US/Central')),

            (pd.period_range('2014-01-01', '2014-01-05'),
             pd.period_range('2014-01-06', '2014-01-07'),
             pd.period_range('2014-01-01', '2014-01-07')),
        ]

        for a, b, combined in data:
            for box in [Categorical, CategoricalIndex, Series]:
                result = union_categoricals([box(Categorical(a)),
                                             box(Categorical(b))])
                expected = Categorical(combined)
                tm.assert_categorical_equal(result, expected,
                                            check_category_order=True)

        # new categories ordered by appearance
        s = Categorical(['x', 'y', 'z'])
        s2 = Categorical(['a', 'b', 'c'])
        result = union_categoricals([s, s2])
        expected = Categorical(['x', 'y', 'z', 'a', 'b', 'c'],
                               categories=['x', 'y', 'z', 'a', 'b', 'c'])
        tm.assert_categorical_equal(result, expected)

        s = Categorical([0, 1.2, 2], ordered=True)
        s2 = Categorical([0, 1.2, 2], ordered=True)
        result = union_categoricals([s, s2])
        expected = Categorical([0, 1.2, 2, 0, 1.2, 2], ordered=True)
        tm.assert_categorical_equal(result, expected)

        # must exactly match types
        s = Categorical([0, 1.2, 2])
        s2 = Categorical([2, 3, 4])
        msg = 'dtype of categories must be the same'
        with tm.assertRaisesRegexp(TypeError, msg):
            union_categoricals([s, s2])

        msg = 'No Categoricals to union'
        with tm.assertRaisesRegexp(ValueError, msg):
            union_categoricals([])
Beispiel #29
0
    def test_pi_sub_isub_timedeltalike_daily(self, three_days):
        # Tick-like 3 Days
        other = three_days
        rng = pd.period_range('2014-05-01', '2014-05-15', freq='D')
        expected = pd.period_range('2014-04-28', '2014-05-12', freq='D')

        result = rng - other
        tm.assert_index_equal(result, expected)

        rng -= other
        tm.assert_index_equal(rng, expected)
Beispiel #30
0
    def test_pi_sub_isub_timedeltalike_hourly(self, two_hours):
        other = two_hours
        rng = pd.period_range('2014-01-01 10:00', '2014-01-05 10:00', freq='H')
        expected = pd.period_range('2014-01-01 08:00', '2014-01-05 08:00',
                                   freq='H')

        result = rng - other
        tm.assert_index_equal(result, expected)

        rng -= other
        tm.assert_index_equal(rng, expected)
Beispiel #31
0
def test_period_index_roundtrip():
    idx = pd.period_range(start='2017-01-01', end='2018-01-01', freq='M')
    decoded_idx = roundtrip(idx)
    assert_index_equal(decoded_idx, idx)