Ejemplo n.º 1
0
    def test_to_string_with_formatters(self):
        df = DataFrame({'int': [1, 2, 3],
                        'float': [1.0, 2.0, 3.0],
                        'object': [(1,2), True, False]},
                        columns=['int', 'float', 'object'])

        formatters = [('int', lambda x: '0x%x' % x),
                      ('float', lambda x: '[% 4.1f]' % x),
                      ('object', lambda x: '-%s-' % str(x))]
        result = df.to_string(formatters=dict(formatters))
        result2 = df.to_string(formatters=lzip(*formatters)[1])
        self.assertEqual(result, ('  int  float    object\n'
                                  '0 0x1 [ 1.0]  -(1, 2)-\n'
                                  '1 0x2 [ 2.0]    -True-\n'
                                  '2 0x3 [ 3.0]   -False-'))
        self.assertEqual(result, result2)
Ejemplo n.º 2
0
    def test_to_string_with_formatters(self):
        df = DataFrame(
            {"int": [1, 2, 3], "float": [1.0, 2.0, 3.0], "object": [(1, 2), True, False]},
            columns=["int", "float", "object"],
        )

        formatters = [
            ("int", lambda x: "0x%x" % x),
            ("float", lambda x: "[% 4.1f]" % x),
            ("object", lambda x: "-%s-" % str(x)),
        ]
        result = df.to_string(formatters=dict(formatters))
        result2 = df.to_string(formatters=lzip(*formatters)[1])
        self.assertEqual(
            result,
            ("  int  float    object\n" "0 0x1 [ 1.0]  -(1, 2)-\n" "1 0x2 [ 2.0]    -True-\n" "2 0x3 [ 3.0]   -False-"),
        )
        self.assertEqual(result, result2)
Ejemplo n.º 3
0
    def test_to_string_with_formatters(self):
        df = DataFrame(
            {
                'int': [1, 2, 3],
                'float': [1.0, 2.0, 3.0],
                'object': [(1, 2), True, False]
            },
            columns=['int', 'float', 'object'])

        formatters = [('int', lambda x: '0x%x' % x),
                      ('float', lambda x: '[% 4.1f]' % x),
                      ('object', lambda x: '-%s-' % str(x))]
        result = df.to_string(formatters=dict(formatters))
        result2 = df.to_string(formatters=lzip(*formatters)[1])
        self.assertEqual(result, ('  int  float    object\n'
                                  '0 0x1 [ 1.0]  -(1, 2)-\n'
                                  '1 0x2 [ 2.0]    -True-\n'
                                  '2 0x3 [ 3.0]   -False-'))
        self.assertEqual(result, result2)
    def _get_schema(self, data, tblname, format_file=None, keys=None):
        """
        Return a CREATE TABLE statement to suit the contents of a DataFrame.
        Extending existing pandas sql api to add support for MSSQL
        """
        column_types = []
        if self.dbFlavor == 'mssql' or self.dbFlavor == 'postgres':

            if format_file != None:
                column_types = self.parse_format_file(data, format_file)

            else:
                lookup_type = lambda dtype: self._get_type(dtype.type)

                # Replace spaces in DataFrame column names with _.
                if self.dbFlavor == 'mssql':
                    safe_columns = [
                        s.replace(' ', '_').replace('.', '_').replace(
                            '\n', '_').strip().rstrip('_')[:127]
                        for s in data.dtypes.index
                    ]
                elif self.dbFlavor == 'postgres':
                    safe_columns = make_psql_compatible_column(
                        data.dtypes.index)
                column_types = lzip(safe_columns, map(lookup_type,
                                                      data.dtypes))

            if self.dbFlavor == 'mssql':
                columns = ',\n  '.join('[%s] %s' % x for x in column_types)
            elif self.dbFlavor == 'postgres':
                column_list = []
                column_name_list = []
                #Checking column names for reserved words and non-ascii values
                for x in column_types:
                    col_name = x[0]
                    col_type = x[1]
                    column_list.append(col_name + ' ' + col_type)
                    column_name_list.append(col_name)

                columns = ',\n  '.join('%s' % x for x in column_list)

            keystr = ''
            if keys is not None:
                if isinstance(keys, pandas.compat.string_types):
                    keys = (keys, )
                keystr = ', PRIMARY KEY (%s)' % ','.join(keys)
            template = """CREATE TABLE %(name)s (
                    %(columns)s
                    %(keystr)s
                    );"""

            create_statement = template % {
                'name': tblname,
                'columns': columns,
                'keystr': keystr
            }

        else:
            raise NotImplementedError('This module is not implemented')

        if self.dbFlavor == 'mssql':
            return create_statement
        elif self.dbFlavor == 'postgres':
            return create_statement, column_name_list