Example #1
0
    def test_frame_to_period(self):
        K = 5
        from pandas.tseries.period import period_range

        dr = date_range('1/1/2000', '1/1/2001')
        pr = period_range('1/1/2000', '1/1/2001')
        df = DataFrame(randn(len(dr), K), index=dr)
        df['mix'] = 'a'

        pts = df.to_period()
        exp = df.copy()
        exp.index = pr
        assert_frame_equal(pts, exp)

        pts = df.to_period('M')
        tm.assert_index_equal(pts.index, exp.index.asfreq('M'))

        df = df.T
        pts = df.to_period(axis=1)
        exp = df.copy()
        exp.columns = pr
        assert_frame_equal(pts, exp)

        pts = df.to_period('M', axis=1)
        tm.assert_index_equal(pts.columns, exp.columns.asfreq('M'))

        self.assertRaises(ValueError, df.to_period, axis=2)
Example #2
0
    def test_to_period(self):
        from pandas.tseries.period import period_range

        ts = _simple_ts('1/1/2000', '1/1/2001')

        pts = ts.to_period()
        exp = ts.copy()
        exp.index = period_range('1/1/2000', '1/1/2001')
        assert_series_equal(pts, exp)

        pts = ts.to_period('M')
        exp.index = exp.index.asfreq('M')
        tm.assert_index_equal(pts.index, exp.index.asfreq('M'))
        assert_series_equal(pts, exp)

        # GH 7606 without freq
        idx = DatetimeIndex(['2011-01-01', '2011-01-02', '2011-01-03',
                             '2011-01-04'])
        exp_idx = pd.PeriodIndex(['2011-01-01', '2011-01-02', '2011-01-03',
                                  '2011-01-04'], freq='D')

        s = Series(np.random.randn(4), index=idx)
        expected = s.copy()
        expected.index = exp_idx
        assert_series_equal(s.to_period(), expected)

        df = DataFrame(np.random.randn(4, 4), index=idx, columns=idx)
        expected = df.copy()
        expected.index = exp_idx
        assert_frame_equal(df.to_period(), expected)

        expected = df.copy()
        expected.columns = exp_idx
        assert_frame_equal(df.to_period(axis=1), expected)
Example #3
0
    def test_frame_to_period(self):
        K = 5
        from pandas.tseries.period import period_range

        dr = date_range('1/1/2000', '1/1/2001')
        pr = period_range('1/1/2000', '1/1/2001')
        df = DataFrame(randn(len(dr), K), index=dr)
        df['mix'] = 'a'

        pts = df.to_period()
        exp = df.copy()
        exp.index = pr
        assert_frame_equal(pts, exp)

        pts = df.to_period('M')
        self.assert_(pts.index.equals(exp.index.asfreq('M')))

        """ Put me back in after fixing DataFrame bug
Example #4
0
    def test_frame_to_period(self):
        K = 5

        dr = date_range('1/1/2000', '1/1/2001')
        pr = period_range('1/1/2000', '1/1/2001')
        df = DataFrame(np.random.randn(len(dr), K), index=dr)
        df['mix'] = 'a'

        pts = df.to_period()
        exp = df.copy()
        exp.index = pr
        assert_frame_equal(pts, exp)

        pts = df.to_period('M')
        tm.assert_index_equal(pts.index, exp.index.asfreq('M'))

        df = df.T
        pts = df.to_period(axis=1)
        exp = df.copy()
        exp.columns = pr
        assert_frame_equal(pts, exp)

        pts = df.to_period('M', axis=1)
        tm.assert_index_equal(pts.columns, exp.columns.asfreq('M'))

        msg = ("No axis named 2 for object type"
               " <class 'pandas.core.frame.DataFrame'>")
        with pytest.raises(ValueError, match=msg):
            df.to_period(axis=2)
Example #5
0
def create_multibarchart(obj, datasources, freq='D'):
    """
        Returns a multibarchart
    """
    start_time = datetime.utcnow().replace(tzinfo=utc)
    end_time = datetime.min.replace(tzinfo=utc)

    tooltip_date = "%d %b %Y"
    extra_serie = {"tooltip": {"y_start": "", "y_end": ""},
                   "date_format": tooltip_date}

    data = {}
    for ds in datasources:
        dps = obj.get_datapoints(ds, recursive=True)
        if not dps.count():
            continue
        df = DataFrame(list(dps.values('start', 'value')))
        df.set_index('start', inplace=True)
        ps = df.to_period(freq=freq).groupby(level=0).sum()

        if dps.order_by('start').first().start < start_time:
            start_time = dps.order_by('start').first().start

        if dps.order_by('end').last().end > end_time:
            end_time = dps.order_by('end').last().start

        data[ds] = ps

    prng = period_range(start=start_time.replace(hour=0, minute=0, second=0),
                        end=end_time.replace(hour=0, minute=0, second=0))
    ps_total = DataFrame(index=prng)
    columns = []
    for ds, ps in data.items():
        columns.append(ds.name)
        ps_total = ps_total.join(ps, lsuffix='_left', rsuffix='_right')
        ps_total.columns = columns

    ps_total = ps_total.reindex(prng).fillna(0)
    ps_total = ps_total.applymap(lambda x: x/1024**3)

    chartdata = {}
    i = 0
    for ds in ps_total.columns:
        i += 1
        chartdata['name{}'.format(i)] = ds
        chartdata['y{}'.format(i)] = [float(x) for x in list(ps_total[ds].values)]
        chartdata['extra{}'.format(i)] = extra_serie

    chartdata['x'] = [str(x / 10**6) for x in list(ps_total.index.to_datetime().astype(np.int64))]

    data = {
        'charttype': 'multiBarChart',
        'chartdata_with_date': chartdata,
        'chartcontainer_with_date': 'date_multibarchart_container',
        'extra_with_date': {
            'name': 'date_multibarchart_container',
            'x_is_date': True,
            'x_axis_format': '%d %b %Y',
            'tag_script_js': True,
            'jquery_on_ready': True,
        },
    }

    return data