Python pandas.util.testing.makePanel() Examples

The following are 30 code examples of pandas.util.testing.makePanel(). 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.util.testing , or try the search function .
Example #1
Source File: test_groupby.py    From recruit with Apache License 2.0 6 votes vote down vote up
def test_sparse_friendly(df):
    sdf = df[['C', 'D']].to_sparse()
    panel = tm.makePanel()
    tm.add_nans(panel)

    def _check_work(gp):
        gp.mean()
        gp.agg(np.mean)
        dict(iter(gp))

    # it works!
    _check_work(sdf.groupby(lambda x: x // 2))
    _check_work(sdf['C'].groupby(lambda x: x // 2))
    _check_work(sdf.groupby(df['A']))

    # do this someday
    # _check_work(panel.groupby(lambda x: x.month, axis=1)) 
Example #2
Source File: test_panel.py    From recruit with Apache License 2.0 6 votes vote down vote up
def test_to_xarray(self):
        from xarray import DataArray

        with catch_warnings(record=True):
            simplefilter("ignore", FutureWarning)
            p = tm.makePanel()

            result = p.to_xarray()
            assert isinstance(result, DataArray)
            assert len(result.coords) == 3
            assert_almost_equal(list(result.coords.keys()),
                                ['items', 'major_axis', 'minor_axis'])
            assert len(result.dims) == 3

            # idempotency
            assert_panel_equal(result.to_pandas(), p)


# run all the tests, but wrap each in a warning catcher 
Example #3
Source File: test_generic.py    From recruit with Apache License 2.0 6 votes vote down vote up
def test_transpose(self):
        msg = (r"transpose\(\) got multiple values for "
               r"keyword argument 'axes'")
        for s in [tm.makeFloatSeries(), tm.makeStringSeries(),
                  tm.makeObjectSeries()]:
            # calls implementation in pandas/core/base.py
            tm.assert_series_equal(s.transpose(), s)
        for df in [tm.makeTimeDataFrame()]:
            tm.assert_frame_equal(df.transpose().transpose(), df)

        with catch_warnings(record=True):
            simplefilter("ignore", FutureWarning)
            for p in [tm.makePanel()]:
                tm.assert_panel_equal(p.transpose(2, 0, 1)
                                      .transpose(1, 2, 0), p)
                with pytest.raises(TypeError, match=msg):
                    p.transpose(2, 0, 1, axes=(2, 0, 1)) 
Example #4
Source File: test_reshape.py    From recruit with Apache License 2.0 6 votes vote down vote up
def test_reshaping_panel_categorical(self):

        p = tm.makePanel()
        p['str'] = 'foo'
        df = p.to_frame()

        df['category'] = df['str'].astype('category')
        result = df['category'].unstack()

        c = Categorical(['foo'] * len(p.major_axis))
        expected = DataFrame({'A': c.copy(),
                              'B': c.copy(),
                              'C': c.copy(),
                              'D': c.copy()},
                             columns=Index(list('ABCD'), name='minor'),
                             index=p.major_axis.set_names('major'))
        tm.assert_frame_equal(result, expected) 
Example #5
Source File: test_generic.py    From recruit with Apache License 2.0 6 votes vote down vote up
def test_numpy_transpose(self):
        msg = "the 'axes' parameter is not supported"

        s = tm.makeFloatSeries()
        tm.assert_series_equal(np.transpose(s), s)

        with pytest.raises(ValueError, match=msg):
            np.transpose(s, axes=1)

        df = tm.makeTimeDataFrame()
        tm.assert_frame_equal(np.transpose(np.transpose(df)), df)

        with pytest.raises(ValueError, match=msg):
            np.transpose(df, axes=1)

        with catch_warnings(record=True):
            simplefilter("ignore", FutureWarning)
            p = tm.makePanel()
            tm.assert_panel_equal(np.transpose(
                np.transpose(p, axes=(2, 0, 1)),
                axes=(1, 2, 0)), p) 
Example #6
Source File: test_generic.py    From recruit with Apache License 2.0 6 votes vote down vote up
def test_take_invalid_kwargs(self):
        indices = [-3, 2, 0, 1]
        s = tm.makeFloatSeries()
        df = tm.makeTimeDataFrame()

        with catch_warnings(record=True):
            simplefilter("ignore", FutureWarning)
            p = tm.makePanel()

        for obj in (s, df, p):
            msg = r"take\(\) got an unexpected keyword argument 'foo'"
            with pytest.raises(TypeError, match=msg):
                obj.take(indices, foo=2)

            msg = "the 'out' parameter is not supported"
            with pytest.raises(ValueError, match=msg):
                obj.take(indices, out=indices)

            msg = "the 'mode' parameter is not supported"
            with pytest.raises(ValueError, match=msg):
                obj.take(indices, mode='clip') 
Example #7
Source File: test_groupby.py    From predictive-maintenance-using-machine-learning with Apache License 2.0 6 votes vote down vote up
def test_sparse_friendly(df):
    sdf = df[['C', 'D']].to_sparse()
    panel = tm.makePanel()
    tm.add_nans(panel)

    def _check_work(gp):
        gp.mean()
        gp.agg(np.mean)
        dict(iter(gp))

    # it works!
    _check_work(sdf.groupby(lambda x: x // 2))
    _check_work(sdf['C'].groupby(lambda x: x // 2))
    _check_work(sdf.groupby(df['A']))

    # do this someday
    # _check_work(panel.groupby(lambda x: x.month, axis=1)) 
Example #8
Source File: test_generic.py    From predictive-maintenance-using-machine-learning with Apache License 2.0 6 votes vote down vote up
def test_take_invalid_kwargs(self):
        indices = [-3, 2, 0, 1]
        s = tm.makeFloatSeries()
        df = tm.makeTimeDataFrame()

        with catch_warnings(record=True):
            simplefilter("ignore", FutureWarning)
            p = tm.makePanel()

        for obj in (s, df, p):
            msg = r"take\(\) got an unexpected keyword argument 'foo'"
            with pytest.raises(TypeError, match=msg):
                obj.take(indices, foo=2)

            msg = "the 'out' parameter is not supported"
            with pytest.raises(ValueError, match=msg):
                obj.take(indices, out=indices)

            msg = "the 'mode' parameter is not supported"
            with pytest.raises(ValueError, match=msg):
                obj.take(indices, mode='clip') 
Example #9
Source File: test_generic.py    From predictive-maintenance-using-machine-learning with Apache License 2.0 6 votes vote down vote up
def test_numpy_transpose(self):
        msg = "the 'axes' parameter is not supported"

        s = tm.makeFloatSeries()
        tm.assert_series_equal(np.transpose(s), s)

        with pytest.raises(ValueError, match=msg):
            np.transpose(s, axes=1)

        df = tm.makeTimeDataFrame()
        tm.assert_frame_equal(np.transpose(np.transpose(df)), df)

        with pytest.raises(ValueError, match=msg):
            np.transpose(df, axes=1)

        with catch_warnings(record=True):
            simplefilter("ignore", FutureWarning)
            p = tm.makePanel()
            tm.assert_panel_equal(np.transpose(
                np.transpose(p, axes=(2, 0, 1)),
                axes=(1, 2, 0)), p) 
Example #10
Source File: test_generic.py    From predictive-maintenance-using-machine-learning with Apache License 2.0 6 votes vote down vote up
def test_transpose(self):
        msg = (r"transpose\(\) got multiple values for "
               r"keyword argument 'axes'")
        for s in [tm.makeFloatSeries(), tm.makeStringSeries(),
                  tm.makeObjectSeries()]:
            # calls implementation in pandas/core/base.py
            tm.assert_series_equal(s.transpose(), s)
        for df in [tm.makeTimeDataFrame()]:
            tm.assert_frame_equal(df.transpose().transpose(), df)

        with catch_warnings(record=True):
            simplefilter("ignore", FutureWarning)
            for p in [tm.makePanel()]:
                tm.assert_panel_equal(p.transpose(2, 0, 1)
                                      .transpose(1, 2, 0), p)
                with pytest.raises(TypeError, match=msg):
                    p.transpose(2, 0, 1, axes=(2, 0, 1)) 
Example #11
Source File: test_panel.py    From predictive-maintenance-using-machine-learning with Apache License 2.0 6 votes vote down vote up
def test_to_xarray(self):
        from xarray import DataArray

        with catch_warnings(record=True):
            simplefilter("ignore", FutureWarning)
            p = tm.makePanel()

            result = p.to_xarray()
            assert isinstance(result, DataArray)
            assert len(result.coords) == 3
            assert_almost_equal(list(result.coords.keys()),
                                ['items', 'major_axis', 'minor_axis'])
            assert len(result.dims) == 3

            # idempotency
            assert_panel_equal(result.to_pandas(), p)


# run all the tests, but wrap each in a warning catcher 
Example #12
Source File: test_groupby.py    From vnpy_crypto with MIT License 6 votes vote down vote up
def test_sparse_friendly(df):
    sdf = df[['C', 'D']].to_sparse()
    with catch_warnings(record=True):
        panel = tm.makePanel()
        tm.add_nans(panel)

    def _check_work(gp):
        gp.mean()
        gp.agg(np.mean)
        dict(iter(gp))

    # it works!
    _check_work(sdf.groupby(lambda x: x // 2))
    _check_work(sdf['C'].groupby(lambda x: x // 2))
    _check_work(sdf.groupby(df['A']))

    # do this someday
    # _check_work(panel.groupby(lambda x: x.month, axis=1)) 
Example #13
Source File: test_reshape.py    From predictive-maintenance-using-machine-learning with Apache License 2.0 6 votes vote down vote up
def test_reshaping_panel_categorical(self):

        p = tm.makePanel()
        p['str'] = 'foo'
        df = p.to_frame()

        df['category'] = df['str'].astype('category')
        result = df['category'].unstack()

        c = Categorical(['foo'] * len(p.major_axis))
        expected = DataFrame({'A': c.copy(),
                              'B': c.copy(),
                              'C': c.copy(),
                              'D': c.copy()},
                             columns=Index(list('ABCD'), name='minor'),
                             index=p.major_axis.set_names('major'))
        tm.assert_frame_equal(result, expected) 
Example #14
Source File: test_reshape.py    From vnpy_crypto with MIT License 6 votes vote down vote up
def test_reshaping_panel_categorical(self):

        with catch_warnings(record=True):
            p = tm.makePanel()
            p['str'] = 'foo'
            df = p.to_frame()

        df['category'] = df['str'].astype('category')
        result = df['category'].unstack()

        c = Categorical(['foo'] * len(p.major_axis))
        expected = DataFrame({'A': c.copy(),
                              'B': c.copy(),
                              'C': c.copy(),
                              'D': c.copy()},
                             columns=Index(list('ABCD'), name='minor'),
                             index=p.major_axis.set_names('major'))
        tm.assert_frame_equal(result, expected) 
Example #15
Source File: test_pytables.py    From Computable with MIT License 6 votes vote down vote up
def test_legacy_table_write(self):
        raise nose.SkipTest("skipping for now")

        store = HDFStore(tm.get_data_path('legacy_hdf/legacy_table_%s.h5' % pandas.__version__), 'a')

        df = tm.makeDataFrame()
        wp = tm.makePanel()

        index = MultiIndex(levels=[['foo', 'bar', 'baz', 'qux'],
                                   ['one', 'two', 'three']],
                           labels=[[0, 0, 0, 1, 1, 2, 2, 3, 3, 3],
                                   [0, 1, 2, 0, 1, 1, 2, 0, 1, 2]],
                           names=['foo', 'bar'])
        df = DataFrame(np.random.randn(10, 3), index=index,
                       columns=['A', 'B', 'C'])
        store.append('mi', df)

        df = DataFrame(dict(A = 'foo', B = 'bar'),index=lrange(10))
        store.append('df', df, data_columns = ['B'], min_itemsize={'A' : 200 })
        store.append('wp', wp)

        store.close() 
Example #16
Source File: test_panel.py    From vnpy_crypto with MIT License 6 votes vote down vote up
def test_to_xarray(self):
        from xarray import DataArray

        with catch_warnings(record=True):
            p = tm.makePanel()

            result = p.to_xarray()
            assert isinstance(result, DataArray)
            assert len(result.coords) == 3
            assert_almost_equal(list(result.coords.keys()),
                                ['items', 'major_axis', 'minor_axis'])
            assert len(result.dims) == 3

            # idempotency
            assert_panel_equal(result.to_pandas(), p)


# run all the tests, but wrap each in a warning catcher 
Example #17
Source File: test_generic.py    From vnpy_crypto with MIT License 6 votes vote down vote up
def test_take_invalid_kwargs(self):
        indices = [-3, 2, 0, 1]
        s = tm.makeFloatSeries()
        df = tm.makeTimeDataFrame()

        with catch_warnings(record=True):
            p = tm.makePanel()

        for obj in (s, df, p):
            msg = r"take\(\) got an unexpected keyword argument 'foo'"
            tm.assert_raises_regex(TypeError, msg, obj.take,
                                   indices, foo=2)

            msg = "the 'out' parameter is not supported"
            tm.assert_raises_regex(ValueError, msg, obj.take,
                                   indices, out=indices)

            msg = "the 'mode' parameter is not supported"
            tm.assert_raises_regex(ValueError, msg, obj.take,
                                   indices, mode='clip') 
Example #18
Source File: test_generic.py    From vnpy_crypto with MIT License 6 votes vote down vote up
def test_transpose(self):
        msg = (r"transpose\(\) got multiple values for "
               r"keyword argument 'axes'")
        for s in [tm.makeFloatSeries(), tm.makeStringSeries(),
                  tm.makeObjectSeries()]:
            # calls implementation in pandas/core/base.py
            tm.assert_series_equal(s.transpose(), s)
        for df in [tm.makeTimeDataFrame()]:
            tm.assert_frame_equal(df.transpose().transpose(), df)

        with catch_warnings(record=True):
            for p in [tm.makePanel()]:
                tm.assert_panel_equal(p.transpose(2, 0, 1)
                                      .transpose(1, 2, 0), p)
                tm.assert_raises_regex(TypeError, msg, p.transpose,
                                       2, 0, 1, axes=(2, 0, 1)) 
Example #19
Source File: test_generic.py    From vnpy_crypto with MIT License 6 votes vote down vote up
def test_numpy_transpose(self):
        msg = "the 'axes' parameter is not supported"

        s = tm.makeFloatSeries()
        tm.assert_series_equal(
            np.transpose(s), s)
        tm.assert_raises_regex(ValueError, msg,
                               np.transpose, s, axes=1)

        df = tm.makeTimeDataFrame()
        tm.assert_frame_equal(np.transpose(
            np.transpose(df)), df)
        tm.assert_raises_regex(ValueError, msg,
                               np.transpose, df, axes=1)

        with catch_warnings(record=True):
            p = tm.makePanel()
            tm.assert_panel_equal(np.transpose(
                np.transpose(p, axes=(2, 0, 1)),
                axes=(1, 2, 0)), p) 
Example #20
Source File: test_pytables.py    From Computable with MIT License 5 votes vote down vote up
def test_conv_read_write(self):

        try:

            def roundtrip(key, obj,**kwargs):
                obj.to_hdf(self.path, key,**kwargs)
                return read_hdf(self.path, key)

            o = tm.makeTimeSeries()
            assert_series_equal(o, roundtrip('series',o))

            o = tm.makeStringSeries()
            assert_series_equal(o, roundtrip('string_series',o))

            o = tm.makeDataFrame()
            assert_frame_equal(o, roundtrip('frame',o))

            o = tm.makePanel()
            assert_panel_equal(o, roundtrip('panel',o))

            # table
            df = DataFrame(dict(A=lrange(5), B=lrange(5)))
            df.to_hdf(self.path,'table',append=True)
            result = read_hdf(self.path, 'table', where = ['index>2'])
            assert_frame_equal(df[df.index>2],result)

        finally:
            safe_remove(self.path) 
Example #21
Source File: test_pytables.py    From Computable with MIT License 5 votes vote down vote up
def test_wide_table(self):

        wp = tm.makePanel()
        self._check_roundtrip_table(wp, assert_panel_equal) 
Example #22
Source File: test_concat.py    From predictive-maintenance-using-machine-learning with Apache License 2.0 5 votes vote down vote up
def test_panel_concat_other_axes(self):
        panel = tm.makePanel()

        p1 = panel.iloc[:, :5, :]
        p2 = panel.iloc[:, 5:, :]

        result = concat([p1, p2], axis=1)
        tm.assert_panel_equal(result, panel)

        p1 = panel.iloc[:, :, :2]
        p2 = panel.iloc[:, :, 2:]

        result = concat([p1, p2], axis=2)
        tm.assert_panel_equal(result, panel)

        # if things are a bit misbehaved
        p1 = panel.iloc[:2, :, :2]
        p2 = panel.iloc[:, :, 2:]
        p1['ItemC'] = 'baz'

        result = concat([p1, p2], axis=2)

        expected = panel.copy()
        expected['ItemC'] = expected['ItemC'].astype('O')
        expected.loc['ItemC', :, :2] = 'baz'
        tm.assert_panel_equal(result, expected) 
Example #23
Source File: test_pytables.py    From Computable with MIT License 5 votes vote down vote up
def test_keys(self):

        with ensure_clean_store(self.path) as store:
            store['a'] = tm.makeTimeSeries()
            store['b'] = tm.makeStringSeries()
            store['c'] = tm.makeDataFrame()
            store['d'] = tm.makePanel()
            store['foo/bar'] = tm.makePanel()
            self.assertEquals(len(store), 5)
            self.assert_(set(
                    store.keys()) == set(['/a', '/b', '/c', '/d', '/foo/bar'])) 
Example #24
Source File: test_pytables.py    From Computable with MIT License 5 votes vote down vote up
def test_invalid_terms(self):

        with ensure_clean_store(self.path) as store:

            df = tm.makeTimeDataFrame()
            df['string'] = 'foo'
            df.ix[0:4,'string'] = 'bar'
            wp = tm.makePanel()
            p4d = tm.makePanel4D()
            store.put('df', df, format='table')
            store.put('wp', wp, format='table')
            store.put('p4d', p4d, format='table')

            # some invalid terms
            self.assertRaises(ValueError, store.select, 'wp', "minor=['A', 'B']")
            self.assertRaises(ValueError, store.select, 'wp', ["index=['20121114']"])
            self.assertRaises(ValueError, store.select, 'wp', ["index=['20121114', '20121114']"])
            self.assertRaises(TypeError, Term)

            # more invalid
            self.assertRaises(ValueError,  store.select, 'df','df.index[3]')
            self.assertRaises(SyntaxError, store.select, 'df','index>')
            self.assertRaises(ValueError,  store.select, 'wp', "major_axis<'20000108' & minor_axis['A', 'B']")

        # from the docs
        with ensure_clean_path(self.path) as path:
            dfq = DataFrame(np.random.randn(10,4),columns=list('ABCD'),index=date_range('20130101',periods=10))
            dfq.to_hdf(path,'dfq',format='table',data_columns=True)

            # check ok
            read_hdf(path,'dfq',where="index>Timestamp('20130104') & columns=['A', 'B']")
            read_hdf(path,'dfq',where="A>0 or C>0")

        # catch the invalid reference
        with ensure_clean_path(self.path) as path:
            dfq = DataFrame(np.random.randn(10,4),columns=list('ABCD'),index=date_range('20130101',periods=10))
            dfq.to_hdf(path,'dfq',format='table')

            self.assertRaises(ValueError, read_hdf, path,'dfq',where="A>0 or C>0") 
Example #25
Source File: test_pytables.py    From Computable with MIT License 5 votes vote down vote up
def test_wide_table_dups(self):
        wp = tm.makePanel()
        with ensure_clean_store(self.path) as store:
            store.put('panel', wp, format='table')
            store.put('panel', wp, format='table', append=True)

            with tm.assert_produces_warning(expected_warning=DuplicateWarning):
                recons = store['panel']

            assert_panel_equal(recons, wp) 
Example #26
Source File: test_merge.py    From Computable with MIT License 5 votes vote down vote up
def test_panel_concat_other_axes(self):
        panel = tm.makePanel()

        p1 = panel.ix[:, :5, :]
        p2 = panel.ix[:, 5:, :]

        result = concat([p1, p2], axis=1)
        tm.assert_panel_equal(result, panel)

        p1 = panel.ix[:, :, :2]
        p2 = panel.ix[:, :, 2:]

        result = concat([p1, p2], axis=2)
        tm.assert_panel_equal(result, panel)

        # if things are a bit misbehaved
        p1 = panel.ix[:2, :, :2]
        p2 = panel.ix[:, :, 2:]
        p1['ItemC'] = 'baz'

        result = concat([p1, p2], axis=2)

        expected = panel.copy()
        expected['ItemC'] = expected['ItemC'].astype('O')
        expected.ix['ItemC', :, :2] = 'baz'
        tm.assert_panel_equal(result, expected) 
Example #27
Source File: test_merge.py    From Computable with MIT License 5 votes vote down vote up
def test_panel_join_many(self):
        tm.K = 10
        panel = tm.makePanel()
        tm.K = 4

        panels = [panel.ix[:2], panel.ix[2:6], panel.ix[6:]]

        joined = panels[0].join(panels[1:])
        tm.assert_panel_equal(joined, panel)

        panels = [panel.ix[:2, :-5], panel.ix[2:6, 2:], panel.ix[6:, 5:-7]]

        data_dict = {}
        for p in panels:
            data_dict.update(compat.iteritems(p))

        joined = panels[0].join(panels[1:], how='inner')
        expected = Panel.from_dict(data_dict, intersect=True)
        tm.assert_panel_equal(joined, expected)

        joined = panels[0].join(panels[1:], how='outer')
        expected = Panel.from_dict(data_dict, intersect=False)
        tm.assert_panel_equal(joined, expected)

        # edge cases
        self.assertRaises(ValueError, panels[0].join, panels[1:],
                          how='outer', lsuffix='foo', rsuffix='bar')
        self.assertRaises(ValueError, panels[0].join, panels[1:],
                          how='right') 
Example #28
Source File: test_merge.py    From Computable with MIT License 5 votes vote down vote up
def test_panel_join_overlap(self):
        panel = tm.makePanel()
        tm.add_nans(panel)

        p1 = panel.ix[['ItemA', 'ItemB', 'ItemC']]
        p2 = panel.ix[['ItemB', 'ItemC']]

        joined = p1.join(p2, lsuffix='_p1', rsuffix='_p2')
        p1_suf = p1.ix[['ItemB', 'ItemC']].add_suffix('_p1')
        p2_suf = p2.ix[['ItemB', 'ItemC']].add_suffix('_p2')
        no_overlap = panel.ix[['ItemA']]
        expected = p1_suf.join(p2_suf).join(no_overlap)
        tm.assert_panel_equal(joined, expected) 
Example #29
Source File: test_merge.py    From Computable with MIT License 5 votes vote down vote up
def test_panel_join(self):
        panel = tm.makePanel()
        tm.add_nans(panel)

        p1 = panel.ix[:2, :10, :3]
        p2 = panel.ix[2:, 5:, 2:]

        # left join
        result = p1.join(p2)
        expected = p1.copy()
        expected['ItemC'] = p2['ItemC']
        tm.assert_panel_equal(result, expected)

        # right join
        result = p1.join(p2, how='right')
        expected = p2.copy()
        expected['ItemA'] = p1['ItemA']
        expected['ItemB'] = p1['ItemB']
        expected = expected.reindex(items=['ItemA', 'ItemB', 'ItemC'])
        tm.assert_panel_equal(result, expected)

        # inner join
        result = p1.join(p2, how='inner')
        expected = panel.ix[:, 5:10, 2:3]
        tm.assert_panel_equal(result, expected)

        # outer join
        result = p1.join(p2, how='outer')
        expected = p1.reindex(major=panel.major_axis,
                              minor=panel.minor_axis)
        expected = expected.join(p2.reindex(major=panel.major_axis,
                                            minor=panel.minor_axis))
        tm.assert_panel_equal(result, expected) 
Example #30
Source File: test_pytables.py    From Computable with MIT License 5 votes vote down vote up
def test_wide(self):

        wp = tm.makePanel()
        self._check_roundtrip(wp, assert_panel_equal)