Пример #1
0
    def test_resample_panel(self):
        rng = date_range("1/1/2000", "6/30/2000")
        n = len(rng)

        panel = Panel(
            np.random.randn(3, n, 5),
            items=["one", "two", "three"],
            major_axis=rng,
            minor_axis=["a", "b", "c", "d", "e"],
        )

        result = panel.resample("M", axis=1)

        def p_apply(panel, f):
            result = {}
            for item in panel.items:
                result[item] = f(panel[item])
            return Panel(result, items=panel.items)

        expected = p_apply(panel, lambda x: x.resample("M"))
        tm.assert_panel_equal(result, expected)

        panel2 = panel.swapaxes(1, 2)
        result = panel2.resample("M", axis=2)
        expected = p_apply(panel2, lambda x: x.resample("M", axis=1))
        tm.assert_panel_equal(result, expected)
Пример #2
0
def test_resample_panel():
    rng = date_range('1/1/2000', '6/30/2000')
    n = len(rng)

    with catch_warnings(record=True):
        simplefilter("ignore", FutureWarning)
        panel = Panel(np.random.randn(3, n, 5),
                      items=['one', 'two', 'three'],
                      major_axis=rng,
                      minor_axis=['a', 'b', 'c', 'd', 'e'])

        result = panel.resample('M', axis=1).mean()

        def p_apply(panel, f):
            result = {}
            for item in panel.items:
                result[item] = f(panel[item])
            return Panel(result, items=panel.items)

        expected = p_apply(panel, lambda x: x.resample('M').mean())
        tm.assert_panel_equal(result, expected)

        panel2 = panel.swapaxes(1, 2)
        result = panel2.resample('M', axis=2).mean()
        expected = p_apply(panel2,
                           lambda x: x.resample('M', axis=1).mean())
        tm.assert_panel_equal(result, expected)
Пример #3
0
def test_resample_panel():
    rng = date_range('1/1/2000', '6/30/2000')
    n = len(rng)

    with catch_warnings(record=True):
        simplefilter("ignore", FutureWarning)
        panel = Panel(np.random.randn(3, n, 5),
                      items=['one', 'two', 'three'],
                      major_axis=rng,
                      minor_axis=['a', 'b', 'c', 'd', 'e'])

        result = panel.resample('M', axis=1).mean()

        def p_apply(panel, f):
            result = {}
            for item in panel.items:
                result[item] = f(panel[item])
            return Panel(result, items=panel.items)

        expected = p_apply(panel, lambda x: x.resample('M').mean())
        tm.assert_panel_equal(result, expected)

        panel2 = panel.swapaxes(1, 2)
        result = panel2.resample('M', axis=2).mean()
        expected = p_apply(panel2,
                           lambda x: x.resample('M', axis=1).mean())
        tm.assert_panel_equal(result, expected)
Пример #4
0
    def test_resample_panel_numpy(self):
        rng = date_range('1/1/2000', '6/30/2000')
        n = len(rng)

        panel = Panel(np.random.randn(3, n, 5),
                      items=['one', 'two', 'three'],
                      major_axis=rng,
                      minor_axis=['a', 'b', 'c', 'd', 'e'])

        result = panel.resample('M', how=lambda x: x.mean(1), axis=1)
        expected = panel.resample('M', how='mean', axis=1)
        tm.assert_panel_equal(result, expected)

        panel = panel.swapaxes(1, 2)
        result = panel.resample('M', how=lambda x: x.mean(2), axis=2)
        expected = panel.resample('M', how='mean', axis=2)
        tm.assert_panel_equal(result, expected)
Пример #5
0
    def test_resample_panel_numpy(self):
        rng = date_range('1/1/2000', '6/30/2000')
        n = len(rng)

        panel = Panel(np.random.randn(3, n, 5),
                      items=['one', 'two', 'three'],
                      major_axis=rng,
                      minor_axis=['a', 'b', 'c', 'd', 'e'])

        result = panel.resample('M', how=lambda x: x.mean(1), axis=1)
        expected = panel.resample('M', how='mean', axis=1)
        tm.assert_panel_equal(result, expected)

        panel = panel.swapaxes(1, 2)
        result = panel.resample('M', how=lambda x: x.mean(2), axis=2)
        expected = panel.resample('M', how='mean', axis=2)
        tm.assert_panel_equal(result, expected)
Пример #6
0
    def first_difference(self):
        """
        Compute first differences of variables

        Returns
        -------
        diffs : PanelData
            Differenced values
        """
        diffs = self.panel.values
        diffs = diffs[:, 1:] - diffs[:, :-1]
        diffs = Panel(diffs, items=self.panel.items,
                      major_axis=self.panel.major_axis[1:],
                      minor_axis=self.panel.minor_axis)
        diffs = diffs.swapaxes(1, 2).to_frame(filter_observations=False)
        diffs = diffs.reindex(self._frame.index).dropna(how='any')
        return PanelData(diffs)
Пример #7
0
def test_resample_panel_numpy():
    rng = date_range('1/1/2000', '6/30/2000')
    n = len(rng)

    with catch_warnings(record=True):
        panel = Panel(np.random.randn(3, n, 5),
                      items=['one', 'two', 'three'],
                      major_axis=rng,
                      minor_axis=['a', 'b', 'c', 'd', 'e'])

        result = panel.resample('M', axis=1).apply(lambda x: x.mean(1))
        expected = panel.resample('M', axis=1).mean()
        tm.assert_panel_equal(result, expected)

        panel = panel.swapaxes(1, 2)
        result = panel.resample('M', axis=2).apply(lambda x: x.mean(2))
        expected = panel.resample('M', axis=2).mean()
        tm.assert_panel_equal(result, expected)
Пример #8
0
def test_resample_panel_numpy():
    rng = date_range('1/1/2000', '6/30/2000')
    n = len(rng)

    with catch_warnings(record=True):
        panel = Panel(np.random.randn(3, n, 5),
                      items=['one', 'two', 'three'],
                      major_axis=rng,
                      minor_axis=['a', 'b', 'c', 'd', 'e'])

        result = panel.resample('M', axis=1).apply(lambda x: x.mean(1))
        expected = panel.resample('M', axis=1).mean()
        tm.assert_panel_equal(result, expected)

        panel = panel.swapaxes(1, 2)
        result = panel.resample('M', axis=2).apply(lambda x: x.mean(2))
        expected = panel.resample('M', axis=2).mean()
        tm.assert_panel_equal(result, expected)
Пример #9
0
    def test_resample_panel_numpy(self):
        rng = date_range("1/1/2000", "6/30/2000")
        n = len(rng)

        panel = Panel(
            np.random.randn(3, n, 5),
            items=["one", "two", "three"],
            major_axis=rng,
            minor_axis=["a", "b", "c", "d", "e"],
        )

        result = panel.resample("M", how=lambda x: x.mean(1), axis=1)
        expected = panel.resample("M", how="mean", axis=1)
        tm.assert_panel_equal(result, expected)

        panel = panel.swapaxes(1, 2)
        result = panel.resample("M", how=lambda x: x.mean(2), axis=2)
        expected = panel.resample("M", how="mean", axis=2)
        tm.assert_panel_equal(result, expected)
Пример #10
0
    def test_resample_panel(self):
        rng = date_range('1/1/2000', '6/30/2000')
        n = len(rng)

        panel = Panel(np.random.randn(3, n, 5),
                      items=['one', 'two', 'three'],
                      major_axis=rng,
                      minor_axis=['a', 'b', 'c', 'd', 'e'])

        result = panel.resample('M', axis=1)

        def p_apply(panel, f):
            result = {}
            for item in panel.items:
                result[item] = f(panel[item])
            return Panel(result, items=panel.items)

        expected = p_apply(panel, lambda x: x.resample('M'))
        tm.assert_panel_equal(result, expected)

        panel2 = panel.swapaxes(1, 2)
        result = panel2.resample('M', axis=2)
        expected = p_apply(panel2, lambda x: x.resample('M', axis=1))
        tm.assert_panel_equal(result, expected)
Пример #11
0
    def test_resample_panel(self):
        rng = date_range('1/1/2000', '6/30/2000')
        n = len(rng)

        panel = Panel(np.random.randn(3, n, 5),
                      items=['one', 'two', 'three'],
                      major_axis=rng,
                      minor_axis=['a', 'b', 'c', 'd', 'e'])

        result = panel.resample('M', axis=1)

        def p_apply(panel, f):
            result = {}
            for item in panel.items:
                result[item] = f(panel[item])
            return Panel(result, items=panel.items)

        expected = p_apply(panel, lambda x: x.resample('M'))
        tm.assert_panel_equal(result, expected)

        panel2 = panel.swapaxes(1, 2)
        result = panel2.resample('M', axis=2)
        expected = p_apply(panel2, lambda x: x.resample('M', axis=1))
        tm.assert_panel_equal(result, expected)
Пример #12
0
 def response_as_panel(self, swap=False):
     panel = Panel(self.response)
     if swap:
         panel = panel.swapaxes('items', 'minor')
     return panel
Пример #13
0
 def response_as_panel(self, swap=False):
     panel = Panel(self.response)
     if swap:
         panel = panel.swapaxes('items', 'minor')
     return panel
Пример #14
0
    dict(
        (stk, web.get_data_yahoo(
                  stk,"1/1/2011", "6/1/2014")
     )for stk in ["AAPL","GOOG","MSFT","DELL"])
)
# pdata.to_frame().tail() # [2490 rows x 6 columns]
pdata
# indexes = date, cols = company ticker names
'''
<class 'pandas.core.panel.Panel'>
Dimensions: 4 (items) x 877 (major_axis) x 6 (minor_axis)
Items axis: AAPL to MSFT
Major_axis axis: 2011-01-03 00:00:00 to 2014-05-30 00:00:00
Minor_axis axis: Open to Adj Close
'''
pdata=pdata.swapaxes("items","minor")
pdata["Adj Close"].tail()
'''
             AAPL  DELL    GOOG   MSFT
Date
2014-05-23  86.58   NaN  552.70  39.34
2014-05-27  88.20   NaN  565.95  39.41
2014-05-28  87.97   NaN  561.68  39.23
2014-05-29  89.58   NaN  560.08  39.56
2014-05-30  89.24   NaN  559.89  40.15
'''
pdata.ix[:,"2013-02-15",:]
# select all data at specific date or
# range of dates
'''
Open    High     Low   Close    Volume  Adj Close
Пример #15
0
 def response_as_panel(self, swap=False):
     panel = Panel(self.response)
     if swap:
         panel = panel.swapaxes("items", "minor")
     return panel
Пример #16
0
    def __init__(self, x, var_name='x', convert_dummies=True, drop_first=True):
        self._var_name = var_name
        self._convert_dummies = convert_dummies
        self._drop_first = drop_first
        if isinstance(x, PanelData):
            x = x.dataframe
        self._original = x

        if isinstance(x, DataArray):
            if x.ndim not in (2, 3):
                raise ValueError('Only 2-d or 3-d DataArrays are supported')
            x = x.to_pandas()

        if isinstance(x, Series) and isinstance(x.index, pd.MultiIndex):
            x = DataFrame(x)
        elif isinstance(x, Series):
            raise ValueError(
                'Series can only be used with a 2-level MultiIndex')

        if isinstance(x, (Panel, DataFrame)):
            if isinstance(x, DataFrame):
                if isinstance(x.index, pd.MultiIndex):
                    if len(x.index.levels) != 2:
                        raise ValueError('DataFrame input must have a '
                                         'MultiIndex with 2 levels')
                    self._frame = x.copy()
                else:
                    self._frame = DataFrame(
                        {var_name: x.T.stack(dropna=False)})
            else:
                self._frame = x.swapaxes(1,
                                         2).to_frame(filter_observations=False)
        elif isinstance(x, ndarray):
            if not 2 <= x.ndim <= 3:
                raise ValueError('2 or 3-d array required for numpy input')
            if x.ndim == 2:
                x = x[None, :, :]

            k, t, n = x.shape
            variables = [var_name] if k == 1 else [
                var_name + '.{0}'.format(i) for i in range(k)
            ]
            entities = ['entity.{0}'.format(i) for i in range(n)]
            time = list(range(t))
            x = x.astype(np.float64)
            panel = Panel(x,
                          items=variables,
                          major_axis=time,
                          minor_axis=entities)
            self._frame = panel.swapaxes(1,
                                         2).to_frame(filter_observations=False)
        else:
            raise TypeError('Only ndarrays, DataFrames, Panels or DataArrays '
                            'supported.')
        if convert_dummies:
            self._frame = expand_categoricals(self._frame, drop_first)
            self._frame = self._frame.astype(np.float64)

        time_index = Series(self._frame.index.levels[1])
        if not (is_numeric_dtype(time_index.dtype)
                or is_datetime64_any_dtype(time_index.dtype)):
            raise ValueError('The index on the time dimension must be either '
                             'numeric or date-like')
        self._k, self._t, self._n = self.panel.shape
        self._frame.index.levels[0].name = 'entity'
        self._frame.index.levels[1].name = 'time'
Пример #17
0
# -*- coding: utf-8 -*-

import numpy as np
import pandas as pd
import pandas_datareader.data as web
from pandas import Series, DataFrame, Index, Panel

pdata = Panel(
    dict((stk, web.get_data_yahoo(stk, '1/1/2016', '1/15/2016'))
         for stk in ['AAPL', 'GOOG', 'BIDU', 'MSFT']))
print(pdata)
pdata = pdata.swapaxes('items', 'minor')
print(pdata)
print()

print("## access order: Item -> Major -> Minor:")
print(pdata['Adj Close'])
print(pdata[:, '1/5/2016', :])  # items as columns
print(pdata['Adj Close', '1/6/2016', :])
print()

print("## conversion between Panel and DataFrame:")
print("""### items as columns;
    major, and minor as hierarchical index:""")
stacked1 = pdata.ix[:, '1/7/2016':, :].to_frame()
print("stacked1 ==>")
print(stacked1)
print()

stacked2 = pdata.ix[:, '1/7/2016':, 0:1].to_frame()
#stacked2 = pdata.ix[:, '1/7/2016':, 0].to_frame()  # Error, not interval
Пример #18
0
# -*- coding: utf-8 -*- 

import numpy as np
import pandas as pd
import pandas.io.data as web
from pandas import Series, DataFrame, Index, Panel

pdata = Panel(dict((stk, web.get_data_yahoo(stk, '1/1/2016', '1/15/2016')) for stk in ['AAPL', 'GOOG', 'BIDU', 'MSFT']))
print pdata
pdata = pdata.swapaxes('items', 'minor')
print pdata
print

print "访问顺序:# Item -> Major -> Minor"
print pdata['Adj Close']
print pdata[:, '1/5/2016', :]
print pdata['Adj Close', '1/6/2016', :]
print

print 'Panel与DataFrame相互转换'
stacked = pdata.ix[:, '1/7/2016':, :].to_frame()
print stacked
print stacked.to_panel()