예제 #1
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()))
예제 #2
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:]))
예제 #3
0
파일: plm.py 프로젝트: GunioRobot/pandas
    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