Ejemplo n.º 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": None, "B": DataFrame(itemb[5:]._series), "C": DataFrame(itema._series)}

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

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

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

        # a pathological case
        d4 = {"A": None, "B": None}
        wp4 = Panel.from_dict(d4)
        assert_panel_equal(Panel(d4), Panel(items=["A", "B"]))

        # cast
        dcasted = dict((k, v.reindex(wp.major_axis).fillna(0)) for k, v in d.iteritems())
        result = Panel(dcasted, dtype=int)
        expected = Panel(dict((k, v.astype(int)) for k, v in dcasted.iteritems()))
        assert_panel_equal(result, expected)
Ejemplo n.º 2
0
 def test_shift(self):
     # mixed dtypes #6959
     data = [('item ' + ch, makeMixedDataFrame()) for ch in list('abcde')]
     data = dict(data)
     mixed_panel = Panel.from_dict(data, orient='minor')
     shifted = mixed_panel.shift(1)
     assert_series_equal(mixed_panel.dtypes, shifted.dtypes)
Ejemplo n.º 3
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 = Panel.from_dict(d)
        wp2 = Panel.from_dict(d2) # nested Dict
        wp3 = Panel.from_dict(d3)
        self.assert_(wp.major_axis.equals(self.panel.major_axis))
        assert_panel_equal(wp, wp2)

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

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

        # cast
        result = Panel(d, dtype=int)
        expected = Panel(dict((k, v.astype(int)) for k, v in d.iteritems()))
Ejemplo n.º 4
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 = Panel.from_dict(d)
        wp2 = Panel.from_dict(d2)  # nested Dict
        wp3 = Panel.from_dict(d3)
        self.assert_(wp.major_axis.equals(self.panel.major_axis))
        assert_panel_equal(wp, wp2)

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

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

        # cast
        result = Panel(d, dtype=int)
        expected = Panel(dict((k, v.astype(int)) for k, v in d.iteritems()))
Ejemplo n.º 5
0
    def test_ctor_orderedDict(self):
        keys = list(set(np.random.randint(
            0, 5000, 100)))[:50]  # unique random int  keys
        d = OrderedDict([(k, mkdf(10, 5)) for k in keys])
        p = Panel(d)
        assert list(p.items) == keys

        p = Panel.from_dict(d)
        assert list(p.items) == keys
Ejemplo n.º 6
0
    def var_beta(self):
        """Returns the covariance of beta."""
        result = {}
        result_index = self._result_index
        for i in range(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 Panel.from_dict(result, intersect=False)
Ejemplo n.º 7
0
    def test_from_dict_mixed_orient(self):
        df = tm.makeDataFrame()
        df["foo"] = "bar"

        data = {"k1": df, "k2": df}

        panel = Panel.from_dict(data, orient="minor")

        self.assert_(panel["foo"].values.dtype == np.object_)
        self.assert_(panel["A"].values.dtype == np.float64)
Ejemplo n.º 8
0
    def test_from_dict_mixed_orient(self):
        df = tm.makeDataFrame()
        df['foo'] = 'bar'

        data = {'k1': df, 'k2': df}

        panel = Panel.from_dict(data, orient='minor')

        self.assert_(panel['foo'].values.dtype == np.object_)
        self.assert_(panel['A'].values.dtype == np.float64)
Ejemplo 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 = DataFrame(self._var_beta_raw[i], columns=self.beta.columns,
                           index=self.beta.columns)
            result[result_index[i]] = dm

        return Panel.from_dict(result, intersect=False)
Ejemplo n.º 10
0
    def test_from_dict_mixed_orient(self):
        df = tm.makeDataFrame()
        df['foo'] = 'bar'

        data = {'k1' : df,
                'k2' : df}

        panel = Panel.from_dict(data, orient='minor')

        self.assert_(panel['foo'].values.dtype == np.object_)
        self.assert_(panel['A'].values.dtype == np.float64)
Ejemplo n.º 11
0
Archivo: plm.py Proyecto: MLnick/pandas
    def _filter_data(self):
        """

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

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

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

            if not isinstance(data, Panel):
                data = Panel.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['__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
Ejemplo n.º 12
0
    def test_constructor_dtypes(self):
        # GH #797

        def _check_dtype(panel, dtype):
            for i in panel.items:
                assert panel[i].values.dtype.name == dtype

        # only nan holding types allowed here
        for dtype in ['float64', 'float32', 'object']:
            panel = Panel(items=lrange(2),
                          major_axis=lrange(10),
                          minor_axis=lrange(5),
                          dtype=dtype)
            _check_dtype(panel, dtype)

        for dtype in ['float64', 'float32', 'int64', 'int32', 'object']:
            panel = Panel(np.array(np.random.randn(2, 10, 5), dtype=dtype),
                          items=lrange(2),
                          major_axis=lrange(10),
                          minor_axis=lrange(5),
                          dtype=dtype)
            _check_dtype(panel, dtype)

        for dtype in ['float64', 'float32', 'int64', 'int32', 'object']:
            panel = Panel(np.array(np.random.randn(2, 10, 5), dtype='O'),
                          items=lrange(2),
                          major_axis=lrange(10),
                          minor_axis=lrange(5),
                          dtype=dtype)
            _check_dtype(panel, dtype)

        for dtype in ['float64', 'float32', 'int64', 'int32', 'object']:
            panel = Panel(np.random.randn(2, 10, 5),
                          items=lrange(2),
                          major_axis=lrange(10),
                          minor_axis=lrange(5),
                          dtype=dtype)
            _check_dtype(panel, dtype)

        for dtype in ['float64', 'float32', 'int64', 'int32', 'object']:
            df1 = DataFrame(np.random.randn(2, 5),
                            index=lrange(2),
                            columns=lrange(5))
            df2 = DataFrame(np.random.randn(2, 5),
                            index=lrange(2),
                            columns=lrange(5))
            panel = Panel.from_dict({'a': df1, 'b': df2}, dtype=dtype)
            _check_dtype(panel, dtype)
Ejemplo n.º 13
0
    def _filter_data(self):
        """

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

        if isinstance(data, DataFrame):
            data = data.to_panel()
        else:
            if isinstance(data, Panel):
                data = data.copy()

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

            if not isinstance(data, Panel):
                data = Panel.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_frame()

        # Filter all data together using to_frame

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

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

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

        if self._weights is not None and not self._weights.empty:
            weights = data_long['__weights__']
        else:
            weights = None

        return x, x_filt, y, weights, cat_mapping