Python pandas.core.panel.Panel.from_dict() Examples

The following are 30 code examples of pandas.core.panel.Panel.from_dict(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module pandas.core.panel.Panel , or try the search function .
Example #1
Source File: test_panel.py    From predictive-maintenance-using-machine-learning with Apache License 2.0 5 votes vote down vote up
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')

        assert panel['foo'].values.dtype == np.object_
        assert panel['A'].values.dtype == np.float64 
Example #2
Source File: test_panel.py    From twitter-stock-recommendation with MIT License 5 votes vote down vote up
def test_shift(self):
        with catch_warnings(record=True):
            # major
            idx = self.panel.major_axis[0]
            idx_lag = self.panel.major_axis[1]
            shifted = self.panel.shift(1)
            assert_frame_equal(self.panel.major_xs(idx),
                               shifted.major_xs(idx_lag))

            # minor
            idx = self.panel.minor_axis[0]
            idx_lag = self.panel.minor_axis[1]
            shifted = self.panel.shift(1, axis='minor')
            assert_frame_equal(self.panel.minor_xs(idx),
                               shifted.minor_xs(idx_lag))

            # items
            idx = self.panel.items[0]
            idx_lag = self.panel.items[1]
            shifted = self.panel.shift(1, axis='items')
            assert_frame_equal(self.panel[idx], shifted[idx_lag])

            # negative numbers, #2164
            result = self.panel.shift(-1)
            expected = Panel({i: f.shift(-1)[:-1]
                              for i, f in self.panel.iteritems()})
            assert_panel_equal(result, expected)

            # 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) 
Example #3
Source File: test_panel.py    From twitter-stock-recommendation with MIT License 5 votes vote down vote up
def test_from_dict_mixed_orient(self):
        with catch_warnings(record=True):
            df = tm.makeDataFrame()
            df['foo'] = 'bar'

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

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

            assert panel['foo'].values.dtype == np.object_
            assert panel['A'].values.dtype == np.float64 
Example #4
Source File: test_panel.py    From twitter-stock-recommendation with MIT License 5 votes vote down vote up
def test_ctor_orderedDict(self):
        with catch_warnings(record=True):
            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 
Example #5
Source File: test_panel.py    From coffeegrindsize with MIT License 5 votes vote down vote up
def test_shift(self):
        # major
        idx = self.panel.major_axis[0]
        idx_lag = self.panel.major_axis[1]
        shifted = self.panel.shift(1)
        assert_frame_equal(self.panel.major_xs(idx),
                           shifted.major_xs(idx_lag))

        # minor
        idx = self.panel.minor_axis[0]
        idx_lag = self.panel.minor_axis[1]
        shifted = self.panel.shift(1, axis='minor')
        assert_frame_equal(self.panel.minor_xs(idx),
                           shifted.minor_xs(idx_lag))

        # items
        idx = self.panel.items[0]
        idx_lag = self.panel.items[1]
        shifted = self.panel.shift(1, axis='items')
        assert_frame_equal(self.panel[idx], shifted[idx_lag])

        # negative numbers, #2164
        result = self.panel.shift(-1)
        expected = Panel({i: f.shift(-1)[:-1]
                          for i, f in self.panel.iteritems()})
        assert_panel_equal(result, expected)

        # 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) 
Example #6
Source File: test_panel.py    From coffeegrindsize with MIT License 5 votes vote down vote up
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')

        assert panel['foo'].values.dtype == np.object_
        assert panel['A'].values.dtype == np.float64 
Example #7
Source File: test_panel.py    From coffeegrindsize with MIT License 5 votes vote down vote up
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 
Example #8
Source File: test_panel.py    From elasticintel with GNU General Public License v3.0 5 votes vote down vote up
def test_shift(self):
        with catch_warnings(record=True):
            # major
            idx = self.panel.major_axis[0]
            idx_lag = self.panel.major_axis[1]
            shifted = self.panel.shift(1)
            assert_frame_equal(self.panel.major_xs(idx),
                               shifted.major_xs(idx_lag))

            # minor
            idx = self.panel.minor_axis[0]
            idx_lag = self.panel.minor_axis[1]
            shifted = self.panel.shift(1, axis='minor')
            assert_frame_equal(self.panel.minor_xs(idx),
                               shifted.minor_xs(idx_lag))

            # items
            idx = self.panel.items[0]
            idx_lag = self.panel.items[1]
            shifted = self.panel.shift(1, axis='items')
            assert_frame_equal(self.panel[idx], shifted[idx_lag])

            # negative numbers, #2164
            result = self.panel.shift(-1)
            expected = Panel(dict((i, f.shift(-1)[:-1])
                                  for i, f in self.panel.iteritems()))
            assert_panel_equal(result, expected)

            # 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) 
Example #9
Source File: test_panel.py    From elasticintel with GNU General Public License v3.0 5 votes vote down vote up
def test_from_dict_mixed_orient(self):
        with catch_warnings(record=True):
            df = tm.makeDataFrame()
            df['foo'] = 'bar'

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

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

            assert panel['foo'].values.dtype == np.object_
            assert panel['A'].values.dtype == np.float64 
Example #10
Source File: test_panel.py    From elasticintel with GNU General Public License v3.0 5 votes vote down vote up
def test_ctor_orderedDict(self):
        with catch_warnings(record=True):
            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 
Example #11
Source File: test_panel.py    From predictive-maintenance-using-machine-learning with Apache License 2.0 5 votes vote down vote up
def test_shift(self):
        # major
        idx = self.panel.major_axis[0]
        idx_lag = self.panel.major_axis[1]
        shifted = self.panel.shift(1)
        assert_frame_equal(self.panel.major_xs(idx),
                           shifted.major_xs(idx_lag))

        # minor
        idx = self.panel.minor_axis[0]
        idx_lag = self.panel.minor_axis[1]
        shifted = self.panel.shift(1, axis='minor')
        assert_frame_equal(self.panel.minor_xs(idx),
                           shifted.minor_xs(idx_lag))

        # items
        idx = self.panel.items[0]
        idx_lag = self.panel.items[1]
        shifted = self.panel.shift(1, axis='items')
        assert_frame_equal(self.panel[idx], shifted[idx_lag])

        # negative numbers, #2164
        result = self.panel.shift(-1)
        expected = Panel({i: f.shift(-1)[:-1]
                          for i, f in self.panel.iteritems()})
        assert_panel_equal(result, expected)

        # 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) 
Example #12
Source File: test_panel.py    From predictive-maintenance-using-machine-learning with Apache License 2.0 5 votes vote down vote up
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 
Example #13
Source File: test_panel.py    From recruit with Apache License 2.0 5 votes vote down vote up
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 
Example #14
Source File: ols.py    From Computable with MIT License 5 votes vote down vote up
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) 
Example #15
Source File: test_panel.py    From recruit with Apache License 2.0 5 votes vote down vote up
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')

        assert panel['foo'].values.dtype == np.object_
        assert panel['A'].values.dtype == np.float64 
Example #16
Source File: panel.py    From Computable with MIT License 5 votes vote down vote up
def from_dict(cls, data):
        """
        Analogous to Panel.from_dict
        """
        return SparsePanel(data) 
Example #17
Source File: test_panel.py    From vnpy_crypto with MIT License 5 votes vote down vote up
def test_shift(self):
        with catch_warnings(record=True):
            # major
            idx = self.panel.major_axis[0]
            idx_lag = self.panel.major_axis[1]
            shifted = self.panel.shift(1)
            assert_frame_equal(self.panel.major_xs(idx),
                               shifted.major_xs(idx_lag))

            # minor
            idx = self.panel.minor_axis[0]
            idx_lag = self.panel.minor_axis[1]
            shifted = self.panel.shift(1, axis='minor')
            assert_frame_equal(self.panel.minor_xs(idx),
                               shifted.minor_xs(idx_lag))

            # items
            idx = self.panel.items[0]
            idx_lag = self.panel.items[1]
            shifted = self.panel.shift(1, axis='items')
            assert_frame_equal(self.panel[idx], shifted[idx_lag])

            # negative numbers, #2164
            result = self.panel.shift(-1)
            expected = Panel({i: f.shift(-1)[:-1]
                              for i, f in self.panel.iteritems()})
            assert_panel_equal(result, expected)

            # 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) 
Example #18
Source File: test_panel.py    From vnpy_crypto with MIT License 5 votes vote down vote up
def test_from_dict_mixed_orient(self):
        with catch_warnings(record=True):
            df = tm.makeDataFrame()
            df['foo'] = 'bar'

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

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

            assert panel['foo'].values.dtype == np.object_
            assert panel['A'].values.dtype == np.float64 
Example #19
Source File: test_panel.py    From vnpy_crypto with MIT License 5 votes vote down vote up
def test_ctor_orderedDict(self):
        with catch_warnings(record=True):
            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 
Example #20
Source File: test_panel.py    From recruit with Apache License 2.0 5 votes vote down vote up
def test_shift(self):
        # major
        idx = self.panel.major_axis[0]
        idx_lag = self.panel.major_axis[1]
        shifted = self.panel.shift(1)
        assert_frame_equal(self.panel.major_xs(idx),
                           shifted.major_xs(idx_lag))

        # minor
        idx = self.panel.minor_axis[0]
        idx_lag = self.panel.minor_axis[1]
        shifted = self.panel.shift(1, axis='minor')
        assert_frame_equal(self.panel.minor_xs(idx),
                           shifted.minor_xs(idx_lag))

        # items
        idx = self.panel.items[0]
        idx_lag = self.panel.items[1]
        shifted = self.panel.shift(1, axis='items')
        assert_frame_equal(self.panel[idx], shifted[idx_lag])

        # negative numbers, #2164
        result = self.panel.shift(-1)
        expected = Panel({i: f.shift(-1)[:-1]
                          for i, f in self.panel.iteritems()})
        assert_panel_equal(result, expected)

        # 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) 
Example #21
Source File: test_panel.py    From recruit with Apache License 2.0 4 votes vote down vote up
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

        # TODO: unused?
        wp3 = Panel.from_dict(d3)  # noqa

        tm.assert_index_equal(wp.major_axis, self.panel.major_axis)
        assert_panel_equal(wp, wp2)

        # intersect
        wp = Panel.from_dict(d, intersect=True)
        tm.assert_index_equal(wp.major_axis, 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}

        # TODO: unused?
        wp4 = Panel.from_dict(d4)  # noqa

        assert_panel_equal(Panel(d4), Panel(items=['A', 'B']))

        # cast
        dcasted = {k: v.reindex(wp.major_axis).fillna(0)
                   for k, v in compat.iteritems(d)}
        result = Panel(dcasted, dtype=int)
        expected = Panel({k: v.astype(int)
                          for k, v in compat.iteritems(dcasted)})
        assert_panel_equal(result, expected)

        result = Panel(dcasted, dtype=np.int32)
        expected = Panel({k: v.astype(np.int32)
                          for k, v in compat.iteritems(dcasted)})
        assert_panel_equal(result, expected) 
Example #22
Source File: plm.py    From Computable with MIT License 4 votes vote down vote up
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 
Example #23
Source File: test_panel.py    From twitter-stock-recommendation with MIT License 4 votes vote down vote up
def test_ctor_dict(self):
        with catch_warnings(record=True):
            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

            # TODO: unused?
            wp3 = Panel.from_dict(d3)  # noqa

            tm.assert_index_equal(wp.major_axis, self.panel.major_axis)
            assert_panel_equal(wp, wp2)

            # intersect
            wp = Panel.from_dict(d, intersect=True)
            tm.assert_index_equal(wp.major_axis, 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}

            # TODO: unused?
            wp4 = Panel.from_dict(d4)  # noqa

            assert_panel_equal(Panel(d4), Panel(items=['A', 'B']))

            # cast
            dcasted = {k: v.reindex(wp.major_axis).fillna(0)
                       for k, v in compat.iteritems(d)}
            result = Panel(dcasted, dtype=int)
            expected = Panel({k: v.astype(int)
                              for k, v in compat.iteritems(dcasted)})
            assert_panel_equal(result, expected)

            result = Panel(dcasted, dtype=np.int32)
            expected = Panel({k: v.astype(np.int32)
                              for k, v in compat.iteritems(dcasted)})
            assert_panel_equal(result, expected) 
Example #24
Source File: test_panel.py    From twitter-stock-recommendation with MIT License 4 votes vote down vote up
def test_constructor_dtypes(self):
        with catch_warnings(record=True):
            # 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) 
Example #25
Source File: test_panel.py    From vnpy_crypto with MIT License 4 votes vote down vote up
def test_constructor_dtypes(self):
        with catch_warnings(record=True):
            # 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) 
Example #26
Source File: test_panel.py    From vnpy_crypto with MIT License 4 votes vote down vote up
def test_ctor_dict(self):
        with catch_warnings(record=True):
            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

            # TODO: unused?
            wp3 = Panel.from_dict(d3)  # noqa

            tm.assert_index_equal(wp.major_axis, self.panel.major_axis)
            assert_panel_equal(wp, wp2)

            # intersect
            wp = Panel.from_dict(d, intersect=True)
            tm.assert_index_equal(wp.major_axis, 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}

            # TODO: unused?
            wp4 = Panel.from_dict(d4)  # noqa

            assert_panel_equal(Panel(d4), Panel(items=['A', 'B']))

            # cast
            dcasted = {k: v.reindex(wp.major_axis).fillna(0)
                       for k, v in compat.iteritems(d)}
            result = Panel(dcasted, dtype=int)
            expected = Panel({k: v.astype(int)
                              for k, v in compat.iteritems(dcasted)})
            assert_panel_equal(result, expected)

            result = Panel(dcasted, dtype=np.int32)
            expected = Panel({k: v.astype(np.int32)
                              for k, v in compat.iteritems(dcasted)})
            assert_panel_equal(result, expected) 
Example #27
Source File: test_panel.py    From coffeegrindsize with MIT License 4 votes vote down vote up
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

        # TODO: unused?
        wp3 = Panel.from_dict(d3)  # noqa

        tm.assert_index_equal(wp.major_axis, self.panel.major_axis)
        assert_panel_equal(wp, wp2)

        # intersect
        wp = Panel.from_dict(d, intersect=True)
        tm.assert_index_equal(wp.major_axis, 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}

        # TODO: unused?
        wp4 = Panel.from_dict(d4)  # noqa

        assert_panel_equal(Panel(d4), Panel(items=['A', 'B']))

        # cast
        dcasted = {k: v.reindex(wp.major_axis).fillna(0)
                   for k, v in compat.iteritems(d)}
        result = Panel(dcasted, dtype=int)
        expected = Panel({k: v.astype(int)
                          for k, v in compat.iteritems(dcasted)})
        assert_panel_equal(result, expected)

        result = Panel(dcasted, dtype=np.int32)
        expected = Panel({k: v.astype(np.int32)
                          for k, v in compat.iteritems(dcasted)})
        assert_panel_equal(result, expected) 
Example #28
Source File: test_panel.py    From coffeegrindsize with MIT License 4 votes vote down vote up
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) 
Example #29
Source File: test_panel.py    From elasticintel with GNU General Public License v3.0 4 votes vote down vote up
def test_ctor_dict(self):
        with catch_warnings(record=True):
            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

            # TODO: unused?
            wp3 = Panel.from_dict(d3)  # noqa

            tm.assert_index_equal(wp.major_axis, self.panel.major_axis)
            assert_panel_equal(wp, wp2)

            # intersect
            wp = Panel.from_dict(d, intersect=True)
            tm.assert_index_equal(wp.major_axis, 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}

            # TODO: unused?
            wp4 = Panel.from_dict(d4)  # noqa

            assert_panel_equal(Panel(d4), Panel(items=['A', 'B']))

            # cast
            dcasted = dict((k, v.reindex(wp.major_axis).fillna(0))
                           for k, v in compat.iteritems(d))
            result = Panel(dcasted, dtype=int)
            expected = Panel(dict((k, v.astype(int))
                                  for k, v in compat.iteritems(dcasted)))
            assert_panel_equal(result, expected)

            result = Panel(dcasted, dtype=np.int32)
            expected = Panel(dict((k, v.astype(np.int32))
                                  for k, v in compat.iteritems(dcasted)))
            assert_panel_equal(result, expected) 
Example #30
Source File: test_panel.py    From elasticintel with GNU General Public License v3.0 4 votes vote down vote up
def test_constructor_dtypes(self):
        with catch_warnings(record=True):
            # 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)