Пример #1
0
    def test_supermeta(self):
        """
        Test that supermeta metaclass acts like a super parent
        to both UserSeries and UserFrame
        """
        class CommonBase(composition.PandasSuperMeta):
            """
            Test common base 
            """
            _bob = object()

            @property
            def bob(self):
                return self._bob

        class CommonSeries(UserSeries, metaclass=CommonBase):
            pass

        class CommonFrame(UserFrame, metaclass=CommonBase):
            pass

        bob = CommonBase._bob

        s = CommonSeries(list(range(10)))
        assert s.ix[3] == 3
        tm.assert_almost_equal(s, list(range(10)))
        assert s.bob is bob
        s._bob = 123
        assert s.bob == 123

        df = tm.makeDataFrame()
        fr = CommonFrame(df)
        tm.assert_almost_equal(fr.values, df.values)
        assert fr.bob is bob
        assert fr.tail().bob is bob
Пример #2
0
    def test_supermeta(self):
        """
        Test that supermeta metaclass acts like a super parent
        to both UserSeries and UserFrame
        """
        class CommonBase(composition.PandasSuperMeta):
            """
            Test common base 
            """
            _bob = object()

            @property
            def bob(self):
                return self._bob

        class CommonSeries(UserSeries):
            __metaclass__ = CommonBase

        class CommonFrame(UserFrame):
            __metaclass__ = CommonBase

        bob = CommonBase._bob

        s = CommonSeries(range(10))
        assert s.ix[3] == 3
        tm.assert_almost_equal(s, range(10))
        assert s.bob is bob
        s._bob = 123
        assert s.bob == 123

        df = tm.makeDataFrame()
        fr = CommonFrame(df)
        tm.assert_almost_equal(fr, df)
        assert fr.bob is bob
        assert fr.tail().bob is bob
Пример #3
0
    def test_convert_datetime(self):
        ind = pd.date_range(start='1/1/2002', end='12/30/2008', freq="30min")
        ri = conv.convert_datetime_index_num(ind)
        tm.assert_almost_equal(ri[0], ind.asi8[0] / 1E9)

        assert ri.do_slot('tzone')[0] == 'UTC'

        # convert back to datetime
        conv_dates = pd.Series(ri).astype(np.dtype('M8[s]'))
        tm.assert_almost_equal(conv_dates, ind)
Пример #4
0
    def test_convert_datetime(self):
        ind = pd.date_range(start='1/1/2002', end='12/30/2008', freq="30min")
        ri = conv.convert_datetime_index_num(ind)
        tm.assert_almost_equal(ri[0], ind.asi8[0] / 1E9)

        assert ri.do_slot('tzone')[0] == 'UTC'

        # convert back to datetime
        conv_dates = pd.Series(ri).astype(np.dtype('M8[s]'))
        tm.assert_almost_equal(conv_dates, ind)
Пример #5
0
    def test_dshift_fill_value(self):
        # float
        test = df.dshift(1, fill_value=-100)
        correct = df.shift(1).fillna(-100)
        tm.assert_almost_equal(test.values, correct.values)

        # int
        intdf = (df * 100).astype(int)
        test = intdf.dshift(1, fill_value=-100)
        correct = intdf.shift(1).fillna(-100)
        tm.assert_almost_equal(test.values, correct.values)
Пример #6
0
    def test_dshift_fill_value(self):
        # float
        test = df.dshift(1, fill_value=-100)
        correct = df.shift(1).fillna(-100)
        tm.assert_almost_equal(test.values, correct.values)

        # int
        intdf = (df * 100).astype(int)
        test = intdf.dshift(1, fill_value=-100)
        correct = intdf.shift(1).fillna(-100)
        tm.assert_almost_equal(test.values, correct.values)
Пример #7
0
    def test_dshift_float(self):
        """
        Since float is nan-able. The simple call should give the 
        same output
        """
        test = df.dshift(1)
        correct = df.shift(1)
        tm.assert_almost_equal(test.values, correct.values)

        test = df.dshift(-2)
        correct = df.shift(-2)
        tm.assert_almost_equal(test.values, correct.values)
Пример #8
0
    def test_dshift_float(self):
        """
        Since float is nan-able. The simple call should give the 
        same output
        """
        test = df.dshift(1)
        correct = df.shift(1)
        tm.assert_almost_equal(test.values, correct.values)

        test = df.dshift(-2)
        correct = df.shift(-2)
        tm.assert_almost_equal(test.values, correct.values)
Пример #9
0
    def test_dshift_raw(self):
        # bool
        bf = df > 0
        test = bf.dshift(1, raw=True)
        correct = bf.shift(1).fillna(False).astype(float)
        assert type(test) is np.ndarray
        tm.assert_almost_equal(test, correct.values)

        # float
        test = df.dshift(1, raw=True)
        correct = df.shift(1)
        assert type(test) is np.ndarray
        tm.assert_almost_equal(test, correct.values)
Пример #10
0
    def test_dshift_raw(self):
        # bool
        bf = df > 0
        test = bf.dshift(1, raw=True)
        correct = bf.shift(1).fillna(False).astype(float)
        assert type(test) is np.ndarray
        tm.assert_almost_equal(test, correct.values)

        # float
        test = df.dshift(1, raw=True)
        correct = df.shift(1)
        assert type(test) is np.ndarray
        tm.assert_almost_equal(test, correct.values)
Пример #11
0
    def test_dshift_int(self):
        """
        int has no nan.
        """
        intdf = (df * 100).astype(int)
        test = intdf.dshift(1)
        correct = intdf.shift(1).fillna(-1).astype(int)
        assert test.dtypes.unique()[0] == int
        assert test.dtypes.nunique() == 1
        tm.assert_almost_equal(test.values, correct.values)

        test = intdf.dshift(-2)
        correct = intdf.shift(-2).fillna(-1).astype(int)
        assert test.dtypes.unique()[0] == int
        assert test.dtypes.nunique() == 1
        tm.assert_almost_equal(test.values, correct.values)
Пример #12
0
    def test_dshift_bool(self):
        """
        bool has no nan.
        """
        bf = df > 0
        test = bf.dshift(1)
        correct = bf.shift(1).fillna(False).astype(bool)
        assert test.dtypes.unique()[0] == bool
        assert test.dtypes.nunique() == 1
        tm.assert_almost_equal(test.values, correct.values)

        test = bf.dshift(-2)
        correct = bf.shift(-2).fillna(False).astype(bool)
        assert test.dtypes.unique()[0] == bool
        assert test.dtypes.nunique() == 1
        tm.assert_almost_equal(test.values, correct.values)
Пример #13
0
    def test_dshift_bool(self):
        """
        bool has no nan.
        """
        bf = df > 0
        test = bf.dshift(1)
        correct = bf.shift(1).fillna(False).astype(bool)
        assert test.dtypes.unique()[0] == bool
        assert test.dtypes.nunique() == 1
        tm.assert_almost_equal(test.values, correct.values)

        test = bf.dshift(-2)
        correct = bf.shift(-2).fillna(False).astype(bool)
        assert test.dtypes.unique()[0] == bool
        assert test.dtypes.nunique() == 1
        tm.assert_almost_equal(test.values, correct.values)
Пример #14
0
    def test_dshift_int(self):
        """
        int has no nan.
        """
        intdf = (df * 100).astype(int)
        test = intdf.dshift(1)
        correct = intdf.shift(1).fillna(-1).astype(int)
        assert test.dtypes.unique()[0] == int
        assert test.dtypes.nunique() == 1
        tm.assert_almost_equal(test.values, correct.values)

        test = intdf.dshift(-2)
        correct = intdf.shift(-2).fillna(-1).astype(int)
        assert test.dtypes.unique()[0] == int
        assert test.dtypes.nunique() == 1
        tm.assert_almost_equal(test.values, correct.values)
Пример #15
0
    def test_convert_posixct_to_index(self):
        ind = pd.date_range(start='1/1/2002', end='12/30/2008', freq="30min")
        ri = conv.convert_datetime_index_num(ind)

        # test converting back 'UTC'
        dt = conv.convert_posixct_to_index(ri)
        tm.assert_almost_equal(dt, ind,)

        # test 'US/Eastern'
        # At one point this failed due to DatetimeIndex being 
        # given a tz. DatetimeIndex needs to be initialied to UTC
        # and then converted
        ri.do_slot_assign('tzone', StrSexpVector(['US/Eastern']))
        dt = conv.convert_posixct_to_index(ri)
        assert dt.tz.zone == 'US/Eastern'
        assert ind.tz is None
        tm.assert_almost_equal(dt, ind)

        ind = ind.tz_localize('UTC')
        est_ind = ind.tz_convert('US/Eastern')
        est_ri = conv.convert_datetime_index_num(est_ind)
        assert est_ri.do_slot('tzone')[0] == 'US/Eastern'
        est_dt = conv.convert_posixct_to_index(est_ri)
        tm.assert_almost_equal(est_dt, est_ind)
        assert est_dt.tz.zone == 'US/Eastern'
Пример #16
0
    def test_convert_posixct_to_index(self):
        ind = pd.date_range(start='1/1/2002', end='12/30/2008', freq="30min")
        ri = conv.convert_datetime_index_num(ind)

        # test converting back 'UTC'
        dt = conv.convert_posixct_to_index(ri)
        tm.assert_almost_equal(
            dt,
            ind,
        )

        # test 'US/Eastern'
        # At one point this failed due to DatetimeIndex being
        # given a tz. DatetimeIndex needs to be initialied to UTC
        # and then converted
        ri.do_slot_assign('tzone', StrSexpVector(['US/Eastern']))
        dt = conv.convert_posixct_to_index(ri)
        assert dt.tz.zone == 'US/Eastern'
        assert ind.tz is None
        tm.assert_almost_equal(dt, ind)

        ind = ind.tz_localize('UTC')
        est_ind = ind.tz_convert('US/Eastern')
        est_ri = conv.convert_datetime_index_num(est_ind)
        assert est_ri.do_slot('tzone')[0] == 'US/Eastern'
        est_dt = conv.convert_posixct_to_index(est_ri)
        tm.assert_almost_equal(est_dt, est_ind)
        assert est_dt.tz.zone == 'US/Eastern'
Пример #17
0
    def test_level_wrapper_mi(self):
        """
        Test level wrapper against MultiIndex
        """
        mi = pd.MultiIndex.from_tuples(sets)
        df = pd.DataFrame(np.random.randn(N, N), index=ind)
        df.columns = mi
        df.columns.names = ['limit', 'stop', 'target']
        m = MultiIndexGetter(df, attr='columns')
        lw = df.columns.lev.limit
        # test some ops
        test = lw == 0
        correct = mi.get_level_values('limit') == 0
        tm.assert_almost_equal(test, correct)

        test = lw > 1
        correct = mi.get_level_values('limit') > 1
        tm.assert_almost_equal(test, correct)

        # test getitem
        assert lw[0] == 0
        assert lw[1] == 1
Пример #18
0
    def test_level_wrapper_mi(self):
        """
        Test level wrapper against MultiIndex
        """
        mi = pd.MultiIndex.from_tuples(sets)
        df = pd.DataFrame(np.random.randn(N, N), index=ind)
        df.columns = mi
        df.columns.names = ['limit', 'stop', 'target']
        m = MultiIndexGetter(df, attr='columns')
        lw = df.columns.lev.limit
        # test some ops
        test = lw == 0
        correct = mi.get_level_values('limit') == 0
        tm.assert_almost_equal(test, correct)

        test = lw > 1
        correct = mi.get_level_values('limit') > 1
        tm.assert_almost_equal(test, correct)

        # test getitem
        assert lw[0] == 0
        assert lw[1] == 1
Пример #19
0
    def test_save(self):
        with TemporaryDirectory() as td:
            filepath = os.path.join(td, "test")

            df = pd.DataFrame({"test": list(range(10))})
            table = pddb.PandasTable(filepath)
            table._init_df(df)

            # save to string
            data = tm.TestStringIO()
            table.save(data)

            # PandasTable saves as df
            data.seek(0)
            new_table = pickle.loads(data.read())
            assert tm.assert_almost_equal(new_table.values, df.values)
            data.free()

            # save to disk
            table.save()
            pandas_table = pddb.PandasTable(filepath)
            assert tm.assert_almost_equal(pandas_table._df.values, df.values)
Пример #20
0
    def test_save(self):
        with TemporaryDirectory() as td:
            filepath = os.path.join(td, 'test')

            df = pd.DataFrame({'test':list(range(10))})
            table = pddb.PandasTable(filepath)
            table._init_df(df)

            # save to string
            data = tm.TestStringIO()
            table.save(data)

            # PandasTable saves as df
            data.seek(0)
            new_table = pickle.loads(data.read())
            assert tm.assert_almost_equal(new_table.values, df.values)
            data.free()

            # save to disk
            table.save()
            pandas_table = pddb.PandasTable(filepath)
            assert tm.assert_almost_equal(pandas_table._df.values, df.values)
Пример #21
0
    def test_init_df(self):
        with TemporaryDirectory() as td:
            filepath = os.path.join(td, "test")
            df = pd.DataFrame({"test": range(10)})
            table = pddb.PandasTable(filepath)
            table.init_df(df)

            assert df is table._df

            data = tm.TestStringIO(pickle.dumps(df))

            table = pddb.PandasTable(filepath)
            table._init_df(data)
            assert tm.assert_almost_equal(df, table._df)
Пример #22
0
    def test_init_df(self):
        with TemporaryDirectory() as td:
            filepath = os.path.join(td, 'test')
            df = pd.DataFrame({'test':range(10)})
            table = pddb.PandasTable(filepath)
            table.init_df(df)

            assert df is table._df

            data = tm.TestStringIO(pickle.dumps(df))

            table = pddb.PandasTable(filepath)
            table._init_df(data)
            assert tm.assert_almost_equal(df, table._df)
Пример #23
0
    def test_save(self):
        with TemporaryDirectory() as td:
            filepath = os.path.join(td, "test")

            df = pd.DataFrame({"test": range(10)})
            table = pddb.PandasTable(filepath)
            table._init_df(df)

            data = tm.TestStringIO()
            table.save(data)

            data.seek(0)
            new_table = pickle.loads(data.read())
            assert tm.assert_almost_equal(new_table._df, df)
            data.free()
Пример #24
0
    def test_save(self):
        with TemporaryDirectory() as td:
            filepath = os.path.join(td, 'test')

            df = pd.DataFrame({'test':range(10)})
            table = pddb.PandasTable(filepath)
            table._init_df(df)

            data = tm.TestStringIO()
            table.save(data)

            data.seek(0)
            new_table = pickle.loads(data.read())
            assert tm.assert_almost_equal(new_table._df, df)
            data.free()