Ejemplo n.º 1
0
    def test_columnpanel_groupby(self):
        """
            Test wrapping the panel groupby with ColumnPanelGroupBy
        """
        N = 10000
        data = {}
        data['AAPL'] = tm.fake_ohlc(N)
        data['AMD'] = tm.fake_ohlc(N)
        data['INTC'] = tm.fake_ohlc(N)
        cp = ColumnPanel(data)

        ds = cp.dataset()
        ds['log_returns'] = np.log(1+cp.close.pct_change())
        ds['close'] = cp.close

        # make sure to get the panelgroupby
        grouped = ds.to_panel().downsample('MS', label='left', closed='left')
        cpgrouped = ColumnPanelGroupBy(grouped)
        # check if meaned is equal
        cmean = grouped.mean()
        test_mean = cpgrouped.process(lambda df: df.mean())
        tm.assert_panel_equal(cmean, test_mean)

        bins = [-1, 0, 1]

        # 12-19-12 errors. 
        # problem is that downsample returns a PanelGroupBy which doesn't have the 
        # delegate and combine logic.
        test_tiled = cpgrouped.process(lambda df: df.tile(bins, 'log_returns').mean())
        correct = grouped.process(
            lambda df: ColumnPanelMapper(df).tile(bins, 'log_returns').mean())

        # this implicitly checks that inputs are panel4d
        tm.assert_panel4d_equal(test_tiled, correct)
Ejemplo n.º 2
0
    def test_bundle_io(self):
        """
            Test saving bundle io. 
        """
        N = 10000
        data = {}
        data['AAPL'] = tm.fake_ohlc(N)
        data['AMD'] = tm.fake_ohlc(N).tail(100)
        data['INTC'] = tm.fake_ohlc(N).head(100)
        cp = ColumnPanel(data)

        with TemporaryDirectory() as td:
            path = td + 'TEST.columnpanel'
            cp.bundle_save(path)

            loaded = cp.bundle_load(path)
            tm.assert_columnpanel_equal(cp, loaded)
Ejemplo n.º 3
0
from unittest import TestCase

import pandas as pd
import numpy as np

import trtools.io.bundle as b
import trtools.util.testing as tm
from trtools.util.tempdir import TemporaryDirectory

# panel with many items and < 10 columns
panel = pd.Panel({('item'+str(i), i, i+.0324) : tm.fake_ohlc() for i in range(5000)})
panel.items = pd.MultiIndex.from_tuples(panel.items)
assert isinstance(panel.items, pd.MultiIndex) # make this more complicated

class TestBundle(TestCase):
    def __init__(self, *args, **kwargs):
        TestCase.__init__(self, *args, **kwargs)

    def runTest(self):
        pass

    def setUp(self):
        pass

    def test_panel(self):
        """
        Overview test that panel bundle saving works
        """
        with TemporaryDirectory() as td:
            b.save_panel(panel, td)
Ejemplo n.º 4
0
from unittest import TestCase

import pandas as pd
from pandas.core.groupby import BinGrouper
import trtools.util.testing as tm
import numpy as np

import trtools.core.timeseries as ts

# start on friday, so second day is saturday
df = tm.fake_ohlc(1000000, freq="5min", start="2000-01-07")
# business days and trading hours
df = df.ix[df.index.dayofweek < 5] 
df = ts.trading_hours(df)

class TestBinning(TestCase):

    def __init__(self, *args, **kwargs):
        TestCase.__init__(self, *args, **kwargs)

    def runTest(self):
        pass

    def setUp(self):
        pass

    def downsample(self):
        # these should be equivalent
        grouped = df.downsample('D', drop_empty=False)
        test = grouped.mean()
        correct = df.resample('D', how='mean')
Ejemplo n.º 5
0
from unittest import TestCase

import pandas as pd
from pandas.core.groupby import BinGrouper
import trtools.util.testing as tm
import numpy as np

import trtools.core.timeseries as ts

# start on friday, so second day is saturday
df = tm.fake_ohlc(1000000, freq="5min", start="2000-01-07")
# business days and trading hours
df = df.ix[df.index.dayofweek < 5]
df = ts.trading_hours(df)


class TestBinning(TestCase):
    def __init__(self, *args, **kwargs):
        TestCase.__init__(self, *args, **kwargs)

    def runTest(self):
        pass

    def setUp(self):
        pass

    def test_downsample(self):
        # these should be equivalent
        grouped = df.downsample('D', drop_empty=False)
        test = grouped.mean()
        correct = df.resample('D', how='mean')
Ejemplo n.º 6
0
from unittest import TestCase

import pandas as pd
import numpy as np

import trtools.io.bundle as b
import trtools.util.testing as tm
from trtools.util.tempdir import TemporaryDirectory

# panel with many items and < 10 columns
panel = pd.Panel({('item' + str(i), i, i + .0324): tm.fake_ohlc()
                  for i in range(5000)})
panel.items = pd.MultiIndex.from_tuples(panel.items)
assert isinstance(panel.items, pd.MultiIndex)  # make this more complicated


class TestBundle(TestCase):
    def __init__(self, *args, **kwargs):
        TestCase.__init__(self, *args, **kwargs)

    def runTest(self):
        pass

    def setUp(self):
        pass

    def test_panel(self):
        """
        Overview test that panel bundle saving works
        """
        with TemporaryDirectory() as td:
Ejemplo n.º 7
0
        bins = [-1, 0, 1]

        # 12-19-12 errors. 
        # problem is that downsample returns a PanelGroupBy which doesn't have the 
        # delegate and combine logic.
        test_tiled = cpgrouped.process(lambda df: df.tile(bins, 'log_returns').mean())
        correct = grouped.process(
            lambda df: ColumnPanelMapper(df).tile(bins, 'log_returns').mean())

        # this implicitly checks that inputs are panel4d
        tm.assert_panel4d_equal(test_tiled, correct)

N = 10000
data = {}
data['AAPL'] = tm.fake_ohlc(N)
data['AMD'] = tm.fake_ohlc(N)
data['INTC'] = tm.fake_ohlc(N)
cp = ColumnPanel(data)

ds = cp.dataset()
ds['log_returns'] = np.log(1+cp.close.pct_change())
ds['close'] = cp.close

# make sure to get the panelgroupby
grouped = ds.to_panel().downsample('MS', label='left', closed='left')
cpgrouped = ColumnPanelGroupBy(grouped)
# check if meaned is equal
cmean = grouped.mean()
test_mean = cpgrouped.process(lambda df: df.mean())
tm.assert_panel_equal(cmean, test_mean)