Beispiel #1
0
    def test_pandas_series_loading(self):
        """Pandas Series objects are correctly loaded"""
        # Test valid series types
        name = ['_x', ' name']
        length = [0, 1, 2]
        index_key = [None, 'ix', 1]
        index_types = ['int', 'char', 'datetime', 'Timestamp']
        value_key = [None, 'x', 1]
        value_types = ['int', 'char', 'datetime', 'Timestamp', 'float',
                       'numpy float', 'numpy int']

        series_info = product(name, length, index_key, index_types,
                              value_key, value_types)

        for n, l, ikey, itype, vkey, vtype in series_info:
            index = sequences[itype](l)
            series = pd.Series(sequences[vtype](l), index=index, name=n,)

            vkey = vkey or series.name
            expected = [{'idx': Data.serialize(i), 'col': vkey,
                         'val': Data.serialize(v)}
                        for i, v in zip(index, series)]

            data = Data.from_pandas(series, name=n, series_key=vkey)
            nt.assert_list_equal(expected, data.values)
            nt.assert_equal(n, data.name)
            data.to_json()

        # Missing a name
        series = pd.Series(np.random.randn(10))
        data = Data.from_pandas(series)
        nt.assert_equal(data.name, 'table')
Beispiel #2
0
    def test_pandas_series_loading(self):
        """Pandas Series objects are correctly loaded"""
        # Test valid series types
        name = ['_x', ' name']
        length = [0, 1, 2]
        index_key = [None, 'ix', 1]
        index_types = ['int', 'char', 'datetime', 'Timestamp']
        value_key = [None, 'x', 1]
        value_types = ['int', 'char', 'datetime', 'Timestamp', 'float',
                       'numpy float', 'numpy int']

        series_info = product(name, length, index_key, index_types,
                              value_key, value_types)

        for n, l, ikey, itype, vkey, vtype in series_info:
            index = sequences[itype](l)
            series = pd.Series(sequences[vtype](l), index=index, name=n,)

            vkey = series.name or vkey
            expected = [{'idx': Data.serialize(i), 'col': vkey,
                         'val': Data.serialize(v)}
                        for i, v in zip(index, series)]

            data = Data.from_pandas(series, name=n, series_key=vkey)
            nt.assert_list_equal(expected, data.values)
            nt.assert_equal(n, data.name)
            data.to_json()

        # Missing a name
        series = pd.Series(np.random.randn(10))
        data = Data.from_pandas(series)
        nt.assert_equal(data.name, 'table')
Beispiel #3
0
    def test_pandas_dataframe_loading(self):

        # Simple columns/key_on tests
        df = pd.DataFrame({'one': [1, 2, 3], 'two': [6, 7, 8],
                           'three': [11, 12, 13], 'four': [17, 18, 19]})
        get_all = [{'col': 'four', 'idx': 0, 'val': 17},
                   {'col': 'one', 'idx': 0, 'val': 1},
                   {'col': 'three', 'idx': 0, 'val': 11},
                   {'col': 'two', 'idx': 0, 'val': 6},
                   {'col': 'four', 'idx': 1, 'val': 18},
                   {'col': 'one', 'idx': 1, 'val': 2},
                   {'col': 'three', 'idx': 1, 'val': 12},
                   {'col': 'two', 'idx': 1, 'val': 7},
                   {'col': 'four', 'idx': 2, 'val': 19},
                   {'col': 'one', 'idx': 2, 'val': 3},
                   {'col': 'three', 'idx': 2, 'val': 13},
                   {'col': 'two', 'idx': 2, 'val': 8}]
        get1 = [{'col': 'one', 'idx': 0, 'val': 1},
                {'col': 'one', 'idx': 1, 'val': 2},
                {'col': 'one', 'idx': 2, 'val': 3}]
        get2 = [{'col': 'one', 'idx': 0, 'val': 1},
                {'col': 'two', 'idx': 0, 'val': 6},
                {'col': 'one', 'idx': 1, 'val': 2},
                {'col': 'two', 'idx': 1, 'val': 7},
                {'col': 'one', 'idx': 2, 'val': 3},
                {'col': 'two', 'idx': 2, 'val': 8}]
        getkey2 = [{'col': 'one', 'idx': 6, 'val': 1},
                   {'col': 'one', 'idx': 7, 'val': 2},
                   {'col': 'one', 'idx': 8, 'val': 3}]
        getkey3 = [{'col': 'one', 'idx': 11, 'val': 1},
                   {'col': 'two', 'idx': 11, 'val': 6},
                   {'col': 'one', 'idx': 12, 'val': 2},
                   {'col': 'two', 'idx': 12, 'val': 7},
                   {'col': 'one', 'idx': 13, 'val': 3},
                   {'col': 'two', 'idx': 13, 'val': 8}]
        val_all = Data.from_pandas(df)
        val1 = Data.from_pandas(df, columns=['one'])
        val2 = Data.from_pandas(df, columns=['one', 'two'])
        key2 = Data.from_pandas(df, columns=['one'], key_on='two')
        key3 = Data.from_pandas(df, columns=['one', 'two'], key_on='three')

        nt.assert_list_equal(val_all.values, get_all)
        nt.assert_list_equal(val1.values, get1)
        nt.assert_list_equal(val2.values, get2)
        nt.assert_list_equal(key2.values, getkey2)
        nt.assert_list_equal(key3.values, getkey3)

        # Missing a name
        dataframe = pd.DataFrame(np.random.randn(10, 3))
        data = Data.from_pandas(dataframe)
        nt.assert_equal(data.name, 'table')

        # Bad obj
        nt.assert_raises(ValueError, Data.from_pandas, {})
Beispiel #4
0
    def test_pandas_dataframe_loading(self):
        """Pandas DataFrame objects are correctly loaded"""
        # name = ['_x']
        # length = [0, 1, 2]
        # index_key = [None, 'ix', 1]
        # index_types = ['int', 'char', 'datetime', 'Timestamp']
        # column_types = ['int', 'char', 'datetime', 'Timestamp']

        # # Leaving out some basic types here because we're not worried about
        # # serialization.
        # value_types = [
        #     'char', 'datetime', 'Timestamp', 'numpy float', 'numpy int']

        # dataframe_info = product(
        #     name, length, length, index_key, index_types, column_types,
        #     value_types)
        # for n, rows, cols, ikey, itype, ctype, vtype in dataframe_info:
        #     index = sequences[itype](rows)
        #     columns = sequences[ctype](cols)
        #     series = {
        #         c: pd.Series(sequences[vtype](rows), index=index, name=n)
        #         for c in columns}
        #     dataframe = pd.DataFrame(series)

        #     ikey = ikey or Data._default_index_key
        #     if cols == 0:
        #         expected = []
        #     else:
        #         expected = [
        #             dict([(ikey, Data.serialize(index[i]))] +
        #                  [(str(c), Data.serialize(series[c][i]))
        #                   for c in columns])
        #             for i in xrange(rows)]

        #     data = Data.from_pandas(dataframe, name=n, index_key=ikey)
        #     nt.assert_list_equal(expected, data.values)
        #     nt.assert_equal(n, data.name)
        #     data.to_json()

        #Simple columns/key_on tests
        df = pd.DataFrame({'one': [1,2,3], 'two': [6,7,8],
                           'three': [11, 12, 13], 'four': [17, 18, 19]})
        get_all = [{'col': 'four', 'idx': 0, 'val': 17},
                   {'col': 'one', 'idx': 0, 'val': 1},
                   {'col': 'three', 'idx': 0, 'val': 11},
                   {'col': 'two', 'idx': 0, 'val': 6},
                   {'col': 'four', 'idx': 1, 'val': 18},
                   {'col': 'one', 'idx': 1, 'val': 2},
                   {'col': 'three', 'idx': 1, 'val': 12},
                   {'col': 'two', 'idx': 1, 'val': 7},
                   {'col': 'four', 'idx': 2, 'val': 19},
                   {'col': 'one', 'idx': 2, 'val': 3},
                   {'col': 'three', 'idx': 2, 'val': 13},
                   {'col': 'two', 'idx': 2, 'val': 8}]
        get1 = [{'col': 'one', 'idx': 0, 'val': 1},
                {'col': 'one', 'idx': 1, 'val': 2},
                {'col': 'one', 'idx': 2, 'val': 3}]
        get2 = [{'col': 'one', 'idx': 0, 'val': 1},
                {'col': 'two', 'idx': 0, 'val': 6},
                {'col': 'one', 'idx': 1, 'val': 2},
                {'col': 'two', 'idx': 1, 'val': 7},
                {'col': 'one', 'idx': 2, 'val': 3},
                {'col': 'two', 'idx': 2, 'val': 8}]
        getkey2 = [{'col': 'one', 'idx': 6, 'val': 1},
                   {'col': 'one', 'idx': 7, 'val': 2},
                   {'col': 'one', 'idx': 8, 'val': 3}]
        getkey3 = [{'col': 'one', 'idx': 11, 'val': 1},
                   {'col': 'two', 'idx': 11, 'val': 6},
                   {'col': 'one', 'idx': 12, 'val': 2},
                   {'col': 'two', 'idx': 12, 'val': 7},
                   {'col': 'one', 'idx': 13, 'val': 3},
                   {'col': 'two', 'idx': 13, 'val': 8}]
        val_all = Data.from_pandas(df)
        val1 = Data.from_pandas(df, columns=['one'])
        val2 = Data.from_pandas(df, columns=['one', 'two'])
        key2 = Data.from_pandas(df, columns=['one'], key_on='two')
        key3 = Data.from_pandas(df, columns=['one', 'two'], key_on='three')

        nt.assert_list_equal(val_all.values, get_all)
        nt.assert_list_equal(val1.values, get1)
        nt.assert_list_equal(val2.values, get2)
        nt.assert_list_equal(key2.values, getkey2)
        nt.assert_list_equal(key3.values, getkey3)

        # Missing a name
        dataframe = pd.DataFrame(np.random.randn(10, 3))
        data = Data.from_pandas(dataframe)
        nt.assert_equal(data.name, 'table')

        #Bad obj
        nt.assert_raises(ValueError, Data.from_pandas, {})
Beispiel #5
0
    def test_pandas_dataframe_loading(self):
        """Pandas DataFrame objects are correctly loaded"""
        # name = ['_x']
        # length = [0, 1, 2]
        # index_key = [None, 'ix', 1]
        # index_types = ['int', 'char', 'datetime', 'Timestamp']
        # column_types = ['int', 'char', 'datetime', 'Timestamp']

        # # Leaving out some basic types here because we're not worried about
        # # serialization.
        # value_types = [
        #     'char', 'datetime', 'Timestamp', 'numpy float', 'numpy int']

        # dataframe_info = product(
        #     name, length, length, index_key, index_types, column_types,
        #     value_types)
        # for n, rows, cols, ikey, itype, ctype, vtype in dataframe_info:
        #     index = sequences[itype](rows)
        #     columns = sequences[ctype](cols)
        #     series = {
        #         c: pd.Series(sequences[vtype](rows), index=index, name=n)
        #         for c in columns}
        #     dataframe = pd.DataFrame(series)

        #     ikey = ikey or Data._default_index_key
        #     if cols == 0:
        #         expected = []
        #     else:
        #         expected = [
        #             dict([(ikey, Data.serialize(index[i]))] +
        #                  [(str(c), Data.serialize(series[c][i]))
        #                   for c in columns])
        #             for i in xrange(rows)]

        #     data = Data.from_pandas(dataframe, name=n, index_key=ikey)
        #     nt.assert_list_equal(expected, data.values)
        #     nt.assert_equal(n, data.name)
        #     data.to_json()

        #Simple columns/key_on tests
        df = pd.DataFrame({
            'one': [1, 2, 3],
            'two': [6, 7, 8],
            'three': [11, 12, 13],
            'four': [17, 18, 19]
        })
        get_all = [{
            'col': 'four',
            'idx': 0,
            'val': 17
        }, {
            'col': 'one',
            'idx': 0,
            'val': 1
        }, {
            'col': 'three',
            'idx': 0,
            'val': 11
        }, {
            'col': 'two',
            'idx': 0,
            'val': 6
        }, {
            'col': 'four',
            'idx': 1,
            'val': 18
        }, {
            'col': 'one',
            'idx': 1,
            'val': 2
        }, {
            'col': 'three',
            'idx': 1,
            'val': 12
        }, {
            'col': 'two',
            'idx': 1,
            'val': 7
        }, {
            'col': 'four',
            'idx': 2,
            'val': 19
        }, {
            'col': 'one',
            'idx': 2,
            'val': 3
        }, {
            'col': 'three',
            'idx': 2,
            'val': 13
        }, {
            'col': 'two',
            'idx': 2,
            'val': 8
        }]
        get1 = [{
            'col': 'one',
            'idx': 0,
            'val': 1
        }, {
            'col': 'one',
            'idx': 1,
            'val': 2
        }, {
            'col': 'one',
            'idx': 2,
            'val': 3
        }]
        get2 = [{
            'col': 'one',
            'idx': 0,
            'val': 1
        }, {
            'col': 'two',
            'idx': 0,
            'val': 6
        }, {
            'col': 'one',
            'idx': 1,
            'val': 2
        }, {
            'col': 'two',
            'idx': 1,
            'val': 7
        }, {
            'col': 'one',
            'idx': 2,
            'val': 3
        }, {
            'col': 'two',
            'idx': 2,
            'val': 8
        }]
        getkey2 = [{
            'col': 'one',
            'idx': 6,
            'val': 1
        }, {
            'col': 'one',
            'idx': 7,
            'val': 2
        }, {
            'col': 'one',
            'idx': 8,
            'val': 3
        }]
        getkey3 = [{
            'col': 'one',
            'idx': 11,
            'val': 1
        }, {
            'col': 'two',
            'idx': 11,
            'val': 6
        }, {
            'col': 'one',
            'idx': 12,
            'val': 2
        }, {
            'col': 'two',
            'idx': 12,
            'val': 7
        }, {
            'col': 'one',
            'idx': 13,
            'val': 3
        }, {
            'col': 'two',
            'idx': 13,
            'val': 8
        }]
        val_all = Data.from_pandas(df)
        val1 = Data.from_pandas(df, columns=['one'])
        val2 = Data.from_pandas(df, columns=['one', 'two'])
        key2 = Data.from_pandas(df, columns=['one'], key_on='two')
        key3 = Data.from_pandas(df, columns=['one', 'two'], key_on='three')

        nt.assert_list_equal(val_all.values, get_all)
        nt.assert_list_equal(val1.values, get1)
        nt.assert_list_equal(val2.values, get2)
        nt.assert_list_equal(key2.values, getkey2)
        nt.assert_list_equal(key3.values, getkey3)

        # Missing a name
        dataframe = pd.DataFrame(np.random.randn(10, 3))
        data = Data.from_pandas(dataframe)
        nt.assert_equal(data.name, 'table')

        #Bad obj
        nt.assert_raises(ValueError, Data.from_pandas, {})
Beispiel #6
0
    def test_pandas_dataframe_loading(self):

        # Simple columns/key_on tests
        df = pd.DataFrame({
            'one': [1, 2, 3],
            'two': [6, 7, 8],
            'three': [11, 12, 13],
            'four': [17, 18, 19]
        })
        get_all = [{
            'col': 'four',
            'idx': 0,
            'val': 17
        }, {
            'col': 'one',
            'idx': 0,
            'val': 1
        }, {
            'col': 'three',
            'idx': 0,
            'val': 11
        }, {
            'col': 'two',
            'idx': 0,
            'val': 6
        }, {
            'col': 'four',
            'idx': 1,
            'val': 18
        }, {
            'col': 'one',
            'idx': 1,
            'val': 2
        }, {
            'col': 'three',
            'idx': 1,
            'val': 12
        }, {
            'col': 'two',
            'idx': 1,
            'val': 7
        }, {
            'col': 'four',
            'idx': 2,
            'val': 19
        }, {
            'col': 'one',
            'idx': 2,
            'val': 3
        }, {
            'col': 'three',
            'idx': 2,
            'val': 13
        }, {
            'col': 'two',
            'idx': 2,
            'val': 8
        }]
        get1 = [{
            'col': 'one',
            'idx': 0,
            'val': 1
        }, {
            'col': 'one',
            'idx': 1,
            'val': 2
        }, {
            'col': 'one',
            'idx': 2,
            'val': 3
        }]
        get2 = [{
            'col': 'one',
            'idx': 0,
            'val': 1
        }, {
            'col': 'two',
            'idx': 0,
            'val': 6
        }, {
            'col': 'one',
            'idx': 1,
            'val': 2
        }, {
            'col': 'two',
            'idx': 1,
            'val': 7
        }, {
            'col': 'one',
            'idx': 2,
            'val': 3
        }, {
            'col': 'two',
            'idx': 2,
            'val': 8
        }]
        getkey2 = [{
            'col': 'one',
            'idx': 6,
            'val': 1
        }, {
            'col': 'one',
            'idx': 7,
            'val': 2
        }, {
            'col': 'one',
            'idx': 8,
            'val': 3
        }]
        getkey3 = [{
            'col': 'one',
            'idx': 11,
            'val': 1
        }, {
            'col': 'two',
            'idx': 11,
            'val': 6
        }, {
            'col': 'one',
            'idx': 12,
            'val': 2
        }, {
            'col': 'two',
            'idx': 12,
            'val': 7
        }, {
            'col': 'one',
            'idx': 13,
            'val': 3
        }, {
            'col': 'two',
            'idx': 13,
            'val': 8
        }]
        val_all = Data.from_pandas(df)
        val1 = Data.from_pandas(df, columns=['one'])
        val2 = Data.from_pandas(df, columns=['one', 'two'])
        key2 = Data.from_pandas(df, columns=['one'], key_on='two')
        key3 = Data.from_pandas(df, columns=['one', 'two'], key_on='three')

        nt.assert_list_equal(val_all.values, get_all)
        nt.assert_list_equal(val1.values, get1)
        nt.assert_list_equal(val2.values, get2)
        nt.assert_list_equal(key2.values, getkey2)
        nt.assert_list_equal(key3.values, getkey3)

        # Missing a name
        dataframe = pd.DataFrame(np.random.randn(10, 3))
        data = Data.from_pandas(dataframe)
        nt.assert_equal(data.name, 'table')

        # Bad obj
        nt.assert_raises(ValueError, Data.from_pandas, {})