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:]))
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
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:]))
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)
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)
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)
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)
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
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
def _prepare_data(data): """Converts the given data into a WidePanel.""" if isinstance(data, WidePanel): return data return WidePanel.fromDict(data)
def _prep_panel_data(data): """Converts the given data into a WidePanel.""" if isinstance(data, WidePanel): return data return WidePanel.fromDict(data)