Exemplo n.º 1
0
    def test_delitem_and_pop(self):
        expected = self.panel['ItemA']
        result = self.panel.pop('ItemA')
        assert_frame_equal(expected, result)
        self.assert_('ItemA' not in self.panel.items)

        del self.panel['ItemB']
        self.assert_('ItemB' not in self.panel.items)
        self.assertRaises(Exception, self.panel.__delitem__, 'ItemB')

        values = np.empty((3, 3, 3))
        values[0] = 0
        values[1] = 1
        values[2] = 2

        panel = WidePanel(values, range(3), range(3), range(3))

        # did we delete the right row?

        panelc = panel.copy()
        del panelc[0]
        assert_frame_equal(panelc[1], panel[1])
        assert_frame_equal(panelc[2], panel[2])

        panelc = panel.copy()
        del panelc[1]
        assert_frame_equal(panelc[0], panel[0])
        assert_frame_equal(panelc[2], panel[2])

        panelc = panel.copy()
        del panelc[2]
        assert_frame_equal(panelc[1], panel[1])
        assert_frame_equal(panelc[0], panel[0])
Exemplo n.º 2
0
    def test_fromDict(self):
        itema = self.panel['ItemA']
        itemb = self.panel['ItemB']

        d = {'A' : itema, 'B' : itemb[5:]}

        wp = WidePanel.fromDict(d)
        self.assert_(wp.major_axis.equals(self.panel.major_axis))

        # intersect
        wp = WidePanel.fromDict(d, intersect=True)
        self.assert_(wp.major_axis.equals(itemb.index[5:]))
Exemplo n.º 3
0
    def aggregate(self, func):
        """
        For given DataFrame, group index by given mapper function or dict, take
        the sub-DataFrame (reindex) for this group and call apply(func)
        on this sub-DataFrame. Return a DataFrame of the results for each
        key.

        Parameters
        ----------
        mapper : function, dict-like, or string
            Mapping or mapping function. If string given, must be a column
            name in the frame
        func : function
            Function to use for aggregating groups

        N.B.: func must produce one value from a Series, otherwise
        an error will occur.

        Optional: provide set mapping as dictionary
        """
        axis_name = self.obj._get_axis_name(self.axis)
        getter = lambda p, group: p.reindex(**{axis_name : group})
        result_d = self._aggregate_generic(getter, func,
                                           axis=self.axis)

        result = WidePanel.fromDict(result_d, intersect=False)

        if self.axis > 0:
            result = result.swapaxes(0, self.axis)

        return result
Exemplo n.º 4
0
 def forecast(self, h):
     """
     Returns the forecasts at 1, 2, ..., n timesteps in the future.
     """
     forecast = self._forecast_raw(h).T.swapaxes(1, 2)
     index = xrange(1, 1 + h)
     w = WidePanel(forecast, self._data.items, index, self._data.minor_axis)
     return w
Exemplo n.º 5
0
    def test_ctor_dict(self):
        itema = self.panel['ItemA']
        itemb = self.panel['ItemB']

        d = {'A' : itema, 'B' : itemb[5:]}
        d2 = {'A' : itema._series, 'B' : itemb[5:]._series}
        d3 = {'A' : DataFrame(itema._series),
              'B' : DataFrame(itemb[5:]._series)}

        wp = WidePanel.from_dict(d)
        wp2 = WidePanel.from_dict(d2) # nested Dict
        wp3 = WidePanel.from_dict(d3)
        self.assert_(wp.major_axis.equals(self.panel.major_axis))
        assert_panel_equal(wp, wp2)

        # intersect
        wp = WidePanel.from_dict(d, intersect=True)
        self.assert_(wp.major_axis.equals(itemb.index[5:]))

        # use constructor
        assert_panel_equal(WidePanel(d), WidePanel.from_dict(d))
        assert_panel_equal(WidePanel(d2), WidePanel.from_dict(d2))
        assert_panel_equal(WidePanel(d3), WidePanel.from_dict(d3))

        # cast
        result = WidePanel(d, dtype=int)
        expected = WidePanel(dict((k, v.astype(int)) for k, v in d.iteritems()))
Exemplo n.º 6
0
    def test_from_dict(self):
        itema = self.panel['ItemA']
        itemb = self.panel['ItemB']

        d = {'A' : itema, 'B' : itemb[5:]}
        d2 = {'A' : itema._series, 'B' : itemb[5:]._series}
        d3 = {'A' : DataFrame(itema._series),
              'B' : DataFrame(itemb[5:]._series)}

        wp = WidePanel.from_dict(d)
        wp2 = WidePanel.from_dict(d2) # nested Dict
        wp3 = WidePanel.from_dict(d3)
        self.assert_(wp.major_axis.equals(self.panel.major_axis))
        assert_panel_equal(wp, wp2)

        # intersect
        wp = WidePanel.from_dict(d, intersect=True)
        self.assert_(wp.major_axis.equals(itemb.index[5:]))
Exemplo n.º 7
0
    def test_fromDict(self):
        itema = self.panel['ItemA']
        itemb = self.panel['ItemB']

        d = {'A' : itema, 'B' : itemb[5:]}
        d2 = {'A' : itema._series, 'B' : itemb[5:]._series}
        d3 = {'A' : DataFrame(itema._series),
              'B' : DataFrame(itemb[5:]._series)}

        wp = WidePanel.fromDict(d)
        wp2 = WidePanel.fromDict(d2) # nested Dict
        wp3 = WidePanel.fromDict(d3)
        self.assert_(wp.major_axis.equals(self.panel.major_axis))
        assert_panel_equal(wp, wp2)

        # intersect
        wp = WidePanel.fromDict(d, intersect=True)
        self.assert_(wp.major_axis.equals(itemb.index[5:]))
Exemplo n.º 8
0
    def var_beta(self):
        """Returns the covariance of beta."""
        result = {}
        result_index = self._result_index
        for i in xrange(len(self._var_beta_raw)):
            dm = DataFrame(self._var_beta_raw[i], columns=self.beta.columns,
                           index=self.beta.columns)
            result[result_index[i]] = dm

        return WidePanel.fromDict(result, intersect=False)
Exemplo n.º 9
0
    def var_beta(self):
        """Returns the covariance of beta."""
        result = {}
        result_index = self._result_index
        for i in xrange(len(self._var_beta_raw)):
            dm = DataMatrix(self._var_beta_raw[i],
                            columns=self.beta.cols(),
                            index=self.beta.cols())
            result[result_index[i]] = dm

        return WidePanel.fromDict(result, intersect=False)
Exemplo n.º 10
0
    def _filter_data(self):
        """

        """
        data = self._x_orig
        cat_mapping = {}

        if isinstance(data, LongPanel):
            data = data.to_wide()
        else:
            if isinstance(data, WidePanel):
                data = data.copy()

            if not isinstance(data, SparseWidePanel):
                data, cat_mapping = self._convert_x(data)

            if not isinstance(data, WidePanel):
                data = WidePanel.from_dict(data, intersect=True)

        x_names = data.items

        if self._weights is not None:
            data['__weights__'] = self._weights

        # Filter x's without y (so we can make a prediction)
        filtered = data.to_long()

        # Filter all data together using to_long

        # convert to DataFrame
        y = self._y_orig
        if isinstance(y, Series):
            y = y.unstack()

        data['__y__'] = y
        data_long = data.to_long()

        x_filt = filtered.filter(x_names)

        if self._weights:
            weights_filt = filtered.ix[:, ['__weights__']]
        else:
            weights_filt = None

        x = data_long.filter(x_names)
        y = data_long.ix[:, ['__y__']]

        if self._weights:
            weights = data_long.ix[:, ['__weights__']]
        else:
            weights = None

        return x, x_filt, y, weights, weights_filt, cat_mapping
Exemplo n.º 11
0
    def resid(self):
        """
        Returns the DataFrame containing the residuals of the VAR regressions.
        Each column x1 contains the residuals generated by regressing the x1
        column of the input against the lagged input.

        Returns
        -------
        DataFrame
        """
        d = dict([(key, value.resid) for (key, value) in self.ols_results.iteritems()])
        return WidePanel.fromDict(d)
Exemplo n.º 12
0
    def resid(self):
        """
        Returns the DataMatrix containing the residuals of the VAR regressions.
        Each column x1 contains the residuals generated by regressing the x1
        column of the input against the lagged input.

        Returns
        -------
        DataMatrix
        """
        d = dict([(key, value.resid)
                  for (key, value) in self.ols_results.iteritems()])
        return WidePanel.fromDict(d)
Exemplo n.º 13
0
    def _aggregate_generic(self, agger, axis=0):
        result = {}

        obj = self._get_obj_with_exclusions()

        for name in self.primary:
            data = self.get_group(name, obj=obj)
            try:
                result[name] = agger(data)
            except Exception:
                result[name] = data.apply(agger, axis=axis)

        result = WidePanel.fromDict(result, intersect=False)

        if axis > 0:
            result = result.swapaxes(0, axis)

        return result
Exemplo n.º 14
0
    def _filter_data(self):
        """

        """
        data = self._x_orig

        if isinstance(data, LongPanel):
            cat_mapping = {}
            data = data.toWide()

        else:
            data, cat_mapping = self._convert_x(data)

            if not isinstance(data, WidePanel):
                data = WidePanel.fromDict(data, intersect=True)

        x_names = data.items

        if self._weights is not None:
            data['__weights__'] = self._weights

        # Filter x's without y (so we can make a prediction)
        filtered = data.toLong()

        # Filter all data together using toLong
        data['__y__'] = self._y_orig
        data_long = data.toLong()

        x_filt = filtered.filter(x_names)

        if self._weights:
            weights_filt = filtered['__weights__']
        else:
            weights_filt = None

        x = data_long.filter(x_names)
        y = data_long['__y__']

        if self._weights:
            weights = data_long['__weights__']
        else:
            weights = None

        return x, x_filt, y, weights, weights_filt, cat_mapping
Exemplo n.º 15
0
    def _filter_data(self):
        """

        """
        data = self._x_orig

        if isinstance(data, LongPanel):
            cat_mapping = {}
            data = data.toWide()

        else:
            data, cat_mapping = self._convert_x(data)

            if not isinstance(data, WidePanel):
                data = WidePanel.fromDict(data, intersect=True)

        x_names = data.items

        if self._weights is not None:
            data['__weights__'] = self._weights

        # Filter x's without y (so we can make a prediction)
        filtered = data.toLong()

        # Filter all data together using toLong
        data['__y__'] = self._y_orig
        data_long = data.toLong()

        x_filt = filtered.filter(x_names)

        if self._weights:
            weights_filt = filtered['__weights__']
        else:
            weights_filt = None

        x = data_long.filter(x_names)
        y = data_long['__y__']

        if self._weights:
            weights = data_long['__weights__']
        else:
            weights = None

        return x, x_filt, y, weights, weights_filt, cat_mapping
Exemplo n.º 16
0
    def _prepare_data(data):
        """Converts the given data into a WidePanel."""
        if isinstance(data, WidePanel):
            return data

        return WidePanel.fromDict(data)
Exemplo n.º 17
0
def _prep_panel_data(data):
    """Converts the given data into a WidePanel."""
    if isinstance(data, WidePanel):
        return data

    return WidePanel.fromDict(data)